4

I followed the steps to use runAs but it is still giving me a mixed_dml exception. I need to populate the contactId so the user has to have a portal profile. Here's what I have for the test method thus far and it is failing with Mixed_DML:

static testMethod void shouldUpdateRollupStatusAndUserRecordWithoutMixedDMLException()
{

    //Create portal account owner
    UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
    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);

    //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);

    User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];

    System.runAs ( thisUser ) {     
        //Create user
        Profile portalProfile = [SELECT Id FROM Profile WHERE Name LIKE '%Portal User%' 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);
    }

}
2
  • what line does it fail on? Apr 29 '15 at 21:30
  • This line: Database.insert(portalAccount1); Apr 29 '15 at 21:32
4

This error is because "force.com platofm doesn't allow you to modify Setup and Non Setup objects in the same context. By using System.runAs(), you effectively create a new context and prevent this from happening"

Couple of things that could help us here.

1)I would run system.RunAs before creating Accounts .

2)Use test.starttest and test.stoptest

System.runAs ( thisUser ) {  
//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);
Test.stratTest();
User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];   
    //Create user
    Profile portalProfile = [SELECT Id FROM Profile WHERE Name LIKE '%Portal User%' 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);
    
    Test.stopTest();
}
5
  • You should probably say that the reason for this is because the system doesn't allow you to modify Setup and Non Setup objects in the same context. By using System.runAs(), you effectively create a new context and prevent this happening. Apr 29 '15 at 22:29
  • 2
    @CaspNZ The user who asked this question is aware of system.RunAs .Its just placing system.runAs at right place :) Apr 29 '15 at 22:59
  • 1
    Yes, but it would improve your answer. Not everyone who reads this question will know this and may follow your advice and not really know why it worked. Apr 29 '15 at 23:15
  • @CaspNZ Great Suggestion !Thanks for different perspective Apr 29 '15 at 23:19
  • this doesn't work because portalAccountOwner1.Id doesn't exist until you insert that user as well May 1 '15 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.