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
Varun Sinha 17Varun Sinha 17 

INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY

I am trying to share the job record with the perent account contact records.I am getting the owner of the record and then user and then contact and then parent account and from there contacts of the parent but while inserting I am getting this error.Can anyone help me?Below is my code
trigger CSS_Service_Order_SharingRule on CSS_Job_Order__c (after insert) {
    List<ID> userList = new List<ID>();
    List<ID> conListId = new List<ID>();
    List<ID> conList = new List<ID>();
    List<contact> coList = new List<contact>();
    List<user>usList = new List<user>();
    List<CSS_Job_Order__share> jobOrderList = new List<CSS_Job_Order__share>();
    List<Contact> contList = new List<Contact>();
    List<Account> accListID = new List<Account>();
    Map<Id,Id> uList = new Map<Id,Id>();
    for(CSS_Job_Order__c jobOrder : trigger.new) {
        userList.add(jobOrder.ownerID);
        System.debug('The owner id is'+userList);
    }
    for(User u:[Select id,contactID from user where id=:userList ]){
        conListId.add(u.ContactId);
         System.debug('The contact owner id is'+conListId);
    }
    for(Contact c:[Select accountID from contact where id=:conListId]){
        conList.add(c.accountID);
         System.debug('The ACCOUNT owner id is'+conList);
    }
    for(Account a:[Select parentid from account where id=:conList]){
        accListID.add(a);
         System.debug('The parent id is'+accListID);
    }
    if(accListID.size()>0){
        for(Contact c:[Select id,name,ownerid from contact where accountid=:accListID]){
            contList.add(c);       
        }
        for(CSS_Job_Order__c c : trigger.new) {
            for(Integer i = 0; i < contList.size();i++) {
                
                if(!contlist[i].ownerid.equals(c.ownerid)) {
                    coList.add(contList[i]);
                }
            }
            
        }
        if(coList.size()>0){
            
            for(User u : [SELECT id, contactid from User WHERE contactid = :coList]) {
                uList.put(u.contactid,u.id);
                
            }
            
            
            for(CSS_Job_Order__c jobOrd : trigger.new) {
                
                for(Integer i =0; i < coList.size(); i++) {
                    if(uList.containsKey(coList[i].Id)){
                        CSS_Job_Order__share cjo = new CSS_Job_Order__share(); 
                        cjo.AccessLevel='Read';
                        cjo.ParentId = jobOrd.id;
                        cjo.UserOrGroupId = uList.get(contList[i].Id);
                        jobOrderList.add(cjo);
                    }
                }
            }
            if (trigger.isInsert && !jobOrderList.isEmpty())
                insert jobOrderList;
        }
    }
}