0

I'm making some changes to a security model to rely on standard Salesforce security. When I remove a Share Set that grants edit access to an Portal User's Cases because that's redundant, and we need the Case Share Set Rule for something else, several Unit Tests fail because of the order in which the records are created.

For some reason, if the test creates the Contact, then the User, then the Case, the Case is shared as expected. However, if the test creates the Contact, then the Case, then the User, the Case is not shared as expected.

I manually did this through the UI, and the Case was shared as expected in either scenario. Is there a reason why the order would matter? This seems like a bug...

I've included a generic set of test methods to better illustrate what is happening.

@isTest
static void CaseIsCreatedAfterUserIsCreated_IsShared(){
    Contact portalContact = getContact();
    insert portalContact;
    User portalUser = getPortalUser(portalContact);
    insert portalUser;
    Case portalUsersCase = getCase(portalContact);
    Test.startTest();
    insert portalUsersCase;
    Test.stopTest();
    System.assertEquals(1, [select Count() from CaseShare where CaseId = :portalUsersCase.Id and UserOrGroupId = :portalUser.Id 
                            and RowCause =  'RelatedPortalUser' and CaseAccessLevel = 'Edit']); // Passes
}
@isTest
static void CaseIsCreatedBeforeUserIsCreated_ShouldAlsoBeShared(){
    Contact portalContact = getContact();
    insert portalContact;
    Case portalUsersCase = getCase(portalContact);
    insert portalUsersCase;
    User portalUser = getPortalUser(portalContact);
    Test.startTest();
    insert portalUser;
    Test.stopTest();
    System.assertEquals(1, [select Count() from CaseShare where CaseId = :portalUsersCase.Id and UserOrGroupId = :portalUser.Id 
                            and RowCause =  'RelatedPortalUser' and CaseAccessLevel = 'Edit']); // Fails
}

Your Answer

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

Browse other questions tagged or ask your own question.