You need to sign in to do that
Don't have an account?
Ajaz
Records sharing using Apex - [Records are inserted successfully but do not reflect in __share table]
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
APEX Controller :
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.
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.
You need to create "Apex Sharing Reasons" under the CustomObject__c object.
https://developer.salesforce.com/page/Using_Apex_Managed_Sharing_to_Create_Custom_Record_Sharing_Logic
-Thanks
Ashlekh Gera
I am also facing the similar problem with apex sharing. Although I set the Apex Sharing Reason in the object.
The object ServicePartnerRequest should be shared by the users in the same partner community.
Here is the code: The last System.debug output the right result. Insert without exception. But in the share table ServicePartnerRequest__share is the new record not found.