0

We have an existing code in our application where whenever we create an account,we add certain team member and provide them Read/Write access for Account. Below is the apex ocde:

private void UpdateAccountTeam(Map<Id, SObject> newItems, Map<Id, SObject> oldItems) {

    List<AccountTeamMember> listOfAccountTeam = new List<AccountTeamMember>();
    List<AccountTeamMember> listOfDeleteAccTM = new List<AccountTeamMember>();

        List<AccountTeamMember> lisOfAccountTM = new List<AccountTeamMember>([Select TeamMemberRole, Id, userId, AccountId from AccountTeamMember where AccountId in:newItems.keySet()]);
        Map<String, AccountTeamMember> mapOfAccountTM = new Map<String, AccountTeamMember>();

        for(AccountTeamMember accTM:lisOfAccountTM) {
            String strKey = accTM.TeamMemberRole + accTM.AccountId;
            mapOfAccountTM.put(strKey, accTM);
        }            

        for(SObject newObj :newItems.values()) {
            Account newAccountObj = (Account)newObj;
            Account oldAccountObj = Null;

            if(oldItems!=Null) {
                oldAccountObj = (Account)oldItems.get(newAccountObj.Id);
            }

            AccountTeamMember accTeamMember = Null;

            if(newAccountObj.SF_Director__c!=null && (oldAccountObj==Null || oldAccountObj.SF_Director__c!=newAccountObj.SF_Director__c)) {
                accTeamMember = getAccountTeamMember(newAccountObj);
                accTeamMember.TeamMemberRole = 'Salesforce Director';
                accTeamMember.userId = newAccountObj.SF_Director__c;

                String strMapKey = accTeamMember.TeamMemberRole + newAccountObj.Id;
                if(mapOfAccountTM.get(strMapKey)!=null) {
                    listOfDeleteAccTM.add(new AccountTeamMember(Id=mapOfAccountTM.get(strMapKey).id));
                }

                listOfAccountTeam.add(accTeamMember);
            }

            if(Trigger.IsInsert) {
                accTeamMember = getAccountTeamMember(newAccountObj);

                accTeamMember.TeamMemberRole = 'Account Manager';
                accTeamMember.userId = newAccountObj.CreatedById;
                listOfAccountTeam.add(accTeamMember);
            }
        }

        delete listOfDeleteAccTM;

        insert listOfAccountTeam;


}


private AccountTeamMember getAccountTeamMember(Account newAccountObj) {
    AccountTeamMember accTeamMember = new AccountTeamMember();

    accTeamMember.AccountId = newAccountObj.Id;
    accTeamMember.AccountAccessLevel = 'Edit';
    accTeamMember.OpportunityAccessLevel = 'None';
    accTeamMember.ContactAccessLevel = 'None';

    return accTeamMember;
}

Strange thing is it was working earlier Before Summer 17.Our envt has been upgraded to Winter 18 where it is not working.

Is there any bug in Winter 18 for accesslevel?

Can someone suggest what needs to be updated in the code?

Solution that I have tried:

As per the below link I have modified the code:

https://help.salesforce.com/articleView?id=000001097&type=1

When I insert AccTeamMember then I have added a for loop and try to insert the sharing rules.

insert listOfAccountTeam;

            for(AccountTeamMember actm:listOfAccountTeam){
                system.debug('Inside ACTM For Loop');
            sharingRules.add(new AccountShare(AccountAccessLevel = accountAccessLevel, OpportunityAccessLevel = opportunityAccessLevel, ContactAccessLevel=contactAccessLevel,CaseAccessLevel = caseAccessLevel, UserOrGroupId = actm.UserId , AccountId = actm.Accountid));    
            }
            system.debug('Sharing Rulz:'+sharingRules);
            insert sharingRules;

When I checked the email logs,it throws below error message when I am trying to insert sharing rules:

16:28:47:872 EXCEPTION_THROWN [399]|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_ACCESS_LEVEL, (ACC_SHARE.ACC_ACCESS_LEVEL, ACC_SHARE.OPP_ACCESS_LEVEL, ACC_SHARE.CASE_ACCESS_LEVEL, ACC_SHARE.CON_ACCESS_LEVEL) (Account, Opportunity, Case Levels, Con Levels (Edit, None, None, None) are below organization levels (Read, None, None, Read)): []

0

After doing analysis I have found that in our org Org Wide Setting for contact has been changed from Private to Public Read only and In my existing code,we were trying to set contact access as Private.

I can upgrade Contact Access from Public Read only to Read Only or Read/Write but I can not downgrade it.This is because it did not give me the access that I defined in code.

Modified Method:

private AccountTeamMember getAccountTeamMember(Account newAccountObj) {
    AccountTeamMember accTeamMember = new AccountTeamMember();

    accTeamMember.AccountId = newAccountObj.Id;
    accTeamMember.AccountAccessLevel = 'Edit';
    accTeamMember.OpportunityAccessLevel = 'None';
    accTeamMember.ContactAccessLevel = 'Read';//Change Contact access from None to Read based on Org Setting

    return accTeamMember;
}

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.