1

Today I have encountered a wierd problem while sharing the records through Apex. Appreciate if someone can help me understand the root cause or faced the similar issue earlier.

Flow of the Code is : Trigger -> Apex Controller

Objects =>
1. CustomObject__c [ lookup to Account ]
2. AccountTeam__c [ Custom Object having a lookup to Account, It behave like standard AccountTeamMember Object but is a custom defined. This contains all the TeamMembers to a particular Account]

The idea is to share CustomObject__c records (After Insert) with the referred Account.

Below is the code :

TRIGGER

trigger createSharingTrigger on CustomObj__c (after insert) {

  list <CustomObj__c >  recordsList = new list<CustomObj__c > ();
  // Below controller has the logic to share records with Team Members
  sharingRuleController SRObj = new sharingRuleController();

  if (Trigger.isInsert){
    for(CustomObj__c  rec : trigger.new){
    recordsList.add(rec);
    }

    if (!recordsList.isEmpty()) {
       SRObj.createSharingRecords(recordsList);
    }
  }
}

APEX Controller :

public without sharing class sharingRuleController
{
    public void createSharingRecords(List<CustomObject__c> shareThisRecords)
      {
         set<Id> accountIdSet = new set<Id>();
         list<CustomObject__Share> shareObjectList = new list<CustomObject__Share>();

         for (CustomObject__c cObj: shareThisRecords){
              accountIdSet.add(cObj.Account__c);
          }

         list <AccountTeam__c> accTeamMembersList = [select Account__c, TeamMemberID__c, from AccountTeam__c  where Account__c IN :accountIdSet];

        for (CustomObject__c cObj: shareThisRecord){

            for (AccountTeam__c TeamMember : accTeamMembersList){

                if (TeamMember.Account__c == shareThisRecord.Account__c){
                    CustomObject__Share cShare = new CustomObject__Share();
                    cShare.ParentId = shareThisRecord.Id; // -> This does not show records in Table, although it has same ID value. The ID is generated for sharing record but cannot query the ID, returns no rows.
                        //GAPShare.ParentId = 'a0p17000000NhM9AAK'; -> This Works
                    cShare.UserorGroupId = TeamMember.TeamMemberID__c;
                    cShare.AccessLevel = 'Read';
                    shareObjectList.add(cShare);
                }
            }   
        }

    try{        
             Database.SaveResult[] shareInsertResult = Database.insert(shareObjectList, false);
             system.debug('GAPShareInsertResult => ' + GAPShareInsertResult); -> GetID has an ID value, and isSuccess is "TRUE". But the record when retrieved using the generated ID returns no rows.
    }catch(Exception e){
       // No exceptions thrown either.
       System.debug("Thrown an exception");
    }
}

Really Appreciate if someone can help me identify the issue here.
The confusion is that the records are being saved and their respective generated IDs are shown in Debug logs (saveResult[]). And also, if the parentID of the sharedRecord is hardcoded, it works !!!

Thanks in Advance, ​Ajaz.

2
  • Have you tried doing insert shareObjectList; without catching it just to see what happens? I have a feeling there actually is an error but the Database.insert doesn't actually cause an error when record insertions fail.
    – dphil
    Oct 28, 2015 at 13:42
  • Ya tried that too.. Didn't work either.
    – Ajaz Mohd
    Oct 28, 2015 at 14:17

1 Answer 1

1

You are using the list shareThisRecord, not the CustomObject__c variable cObj for setting the parentId for the share. You have to change the following code --

for (CustomObject__c cObj: shareThisRecord){

            for (AccountTeam__c TeamMember : accTeamMembersList){

                if (TeamMember.Account__c == shareThisRecord.Account__c){
                    CustomObject__Share cShare = new CustomObject__Share();
                    cShare.ParentId = cObj.Id;   //Your version was --> cShare.ParentId = shareThisRecord.Id;
                    cShare.UserorGroupId = TeamMember.TeamMemberID__c;
                    cShare.AccessLevel = 'Read';
                    shareObjectList.add(cShare);
                }
            }   
        }

Now you can insert the shareObjectList and you can use your workbench to see share records.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.