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
cjencjen 

Trigger needed to update Lead Owner

Hi, I need some help creating a trigger to change the Lead Owner to a user from a custom lookup field also on the Lead object. This is for when a new Lead record is created and the Lead Source is a certain value. Thanks!
Best Answer chosen by cjen
MithunPMithunP
Hi cjen,

I have created a sample trigger for your scenario, it's working fine in my dev org. Here User__c is a lookup field on Lead object.
trigger LeadTrig on Lead (after insert){
         list<Lead> LeadList = [select id,user__c,ownerid from Lead where id in: trigger.newmap.keyset()]; 
          for(Lead ld : LeadList){
              if(ld.user__c != null){
                  ld.ownerid = ld.user__c;
              }
          }
          update LeadList ;
}
Best Regards,
Mithun.
 

All Answers

MithunPMithunP
Hi cjen,

I have created a sample trigger for your scenario, it's working fine in my dev org. Here User__c is a lookup field on Lead object.
trigger LeadTrig on Lead (after insert){
         list<Lead> LeadList = [select id,user__c,ownerid from Lead where id in: trigger.newmap.keyset()]; 
          for(Lead ld : LeadList){
              if(ld.user__c != null){
                  ld.ownerid = ld.user__c;
              }
          }
          update LeadList ;
}
Best Regards,
Mithun.
 
This was selected as the best answer
Robert ZentgrafRobert Zentgraf
Hi Cjen,

in my opinion you can create a workflow as an easy solution.

Regards
Robert

(mindforce: http://www.mind-force.de)
cjencjen
hi Robert, i tried a WFR/FU, but i was only able to select either a user or a queue to update the Lead Owner field. What i need to do is update the Lead Owner field with a user populated in a custom field (user lookup) also on the Lead Object. If this can be done with WFR, please let me know what I am missing here! Thanks!!!!
Robert ZentgrafRobert Zentgraf
Hi Cjen,

I thought you have some lead values and to every value-area you can assign one owner. Is this true?

Regards
Robert
(mindforce: http://www.mind-force.de)
cjencjen
No, the lead owner = user lookup custom field, it will be different users, not one.
Thanks, Cheryl
Robert ZentgrafRobert Zentgraf
Hi Cjen,

ok, so you should try the idea with a trigger from Mithun. Lead-Assignment-Rules wouldn't help you in this case.

Regards
Robert
(mindforce: http://www.mind-force.de)
cjencjen
Mithun, thank you very much for the example, it worked great!
Mark McGuireMark McGuire
i'm trying to do something similar where I use a field update to change lead owner from a queue to last modified by...anyone have suggestions?