function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NehashriNehashri 

Share children records with user who have access to Parent record apex sharing (lookup)

Hi all,
Here is my scenario. I need to give access to the users to children records B if they have access to the parent record A. A and B are in lookup relation. and the owner of A is not this user. Its an admin user. Does anyone have any code sample or starting point I can refer to(other than documentation)? I know it will be a trigger but the problem is the child record won't even exist when they have access to the parent record. The child records are created later,So  I can give access to child record on creation of child records but I am trying to figure out what happens when the user looses access to parent record, they shouldnt see child records either. 
Any help is highly appreciated.

Thank you
Saurabh Sood 8Saurabh Sood 8
Hi Neha,
You can assign parent  owner ship to child record  also . when it change it automatically change accessiblility in child record also 
 
Lynda Elbay 6Lynda Elbay 6
Hello, 
a solution it's to create a trigger after insert on the child and copy/past parents objet share to child objet share with a criteria : 

 list<ParentObjetShare> listParentShare = [select id, UserOrGroupId, ParentId
                                                   From ParentObjetShare where XXXXXX];
list<ChildObjetShare> listChildShare = new ChildObjetShare();
For (ParentObjetShare PS: listParentShare){
 
   If (YYYY == ZZZZ) {
      ChildShare CS;
     CS.ParentId = PS.ParentID;
      CS.UserOrGroupId = PS.UserOrGroupId;
       CS.RowCause = PS.RowCause;
       CS.AccessLevel = PS.AccessLevel ;
       listChildShare .add(CS);
   }
}

Insert listChildShare;