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
SFDC@ErrorSFDC@Error 

based on look up field value how to change record owner.

Hi All
I have 3 custom object ob1__c,ob2__c,Ob3__c.

ob1__c and ob2__c has master-detail relationship,so that ob2__c record owner is ob1__c record owner(ob2__c does not have any record owner because in matser-detail child does not have record owner).

ob3__c and ob2__c has lookup relatioship.but my requirement is wneh i am going to create a record on ob3__c and select ob2__c record uisng look up dialog.after record save need to update record owner of ob3__c based on ob2__c selected record owner.how it is possible.
 
Purushotham YellankiPurushotham Yellanki
Hi SFDC,

I would approach by writing an after Insert Trigger on your Obj3__c and then check if Obj2__c is not null on your Obj3__c record and if it is true then get owner Id from Obj1__c to populate it on Obj3__c record. I have a below sample Code craeted in my Org using your Object Model, please try this and see if it helps!

trigger Obj3_Trigger on Obj3__c (after insert) {
    Set<Id> obj3Id = new Set<Id>();
    for (Obj3__c iter: Trigger.new) {
        if (iter.Obj2__c != null) {
            obj3Id.add(iter.Id);
        }
    }
    
    public static void updtOwnrId(Set<Id> setIds) {
        List<Obj3__c> lstObj3 = new List<Obj3__c>();
        for (Obj3__c iter:[Select Id, OwnerId, Obj2__r.Obj1__r.OwnerId From Obj3__c Where Id IN:setIds]) {
            iter.OwnerId = iter.Obj2__r.Obj1__r.OwnerId;
            lstObj3.add(iter);
        }
        update lstObj3;
    }
}




Thank you
SFDC@ErrorSFDC@Error
Hay @Yellanki I tried the above code ,but it is not working trigger Obj3_Trigger on Invoice_Dispatch_details__c(after insert) { Set obj3Id = new Set(); for (Invoice_Dispatch_details__c iter: Trigger.new) { if (iter.Purchase_Order__c!= null) { obj3Id.add(iter.Id); } } public static void updtOwnrId(Set setIds) { List lstObj3 = new List(); for (Obj3__c iter:[Select Id, OwnerId, Invoice_Dispatch_details__r__r.Sale_Confirmation_Orders__r.OwnerId From Invoice_Dispatch_details__c Where Id IN:setIds]) { iter.OwnerId = iter.Invoice_Dispatch_details__r.Sale_Confirmation_Orders__r.OwnerId; lstObj3.add(iter); } update lstObj3; } }
Purushotham YellankiPurushotham Yellanki
Ok that was a stupid mistake that we did. We never invoked our method to run the logic. Actually its a best practice to move all your Trigger code to some handler class and that way you will have more control for futher code changes. Here you will need a Trigger and a Class which are as below and I just tried in my Org and they work fine, please try this and see if this works!

Trigger:
trigger Obj3_Trigger on Obj3__c (after insert) {   
    if (Trigger.isAfter) {
        if (Trigger.isInsert) {
            Obj3_TriggerHandler.afterInsert(Trigger.new);
        }
    }  
}

Class:
public class Obj3_TriggerHandler {
    public static void afterInsert(List<Obj3__c> lstObj3) {
        Set<Id> obj3Id = new Set<Id>();
        for (Obj3__c iter: lstObj3) {
            if (iter.Obj2__c != null) {
                obj3Id.add(iter.Id);
            }            
        }
        if (obj3Id.size()>0) {
            updtOwnrId(obj3Id);
        }
    }
    public static void updtOwnrId(Set<Id> setIds) {
        List<Obj3__c> lstObj3 = new List<Obj3__c>();
        for (Obj3__c iter:[Select Id, OwnerId, Obj2__r.Obj1__r.OwnerId From Obj3__c Where Id IN:setIds]) {
            iter.OwnerId = iter.Obj2__r.Obj1__r.OwnerId;
            lstObj3.add(iter);
        }
        update lstObj3;
    }
}




Thank you