0

Our contact OWD is Private

When we convert an existing contact as a customer community plus user, he gets read only access to his own contact with the sharing reason 'Associated Portal User or Role '(created by salesforce behind the scenes). We found this by clicking share button for the contact.

When we run the below code in test class and try to check for contactshare records we dont see any records(the line before stop test.)

@istest
    public static void testmethod1(){
        test.startTest();
        Set<String> customerUserTypes = new Set<String> {'PowerCustomerSuccess'};
            Account acc = new Account (Name = 'newAcc1',recordtypeid = ISD_sObjectUtils.getRecordTypeIdByDeveloperName(Account.sObjectType, 'Business_Account'));  
        insert acc;
        Contact con = new Contact (
            AccountId = acc.id,
            LastName = 'portalTestUser',
            recordtypeid = ISD_sObjectUtils.getRecordTypeIdByDeveloperName(Contact.sObjectType, 'Business_User')
        );
        insert con;
        Profile p = [select Id,name from Profile where UserType in :customerUserTypes limit 1];
        User newUser = new User(
            profileId = p.id,
            username = '[email protected]',
            email = '[email protected]',
            emailencodingkey = 'UTF-8',
            localesidkey = 'en_US',
            languagelocalekey = 'en_US',
            timezonesidkey = 'America/Los_Angeles',
            alias='nuser',
            lastname='lastname',
            contactId = con.id
        );
        insert newUser;
        system.debug('newUser' + newUser);
        List<ContactShare> oTemp = [Select Id,ContactAccessLevel, UserOrGroupId from ContactShare where ContactId =:con.Id ];
        system.debug('oTemp-->'+oTemp); 
        test.stopTest();
    }

Questions:

  1. Are we missing anything in our test class code which is causing this behavior?

2.Is this the default behavior in test classes.

1 Answer 1

0

I would suggest as follows

Insert the user, and runas the particular User, and then you should see at least one record with sharing reason as 'Owner', with the Owner being the newUser.

@istest
public static void testmethod1(){
    test.startTest();
    User newUser = new User(
            profileId = p.id,
            username = '[email protected]',
            email = '[email protected]',
            emailencodingkey = 'UTF-8',
            localesidkey = 'en_US',
            languagelocalekey = 'en_US',
            timezonesidkey = 'America/Los_Angeles',
            alias='nuser',
            lastname='lastname',
            contactId = con.id
        );
    insert newUser;
    System.runAs(newUser){
        Set<String> customerUserTypes = new Set<String> {'PowerCustomerSuccess'};
            Account acc = new Account (Name = 'newAcc1',recordtypeid = ISD_sObjectUtils.getRecordTypeIdByDeveloperName(Account.sObjectType, 'Business_Account'));  
        insert acc;
        Contact con = new Contact (
            AccountId = acc.id,
            LastName = 'portalTestUser',
            recordtypeid = ISD_sObjectUtils.getRecordTypeIdByDeveloperName(Contact.sObjectType, 'Business_User')
        );
        insert con;
        Profile p = [select Id,name from Profile where UserType in :customerUserTypes limit 1];
        List<ContactShare> oTemp = [Select Id,ContactAccessLevel, UserOrGroupId from ContactShare where ContactId =:con.Id ];
        system.debug('oTemp-->'+oTemp); 
    }
    test.stopTest();
}

You must log in to answer this question.

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