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
arun kumar 577arun kumar 577 

hi all, I have an urgent requirement update owner id

my requirement:

Owner id should be changed on them when child of custom object are upserted. Owner Id on these should be updated with the owner id of account to which the upserting record belongs.

Please give me the solution it is very urgent to me. I hope you people will give me solution as soon as possible.
Best Answer chosen by arun kumar 577
VamsiVamsi
trigger CaseOwnerChangeTrigger on Case (before insert,before update) 
{ 
  Map AccownerIds = new Map();
  set accID = new set(); 
for(Case cn : Trigger.new) 
{ 
   if(cn.AccountID!=null)
  { 
  accID.add(cn.AccountID); 
  } 
}

 for(Account an : [select id,ownerid from account where id IN:accID]) 
{ 
AccownerIds.put(an.id,an.ownerid); 
} 

for(case cb : Trigger.new)
 { 
if(AccownerIds.containsKey(cb.AccountID)) 
{ 
cb.ownerID = AccownerIds.get(cb.AccountID);
 } 
}
}
reposting with correct format 

Please mark as best answer if the above helps ..!!!
 

All Answers

VamsiVamsi
Hi Arun,

This applies for Look-up relationship 
You can get this done with a Trigger on Child object. Initially collect the parent record owner id (value) with child record id (key) into a map only for upserting records. Iterate over the newely upserted child records by updating the child record owner id to parent record owner id using the map.

Hope this helps 
arun kumar 577arun kumar 577
Thank you vamsi for your time,

But can i expect a code from you if it is possible. And you can take any custom object write the code for this scenario. Please share your code if you know about this.

Thanks again...
VamsiVamsi
Hi, I have written this between Account (parent) and Case (child). Trigger is on child object. whenever an insert or update is made on child object then Account owner will be the case owner trigger CaseOwnerChangeTrigger on Case (before insert,before update) { Map AccownerIds = new Map(); set accID = new set(); for(Case cn : Trigger.new) { if(cn.AccountID!=null) { accID.add(cn.AccountID); } } for(Account an : [select id,ownerid from account where id IN:accID]) { AccownerIds.put(an.id,an.ownerid); } for(case cb : Trigger.new) { if(AccownerIds.containsKey(cb.AccountID)) { cb.ownerID = AccownerIds.get(cb.AccountID); } } } Virus-free. www.avast.com
VamsiVamsi
trigger CaseOwnerChangeTrigger on Case (before insert,before update) 
{ 
  Map AccownerIds = new Map();
  set accID = new set(); 
for(Case cn : Trigger.new) 
{ 
   if(cn.AccountID!=null)
  { 
  accID.add(cn.AccountID); 
  } 
}

 for(Account an : [select id,ownerid from account where id IN:accID]) 
{ 
AccownerIds.put(an.id,an.ownerid); 
} 

for(case cb : Trigger.new)
 { 
if(AccownerIds.containsKey(cb.AccountID)) 
{ 
cb.ownerID = AccownerIds.get(cb.AccountID);
 } 
}
}
reposting with correct format 

Please mark as best answer if the above helps ..!!!
 
This was selected as the best answer
arun kumar 577arun kumar 577
thank you vamsi for your time....
VamsiVamsi
Please mark as best answer if that solves your issue. So that it can be removed from Unanswered queue. Virus-free. www.avast.com