-3

Very basic trigger...2 Picklists, same object (Lead)...Use custom Lead Source Picklist to update standard "Lead Source" picklist, they have exact same values...they should always be in sync


trigger UpdateSource on Lead__c (after update) {

    for(Lead__c a : trigger.new)
    {
       Lead__c Lead = update Lead__c ();
       Lead_Source.Lead__c = Source.Lead__c;

    }

    update Lead;
}
2
  • Your syntax is very confusing and this would not compile. Are you using the standard Lead object or Lead__c? Please provide the API names of the fields and we can help May 11, 2015 at 17:07
  • Thanks Jenny! It is the standard Lead object, the standard Lead field "LeadSource" is the picklist to be updated, and the Picklist value in the custom Lead field "Source__c" should update the Picklist value in "LeadSource" May 11, 2015 at 17:10

3 Answers 3

0

You should write trigger on before update on lead, so that you will not need to update record explicitly.

trigger UpdateSource on Lead (before insert, before update) {
  for(Lead editedLead : trigger.new) {
    editedLead.LeadSrouce = editedLead.Source__c;
  }
}

Once this trigger is created. Update all lead records using data loader. Then this trigger will be fired and all values will be copied.

3
  • This does not require a trigger at all and should be accomplished via workflow.
    – Adrian Larson
    May 11, 2015 at 17:47
  • 1
    I already had that discussion with the OP, went through the various options and even gave him a better option than he originally planned but he went and accepted this answer which is the same code & posted later May 11, 2015 at 17:50
  • You may not be able to achieve it using workflow. Since it is a picklist type field, while updating you cannot use formula expression. Only options that are available are - update to previous value, update to next value, update to specific value. It will not let you to get the value from another field.
    – Victor
    May 11, 2015 at 17:51
1

With corrections on your posted code, so you know for next time:

trigger UpdateSource on Lead (after update) {  // since you're using the standard Lead object, you don't need to say Lead__c
  List<Lead> leadsToUpdate = new List<Lead>(); // create a collection so you only have to use one DML update
  for(Lead currLead : trigger.new) {
    currLead.LeadSource = currLead.Source__c;  // assign the standard LeadSource field the value of Source__c
    leadsToUpdate.add(currLead);               // add this Lead to the update collection
  }
  update leadsToUpdate;                        // one DML statement
}

It would be better to do this on a before update, but since you started with after, I just fixed it and added comments.

3
  • There is no need for an 'after' trigger in this case, you could change it to a 'before' trigger and eliminate the DML statement entirely. May 11, 2015 at 17:28
  • As I said in my answer: "It would be better to do this on a before update, but since you started with after, I just fixed it and added comments." May 11, 2015 at 17:29
  • Fair enough, I missed that. May 11, 2015 at 17:36
0

It sounds like you don't even need triggers for this, Workflow Rules should be more than capable and will be easier to develop & maintain.

Edit: OK, as you don't want to create a workflow rule for each value, then you can use a trigger like so:

trigger UpdateSource on Lead__c (before insert, before update) {

    for (Lead__c a : trigger.new) {

        if (Trigger.isInsert || (Trigger.isUpdate && a.Source__c != trigger.oldMap.get(a.Id).Source__c)) {

            a.LeadSource = a.Source__c;
        }
    }
}

But if the standard Source field (LeadSource) always contains the value of the custom Source field (Source__c), wouldn't a formula field be the simplest option?

4
  • Davin, unfortunately a workflow can't update a Picklist until Summer 15...I'd have to create a separate Workflow field update for EACH picklist value! May 11, 2015 at 17:11
  • OK, updated my answer. May 11, 2015 at 17:17
  • Well it's an odd use case regarding Lead Conversion. Yes formula would be much easier, but for some reason you can't map the standard field Lead Source to Opportunities upon Lead Conversion, only to Accounts/Contacts. So, custom "Source" picklist built on Lead, exact same values as standard field, expose the custom picklist, but update standard picklist behind scenes. When Lead converts, Opp/Contact/Account all get proper Lead Source May 11, 2015 at 17:21
  • OK, well the above could should work but it might be easier to have an Opportunity trigger that on after insert of Opportunity looks for a Lead with a Lead.ConvertedOpportunityId matching it's own ID and simply copy the LeadSource value then. May 11, 2015 at 17:26

Not the answer you're looking for? Browse other questions tagged .