Recently, while doing deployment through change set, I faced an issue –
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.
Another user has already selected this username.
Please select another.: [Username]”
The username used in test classes were definitely unique, but still my test classes were failing with the below error message. After spending some quite amount of time, I have understood the problem. So thought of putting this in my blog hoping that it may help someone in future.
So here it goes –
Error message –
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.
Another user has already selected this username.
Please select another.: [Username]”
Reason –
Usernames are always shared across instances, but not across environments. It means if a user is having username as test@test.com in any sandbox, then this same username you can’t use in any other sandbox instances (CS*), but you can user the same username in production sandboxes (NA*, EU*, AP*).
Uniqueness is always checked during deployment when tests are run. So an insert from test class will also fail if the username is already registered in another org in the same environments. So we need to make sure the uniqueness in maintained in test classes.
Resolution –
To be 100% sure that the username used in test classes are unique, you should follow the below approach to define the username –
String orgId = UserInfo.getOrganizationId();
String dateString = String.valueof(Datetime.now()).replace(‘ ‘,”).replace(‘:’,”).replace(‘-‘,”);
Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
String uniqueName = orgId + dateString + randomInt;
Below is the link which you can check. Link
About The Author
Related Posts
Recent Posts
Admin Apex Architect array B2B B2C Best Practices Business Analyst Committed Use Discount css css3 Custom Custom Image dense array Designer Exam Developer development DoS Enhanced Domain Flow Google Cloud Google Cloud Certification Google Cloud Digital Leader Google Cloud Platform group Instance Template JavaScript Lightning Web Components Loyalty Management Preemptible pseudo-class Release Salesforce Salesforce Certification Security selectors sparse array specificity specificity-rules Sustained Use Discount TypeScript Virtual Machine vscode web where
Recent Comments
Archives
2023
- January 9 Salesforce Flow Best Practices
- January 3 10 Apex Programming Best Practices
2022
- December 12 Setup Loyalty Program in Salesforce
- December 1 Introduction to Dart Programming
- November 23 How to Pass Salesforce Certified Associate Exam
- November 14 How do I setup a static IP with Google Cloud?
- August 26 TypeScript Setup
- August 25 Introduction to TypeScript
- April 18 JavaScript Void Operator
- January 12 Understand Regions and Zones in Google Cloud
- January 10 How To Start With Google Cloud
2021
- October 26 Top JavaScript Console Features
- October 14 Summer ’21 Flow New Features
- October 14 Events in JavaScript
- April 20 Understand JavaScript Closure
- April 15 Functions in JavaScript
- March 31 Sorting Arrays in JavaScript
- March 3 Dynamic Action in Salesforce
- March 2 Dynamic Form in Salesforce
- February 18 How to Query Multi-Currency Fields in Salesforce
- January 5 Top Admin Features from Spring 21
2020
- December 28 Top Apex Features From Spring 21
- December 25 It’s Christmas Time!!! Happy ho-ho- holidays!
- November 16 Truncate Custom Objects in Salesforce
- October 13 Dataloader for Salesforce
- October 12 Send Custom Notifications Using Apex
- September 22 Using Apex with Knowledge
- September 21 Handy SOQL/SOSL Queries for Knowledge
- September 17 Understand Salesforce Lightning Knowledge Data Model
- September 16 Queueable Apex
- September 14 New Admin Features From Winter 21 Release
- September 7 How to update “N” records in Flow
- August 26 Winter ’21 Apex New Feature
- August 25 Winter ’21 Flow Enhancements
- February 7 Dreamforce 2019 Global Gathering
- January 9 Compare Knowledge Articles – WoW Feature
2019
- July 26 All About Asynchronous Apex
- February 10 Understanding JavaScript Promises
2018
- December 28 Happy New Year and Keep Learning
- October 1 Getting Started with Salesforce DX
- September 10 Getting Started with Financial Services Cloud
- February 3 LastModifiedDate Vs SystemModStamp
2017
- February 25 Salesforce Optimizer – Spring 17 New Feature
- January 23 Product Schedule in Salesforce
- January 22 Products and Price Books in Salesforce
- January 18 Delegate Administrator
- January 15 JavaScript Tips for Visualforce Developers
2016
- December 7 All about Git – “Git Stash” Part – II
- December 6 All about Git – Cheat Sheet – Basic Part – I
- October 1 Dynamic Apex – Playing with CRUD and FLS
2015
- August 11 APEX TRIGGER DESIGN PATTERN
- July 19 DYNAMIC VISUALFORCE BINDINGS
- February 5 Spring ’15 – New Feature Added – @testSetup
- January 30 Google Polymer – Override Style
- January 29 Types of Chart for Dashboard in Salesforce
- January 29 Types of Report in Salesforce
- January 26 Google Polymer – First Hello World Component
- January 25 Google Polymer – How to Install
- January 25 Ideas in Salesforce
- January 23 Solution Management in Salesforce Part I
- January 10 Campaign in Salesforce and Security involved
- January 10 Admin Notes — Licenses in Salesforce
- January 3 Integrating jQuery with Visualforce
- January 1 Happy New Year 2015 !!!!
2014
- November 17 How Salesforce Does Agile
- November 17 Winter ’15 Release-Overview and Highlights
- October 31 Different Chatter Licenses in Salesforce
- October 31 Standard Chatter Profiles in Salesforce
- October 31 Standard Profiles available in Salesforce
- October 26 Diwali 2014
- October 17 JSON2Apex – Convert JSON Into Apex Class
- October 15 Custom Logging Framework in Salesforce
- October 7 Before Delete Trigger in Salesforce