+ Start a Discussion
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Access user id field on parent record for apex sharing-Please Help!!!

Hi Everyone,
           I am running into a small issue here.  Need help just with 1 line of syntax. So I have an object called Project Form , which is looking up to its parent called costpoint project . There is a user field on costpoint record (project team member 1) . I want an apex sharing record (on project form) to get created whenever a project form is added to a particular costpoint project .  All is working well, just the syntax to pass on the Id of the user from parent record seems to go awry . Please help me with the code given below, I have underscored the lines .


 for(Project_Form__c ProjectForm : trigger.new){
            // Instantiate the sharing objects
            teamMemberShr = new Project_Form__Share();
            
            
            // Set the ID of record being shared
            teamMemberShr.ParentId = ProjectForm.Id;
            
            
            // Set the ID of user or group being granted access
            //teamMemberShr.UserOrGroupId = ProjectForm.Costpoint_Project__r.Project_Team_Member_1__c;
            system.debug('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: ' + ProjectForm.Costpoint_Project__r.Project_Team_Member_1__c);
         
            teamMemberShr.UserOrGroupId = '005G0000001to4iIAA';
            
            // Set the access level
            teamMemberShr.AccessLevel = 'read';
            
            
            // Set the Apex sharing reason for hiring manager and recruiter
            teamMemberShr.RowCause = Schema.Project_Form__Share.RowCause.Project_Team_Member__c;
            
            
            // Add objects to list for insert
            ProjectFormShrs.add(teamMemberShr);
            
        }
Best Answer chosen by shrey.tyagi88@tcs.com
Bryan JamesBryan James
 List<Project_Form__Share> ProjectFormShrs = new List<Project_Form__Share>();
    List<Project_Form__c> pfList = trigger.new;
    Set<Id> resultIds = (new Map<Id,SObject>(pfList)).keySet();
    Map<Id,Project_Form__c> pfMap = new Map<Id,Project_Form__c>([Select Id, Costpoint_Project__r.Project_Team_Member_1__c From Project_Form__c Where Id in :resultIds]);
    for(Project_Form__c ProjectForm : pfList){
            // Instantiate the sharing objects
            Project_Form__Share teamMemberShr = new Project_Form__Share();
            
            
            // Set the ID of record being shared
            teamMemberShr.ParentId = ProjectForm.Id;
            teamMemberShr.UserOrGroupId = ProjectForm.Costpoint_Project__r.Project_Team_Member_1__c;
            
            // Set the ID of user or group being granted access
            //teamMemberShr.UserOrGroupId = ProjectForm.Costpoint_Project__r.Project_Team_Member_1__c;
            system.debug('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: ' + teamMemberShr.UserOrGroupId);
         
            //eamMemberShr.UserOrGroupId = '005G0000001to4iIAA';
            
            // Set the access level
            teamMemberShr.AccessLevel = 'read';
            
            
            // Set the Apex sharing reason for hiring manager and recruiter
            teamMemberShr.RowCause = Schema.Project_Form__Share.RowCause.Project_Team_Member_1__c;
            
            
            // Add objects to list for insert
            ProjectFormShrs.add(teamMemberShr);
            
        }
    insert ProjectFormShrs;

Give that a try. Hope this solves your problem. Good Luck!

All Answers

Bryan JamesBryan James
So when using a trigger you can only go up 1 parent level I believe, not to a grandparent like you are attempting in this case. So the trick is we have to find a way to run some queries on the trigger using the ids returned from creating the records.
So what I did was instead of using the trigger.new in the for loop I created a list of the records like-a-so:
List<Project_Form__c> pfList = trigger.new;

I then cast that list to a set just like-a-so:
Set<Id> resultIds = (new Map<Id,SObject>(pfList)).keySet();

From here you can either create a new list using the id's in that set as your where in clause such as like-a-so:
List<Project_Form__c> pfList2 ([Select Id, Costpoint_Project__r.Project_Team_Member_1__c From Project_Form__c Where Id in :resultIds])
that way when you loop over your second list you will be able to get access to the Costpoint_Project__r.Project_Team_Member_1 field.

Otherwise you can query into a map like-a-so:
Map<Id,Project_Form__c> pfMap = new Map<Id,Project_Form__c>([Select Id, Costpoint_Project__r.Project_Team_Member_1__c From Project_Form__c Where Id in :resultIds]);

Then use the trigger list we first created in your for loop
for(Project_Form__c ProjectForm : pfList){
         then that way if you have more fields you need access too later you wont have to keep updating the above query and you can access them all ... like-a-so:
String userOrGroupId = pfMap.get(ProjectForm.Id).Costpoint_Project__r.Project_Team_Member_1__c;
}


Hope this helps.
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com
Hey Bryan,
          Thanks a lot for your help buddy !!! From your response , I got the reson why the code is not working . But somehow I am bit confused how to implement your solution in my code. If it is not too much of a trouble for you , can you please make changes in my code and give me the alternate approaches. If you can help me with that , that would be really great.

I really appreciate your help and support. Thanks for your time!!!

Cheers :)
Shrey
Bryan JamesBryan James
 List<Project_Form__Share> ProjectFormShrs = new List<Project_Form__Share>();
    List<Project_Form__c> pfList = trigger.new;
    Set<Id> resultIds = (new Map<Id,SObject>(pfList)).keySet();
    Map<Id,Project_Form__c> pfMap = new Map<Id,Project_Form__c>([Select Id, Costpoint_Project__r.Project_Team_Member_1__c From Project_Form__c Where Id in :resultIds]);
    for(Project_Form__c ProjectForm : pfList){
            // Instantiate the sharing objects
            Project_Form__Share teamMemberShr = new Project_Form__Share();
            
            
            // Set the ID of record being shared
            teamMemberShr.ParentId = ProjectForm.Id;
            teamMemberShr.UserOrGroupId = ProjectForm.Costpoint_Project__r.Project_Team_Member_1__c;
            
            // Set the ID of user or group being granted access
            //teamMemberShr.UserOrGroupId = ProjectForm.Costpoint_Project__r.Project_Team_Member_1__c;
            system.debug('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: ' + teamMemberShr.UserOrGroupId);
         
            //eamMemberShr.UserOrGroupId = '005G0000001to4iIAA';
            
            // Set the access level
            teamMemberShr.AccessLevel = 'read';
            
            
            // Set the Apex sharing reason for hiring manager and recruiter
            teamMemberShr.RowCause = Schema.Project_Form__Share.RowCause.Project_Team_Member_1__c;
            
            
            // Add objects to list for insert
            ProjectFormShrs.add(teamMemberShr);
            
        }
    insert ProjectFormShrs;

Give that a try. Hope this solves your problem. Good Luck!
This was selected as the best answer
Bryan JamesBryan James
Hey Shrey,
If this ended up helping you out would you mind selecting it as best answer?