1

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: []


2
  • Are they both lookup fields or master detail fields? Commented Nov 5, 2014 at 12:41
  • One of this is Lookup --MyAccount and One is Master detail --- Special Commented Nov 6, 2014 at 6:55

1 Answer 1

1

I see a couple of things you are going to want to change. First off, it is a bad practice to include a SOQL query inside of a for loop. Secondly, the way you are handling the error in the exception handler doesn't make sense. The error may not be in the first record the Trigger runs through so it doesn't make sense to always include it there. Finally, you will want to set the RowCause field on the Special__Share record. It is a required field when using Apex Managed Sharing. It may be a good idea to read up on Understanding Sharing as well.

Finally, if adding the RowCause doesn't work, you need to ensure that Special__c isn't the child of a master-detail. Master-detail records inherit sharing from the master. You will also want to check that the Org Wide Defaults are not set to Public Read/Write. This won't allow sharing as well as everyone will already have access.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .