I have a Case Object, P1 and P2. P1 and P2 has an lookup relationship with Case. The moment Case owner changes, original case creator should get the Read access on Case and P1 and P2 object.

In OWD, Case is Private, also P1 and P2 is Private. The moment, new Case owner becomes, he should be able to get Read/Edit access on the P1 and P2 object.

The code to give read permission

public static void giveReadPermission(Map<Id,SObject> newSobjsMap, Map<String,Id> mapRecTypeDevName, Map<Id,SObject> oldSobjsMap){
    Map<Id, Case> newCasesMap = (Map<Id, Case>)newSobjsMap;
    Map<Id, Case> oldCasesMap = (Map<Id, Case>)oldSobjsMap;

    List<CaseShare> shareList = new list<CaseShare>();  
    if(oldCasesMap != null){
        for(Id Ids : oldCasesMap.keyset()){
            Case oldcase = oldCasesMap.get(Ids);
            Case newCase = newCasesMap.get(Ids);

            CaseShare cShare = new CaseShare();
            cShare.caseId = Ids;
            cShare.UserOrGroupId = newCase.CreatedById;         
            cShare.CaseAccessLevel = 'Read';
            cShare.RowCause = 'Manual';
        insert shareList;

This will give read access to Child Object

public static void paymentInfo(Map<Id,SObject> newSobjsMap, Map<Id,SObject> oldSobjsMap, List<SObject> newSobjs){
    Map<Id, Payment__c> newPayMap = (Map<Id, Payment__c>)newSobjsMap;
    Map<Id, Payment__c> oldPayMap = (Map<Id, Payment__c>)oldSobjsMap;
    List<Payment__c> newpayList = (List<Payment__c>) newSobjs;

    Set<Id> caseIdSet = new Set<Id>();
    for(Payment__c pay : newpayList){

    List<Payment__Share> pay1ShareList = new List<Payment__Share>();

    if(oldPayMap != null){

        Map<Id,Case> caseMap = new Map<Id,Case>([SELECT Id, OwnerId FROM Case WHERE Id IN :caseIdSet]);

        for(Id Ids : oldPayMap.keyset()){
            Payment__c oldPay = oldPayMap.get(Ids);
            Payment__c newpay = newPayMap.get(Ids);

            Payment__Share payShare = new Payment__Share();
            payShare.ParentId = newpay.Id;
            payShare.AccessLevel = 'Read';
            payShare.RowCause = 'Owner';
            payShare.UserOrGroupId = newpay.OwnerId;
        insert pay1ShareList;

How we can make the owner of Case to Owner of Child object ? Which parameter do we need to consider ?


You need to update ownerId of the child records with the case ownerId

ChildObj.ownerId = CaseObj.ownerId;

  • +1 what I was typing, but terser.
    – sfdcfox
    Jan 15 '18 at 17:21
  • Could you please show some code ?
    – Kanika
    Jan 15 '18 at 17:21
  • @Kanika Added the code . Jan 15 '18 at 17:22

Since the relationship is lookup and you want to explicitly change the owner , you will need to update the child records i.e Payement__c ownerId field in your trigger .

In the same code you will need to add these lines

for(Payment__c pay : newpayList){
    pay.ownerId = caseMap.get(pay.Case__c).ownerId;

update lstPaymentsToUpdate;//Outside loop
  • Do I need to create a separate method to have a access to the owner ? 3 different functionality should have a 3 separate method..right ?
    – Kanika
    Jan 15 '18 at 17:27
  • Good to have in different methods if you are reusing it lot . Jan 15 '18 at 17:33
  • No, I mean shall I add this logic in paymentInfo() ? Not clear where to put the logic in above method ?
    – Kanika
    Jan 15 '18 at 17:35
  • If I do FATAL_ERROR|System.FinalException: Record is read-only.. Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate and Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)..
    – Kanika
    Jan 16 '18 at 6:42

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.