I have created one Trigger to give access to particular Accounts of my "Special" custom object records,
Here BBB : Is custom object having lookup of both Account and Special E.g. BBBRecord = ( BBBName="Test", Account=lookup of account, Special = lookup of special So, I want to give access to that selected Special record to the selected Account Owner !!
See following Code :
List<Special_Share> lstShare = new List<Special_Share>();
for(BBBB__c objBBB : Trigger.new ) {
if( objBBB.MyAccount__c != NULL ) {
Special__Share objS= new Special__Share();
objS.AccessLevel = 'Edit';
objS.parentID = objBBB.Specification__c; // Set the ID of record being shared.
// Assign user id to grant read write access to this particular Special record.
ID accownerid = [select ownerid from Account where ID=:objBBB.MyAccount__c].OwnerId;
objS.UserOrGroupId = accownerid;
lstShare.add(objS);
}
}
try{
insert lstShare;
}catch(Exception e){
trigger.new[0].MyAccount__c.addError('Error::::::'+e.getMessage());
}
ERROR : :Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []