You need to sign in to do that
Don't have an account?

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.
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.
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
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