function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lee_CampbellLee_Campbell 

Test Class to create a portal user...

Hi folks,

 

I'm trying to create a portal user as part of a test class. I was building the test class piece-by-piece, smoothing out the errors as I went along, but I'm stuck with this one, even though my Test Class, below, contains a ContactId.

 

Error: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Cannot create a portal user without contact: [ContactId]

 

Test Class:

 

@isTest
public class TestClassPasswordReset {
	static testMethod void passwordresettest(){
        list<User> me = new list<User>([Select languagelocalekey, localesidkey, TimezoneSidKey from User where id = '005E0000001Z5kJ']);
        Contact c = new Contact();
        User u = new User();
        u.ContactId = c.id;
        u.ProfileId = '00eE0000000ZkiK';
        u.username = 'test@test.com';
        u.FirstName = 'First';
        c.LastName = 'Last';
        u.LastName = c.LastName;
        u.email = 'test@test.com';
        String alias = u.username;
        if(alias.length() > 5){
            alias = alias.substring(0,5);
        }
        u.alias = alias;
        u.languagelocalekey = me[0].languagelocalekey;
        u.localesidkey = me[0].localesidkey;
        u.emailencodingkey = 'UTF-8';
        u.TimezoneSidKey = me[0].TimezoneSidKey;
        insert c;
        insert u;
        }
}

 Any help greatly appreciated.

 

Many thanks,

Lee

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Laxman RaoLaxman Rao

Salesforce categorizes objects into so called setup and non-setup objects. User is a setup object while Account is a non-setup object. Salesforce restricts DML operations so that both kinds of objects can't be manipulated in the same context.

 

You can refer this link

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_dml_non_mix_sobjects.htm?SearchType

 

Try this code :

 

static testMethod void passwordresettest(){
UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
system.debug('portalRole is ' + portalRole);

Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
User portalAccountOwner1 = new User(
UserRoleId = portalRole.Id,
ProfileId = profile1.Id,
Username = System.now().millisecond() + 'test2@test.com',
Alias = 'batman',
Email='bruce.wayne@wayneenterprises.com',
EmailEncodingKey='UTF-8',
Firstname='Bruce',
Lastname='Wayne',
LanguageLocaleKey='en_US',
LocaleSidKey='en_US',
TimeZoneSidKey='America/Chicago'
);
Database.insert(portalAccountOwner1);

//User u1 = [Select ID From User Where Id =: portalAccountOwner1.Id];

System.runAs ( portalAccountOwner1 ) {
//Create account
Account portalAccount1 = new Account(
Name = 'TestAccount',
OwnerId = portalAccountOwner1.Id
);
Database.insert(portalAccount1);

//Create contact
Contact contact1 = new Contact(
FirstName = 'Test',
Lastname = 'McTesty',
AccountId = portalAccount1.Id,
Email = System.now().millisecond() + 'test@test.com'
);
Database.insert(contact1);

//Create user
Profile portalProfile = [SELECT Id FROM Profile Limit 1];
User user1 = new User(
Username = System.now().millisecond() + 'test12345@test.com',
ContactId = contact1.Id,
ProfileId = portalProfile.Id,
Alias = 'test123',
Email = 'test12345@test.com',
EmailEncodingKey = 'UTF-8',
LastName = 'McTesty',
CommunityNickname = 'test12345',
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US'
);
Database.insert(user1);
}
}

 

 

All Answers

Lee_CampbellLee_Campbell

Deploying the methodology used in the code you referred me to resulted in the following error message in the debug log:

 

System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Account, original object: User: []

 

Which was thrown on the line where the Account is inserted. I'm not the most clued up with this kind of error message and, in fact, have no idea what it means...

Laxman RaoLaxman Rao

Salesforce categorizes objects into so called setup and non-setup objects. User is a setup object while Account is a non-setup object. Salesforce restricts DML operations so that both kinds of objects can't be manipulated in the same context.

 

You can refer this link

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_dml_non_mix_sobjects.htm?SearchType

 

Try this code :

 

static testMethod void passwordresettest(){
UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
system.debug('portalRole is ' + portalRole);

Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
User portalAccountOwner1 = new User(
UserRoleId = portalRole.Id,
ProfileId = profile1.Id,
Username = System.now().millisecond() + 'test2@test.com',
Alias = 'batman',
Email='bruce.wayne@wayneenterprises.com',
EmailEncodingKey='UTF-8',
Firstname='Bruce',
Lastname='Wayne',
LanguageLocaleKey='en_US',
LocaleSidKey='en_US',
TimeZoneSidKey='America/Chicago'
);
Database.insert(portalAccountOwner1);

//User u1 = [Select ID From User Where Id =: portalAccountOwner1.Id];

System.runAs ( portalAccountOwner1 ) {
//Create account
Account portalAccount1 = new Account(
Name = 'TestAccount',
OwnerId = portalAccountOwner1.Id
);
Database.insert(portalAccount1);

//Create contact
Contact contact1 = new Contact(
FirstName = 'Test',
Lastname = 'McTesty',
AccountId = portalAccount1.Id,
Email = System.now().millisecond() + 'test@test.com'
);
Database.insert(contact1);

//Create user
Profile portalProfile = [SELECT Id FROM Profile Limit 1];
User user1 = new User(
Username = System.now().millisecond() + 'test12345@test.com',
ContactId = contact1.Id,
ProfileId = portalProfile.Id,
Alias = 'test123',
Email = 'test12345@test.com',
EmailEncodingKey = 'UTF-8',
LastName = 'McTesty',
CommunityNickname = 'test12345',
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US'
);
Database.insert(user1);
}
}

 

 

This was selected as the best answer
netspidernetspider

You need to insert c; before you set contactid = c.id

 

so just move insert c: higher up

netspidernetspider

Ah yes of course mixed dml, yes the other post is correct