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


Hello Coders,

Need your input on this below mentioned class. Below trigger written on Junction object having Account and contact look up. Requirement is when ever record is created I need to look for the account field and copy all the sharing in Account Record and need to replicate the same in the contact record. 

I am not getting any error when saving the record. During RUN time Contact Share is not getting Inserted. I am getting following error

|System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [] 

Though I did some reserch I am not able to fix this.  Any leads on this is Much Appreciated

trigger cloneAccountShareAccess on Account_Contact__c (after insert) {

// Checking if this operation is After Insert

List<Id> lstAccountName = new List<Id>();
list<ContactShare> lstContactshare = new list<ContactShare>();

     for(Account_Contact__c lstAccContact:
    system.debug('Salesforce: The Number of Account IDs we received :' +lstAccountName);   
     List<AccountShare> lstAccountShr = new list<AccountShare>();
           lstAccountShr = [Select AccountId ,UserOrGroupId,AccountAccessLevel from Accountshare where
                                       AccountId =:lstAccountName];
    system.debug('Salesforce: We Received list' + lstAccountShr);                                   
   // ****************** Received the list of Account Share details******************************
   //******************* Need to provide the same access to the contact Records*******************
  try {   
         lstContactshare= new list<ContactShare>();                                                                                                                                        
         if(lstAccountShr!=null && lstAccountShr.size()>0)
           for(AccountShare accShare:lstAccountShr)
            for(Account_Contact__c acc :Trigger.New)
            ContactShare a= new ContactShare();
            a.ContactId =acc.contact__c;                 
            a.UserOrGroupId =accShare.UserOrGroupId  ;
            a.ContactAccessLevel =accShare.AccountAccessLevel ; 
             system.debug('Venkatesh: Record added in the share object:' + lstContactshare);
            if (lstContactshare.size()>0)
            system.debug('Salesforce: Record added in the share object:' + lstContactshare.size());//Logs working fine till here
              Insert lstContactshare; // Error on this Line during run time
              system.debug('Salesforce: We are in True Condition');
    catch(DMLexception e)


Shashikant SharmaShashikant Sharma
Try one thing give view all data rights on Account_Contact and Account object for the user profile you are testing with.
make sure your profile has permissions for contact__c field...