• Wei Ding 6
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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.
  • October 28, 2015
  • Like
  • 0