3

The error is:

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.
The username already exists in this or another Salesforce organization. Usernames must be unique across all Salesforce organizations. To resolve, use a different username (it doesn't need to match the user's email address). : [Username] Class.testForActivityTrigger.testRunAs: line 11, column 1

@isTest(SeeAllData=true)
Public class testForActivityTrigger{
    public static testMethod void testRunAs() {
        Profile p = [SELECT Id FROM Profile WHERE Name='BLP_NA'];

        User u = new User(Alias = 'standt', Email='[email protected]', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]');

        System.runas(u){    
            Account acct = new Account(Name = 'Apex Test');
            insert acct;

            Task tsk1 = new Task(WhatId = acct.Id, Subject = 'Email: apex test', ActivityDate = date.today(), Status = 'Completed');
            insert tsk1;

            Property__c objProperty = new Property__c(name = 'Apex Test');
            insert objProperty;

            Property_Relation__c objPropRelation = new Property_Relation__c(Property__c = objProperty.id , Account__c =acct.Id );
            insert objPropRelation ;
        }
    }
}
2
  • At which line and what is the exact error message?
    – Keith C
    Aug 5, 2016 at 11:27
  • 4
    Let me guess... duplicate username.
    – Adrian Larson
    Aug 5, 2016 at 11:28

1 Answer 1

6

The Username field is unique across all orgs. Try using a value less likely to collide. Something like [email protected]. You can randomize it more by adding static counters, Math.random, etc. Here are a couple options for your one-off case.

'[email protected].' + Math.random()
'[email protected].' + Datetime.now().getTime()

A refresher from the documentation for those who may have forgotten or not seen it:

The runAs method implicitly inserts the user that is passed in as parameter if the user has been instantiated, but not inserted yet.

4
  • 3
    Yes, We need to make username unique across all orgs. One way is to use getTime() method. Then username could be written as UserName=System.now().getTime()+'[email protected]'. The getTime() method Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this DateTime object
    – Devendra
    Aug 5, 2016 at 12:34
  • @Devendra yes there are many workable approaches.
    – Adrian Larson
    Aug 5, 2016 at 12:36
  • @AdrianLarson Yes, you are correct :). There are many ways to make the unique username. This is the one I generally use.
    – Devendra
    Aug 5, 2016 at 12:38
  • Added a few concrete examples. It's more reliable than random, probably.
    – Adrian Larson
    Aug 5, 2016 at 12:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .