Trying to add Users with Customer Community to CaseShare. Getting first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id. I have my assumptions but I need confirmation before I go back to the client. I am adding users that associated to Contacts for the same Account that the User(Contact) is associated to in a Trigger on Insert... The following is the code.
Controller has without sharing set.
List conList = new List(); List contList = new List(); List accList = new List(); List accntList = new List(); List caseList = new List(); Map uList = new Map();
//Getting Account Ids from the triggered Case for(Case c : newObjectList) { accList.add(c.accountId); } // Getting the Accounts that have Record Type DC for(Account a : [SELECT id, RecordType.Name FROM Account WHERE id = :accList AND RecordType.Name = 'DC']) { accntList.add(a.id); } // Processing only if there is any account with DC Record type if(accntList.size() > 0) { // Getting all the contacts that belong to the above accounts for(Contact c : [SELECT id, accountid, User_Id__c from Contact WHERE accountId = :accntList ]) { contList.add(c); uList.put(c.Id,c.User_Id__c); } // Removing the contact id who had created the case for(case c : newObjectList) { for(Integer i = 0; i < contList.size();i++) { if(!contlist[i].id.equals(c.contactid)) { conList.add(contList[i]); } } } // Getting the UserId for the contacts with whom the case has to be shared // only if there is any contact if(conList.size()>0){ // Sharing the case with the contacts of respective accounts for(Case c : newObjectList) { for(Integer i =0; i < conList.size(); i++) { if(c.accountid.equals(conList[i].accountid) && uList.containsKey(conList[i].Id)) { if(uList.get(conList[i].Id) != UserInfo.getUserId() || string.valueOf(c.CreatedBy) != string.valueOf(uList.get(conList[i].Id ))){ CaseShare cs = new CaseShare(); cs.CaseAccessLevel = 'Edit'; cs.CaseId = c.Id; cs.UserOrGroupId = uList.get(conList[i].Id); caseList.add(cs); } } } } if (!caseList.isEmpty()) insert caseList; } }