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
Krishna Sahu 1Krishna Sahu 1 

I don't know what is wrong in this can some help me on this I am getting this error 'INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY'

public static void careTeamInsertO(map<Id,CareTeam__c> newCareTeamList){
        system.debug('********************careTeamInsert********************');
        Map<Id, List<CareTeam__c>> mapMemberIdtoCareTeam =new Map<Id, List<CareTeam__c>>();
        Map<Id,Id> mapTerritoryToUser = new Map<Id, Id>();
        Set<Id> mapOfPatient = new Set<Id>();
        Map<Id, Account> mapAccountOfPatient = new Map<Id, Account>();
        List<AccountShare> aShareList = new list<AccountShare>();
        for(CareTeam__c careRec :newCareTeamList.values()){
            system.debug('mapMemberIdtoCareTeam==='+ mapMemberIdtoCareTeam.containskey(careRec.Member__c));
            if(careRec.Member__c != null){
                if(mapMemberIdtoCareTeam.containskey(careRec.Member__c))
                {
                    mapMemberIdtoCareTeam.get(careRec.Member__c).add(careRec);
                    mapOfPatient.add(careRec.Patient__c);
                }
                else
                {
                    mapMemberIdtoCareTeam.put(careRec.Member__c, new List<CareTeam__c>{careRec});
                    system.debug('mapMemberIdtoCareTeamputttttt==='+ mapMemberIdtoCareTeam.containskey(careRec.Member__c));
                    mapOfPatient.add(careRec.Patient__c);
                }
                
            }
        }
        system.debug(' mapMemberIdtoCareTeam.keyset()===' +  mapMemberIdtoCareTeam.keyset());
        
        for(UserTerritory2Association us:[SELECT Id, RoleInTerritory2, Territory2Id, User.name FROM UserTerritory2Association 
                                          WHERE UserId IN : mapMemberIdtoCareTeam.keyset() ]){
                                              
                                              mapTerritoryToUser.put(us.Territory2Id, us.UserId); 
                                              
                                          }
        System.debug('mapTerritoryToUser===='+ mapTerritoryToUser);
        for(UserTerritory2Association us:[SELECT Id, RoleInTerritory2, UserId, 
                                          User.name, user.profile.name, Territory2Id
                                          FROM UserTerritory2Association 
                                          WHERE Territory2Id IN : mapTerritoryToUser.keyset() 
                                          AND (user.profile.name = 'Sales Representative' OR user.profile.name = 'Remote Care Team')
                                         ]){ 
                                             System.debug('mapMemberIdtoCareTeam.get(mapTerritoryToUser.get(us.Territory2Id))===='+ mapMemberIdtoCareTeam.get(mapTerritoryToUser.get(us.Territory2Id)));
                                             for(CareTeam__c care : mapMemberIdtoCareTeam.get(mapTerritoryToUser.get(us.Territory2Id)))
                                             {
                                                 system.debug('us.UserId      '+ us.UserId);
                                                 system.debug('care.Patient__c.      '+ care.Patient__c);
                                                 AccountShare aShare = New AccountShare();
                                                 aShare.UserOrGroupID = us.UserId;
                                                 aShare.AccountID = care.Patient__c;
                                                 aShare.AccountaccessLevel = 'Read';
                                                 aShare.OpportunityAccessLevel = 'Read';
                                                 aShare.RowCause = MANUAL;
                                                 aShareList.add(aShare); 
                                                 
                                             }
                                         }
        system.debug('aShareList=='+ aShareList.size());
        
        insert aShareList;
    }   
Best Answer chosen by Krishna Sahu 1
PriyaPriya (Salesforce Developers) 
Hey Krishna,


Kinldy use TRY-CATCH block to handle the exception. And also check OWD and profile permission for that object. 

Refer this link for guidance on this error :-

1. https://salesforce.stackexchange.com/questions/106212/insufficient-access-on-cross-reference-entity-insufficient-access-rights-on-cro

2. https://help.salesforce.com/s/articleView?id=000337167&type=1 (https://help.salesforce.com/s/articleView?id=000337167&type=1)

3. https://help.salesforce.com/s/articleView?id=000328226&type=1 (https://help.salesforce.com/s/articleView?id=000328226&type=1)
 

Kindly mark it as the best answer if it works for you.

 

Thanks & Regards,

Priya Ranjan