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
Gaurav-GuleriaGaurav-Guleria 

test method for account custom sharing with Partner Portal user

I am trying to write a test method for account custom sharing with Partner Portal user. But I am facing error while setting up u.IsActive = true; and u.IsPortalEnabled = true;  fields in test methods. Error as follows.

 

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

 

I understanding we can’t run DML on user and sharing object together(Set up and non setup objects). Wondered if there is any solution so that we can create partner portal user in a test method beside sele4cting this from the User object.    

 

 

@isTest

private class TestUpdateAccountShare_Trigger{

    public static testMethod void myTest()

    {

        Test.startTest();

    //Account created.

    Account acc = new Account(name='test');

    insert acc;

    //Contact created.   

    Contact cont = new Contact();

    cont.lastname = 'test';

    cont.phone = '555-555-5555';

    cont.Accountid =acc.id;

    insert cont;

    //Selects name and id partner user profile.

    Profile pp = [Select p.Name, p.Id From Profile p where Name LIKE '%Partner%'limit 1];

    //Partner user inserted.

    User u = new User(alias = 'port',

                email='portaluser@testorg.com',

                emailencodingkey='UTF-8',

                lastname='Testing',

                languagelocalekey='en_US',

                localesidkey='en_US',

                profileid = pp.Id,

                contactid = cont.Id,

                timezonesidkey='America/Los_Angeles',

                username='portaluser@testorg.com'

                );

    insert u;

 

    u.IsActive = true;

    u.IsPortalEnabled = true;

    update u;

        Test.stopTest();

 

    //Select partner portal user's id and account id from contacts.

    List<Contact> con = [Select id, Accountid from Contact where id =: u.ContactId];

    System.debug('User record' + con);

    //Account's record created.

    account accobj = new account(name='test');

    System.debug('User record' + accobj);

    insert accobj;

 

    }

}

kyle.tkyle.t

I believe if you create the user in a system.runas statement block you can circumevent this.

 

Not sure the best approach for this, but maybe select a random user that matches a profile that can create and account and contact then create the data you need that way.

 

User u1 = [select id from user where Profile = <something that can create accounts and contacts> limit 1];

Account acc = new Account(name='test');

Contact cont = new Contact();

System.runas(u1){

   insert acc;

   cont.lastname = 'test';

    cont.phone = '555-555-5555';

    cont.Accountid =acc.id;

    insert cont;

}

 

....

 

Hope that helps