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
chris hamplechris hample 

use trigger to find and update existing lead.

Hi all. I am new to this stuff so please forgive my question if it seems basic. I use a web-to-lead form on multiple pages as a way to gate content (fill out the form... get the download). In SF I have a custom field called Lead Origin which records the title of the web-to-lead form. The trouble I am having is when a user returns and completes a different form in order to access a different download, the field cannot be updated. I asked SF support for insight on how to complete this and they suggested that I would need to use a trigger, but it is not something i am familiar with. Does anyone have any example of this?
Arvind_SinghArvind_Singh
Hello Chris, 

It is quite easy but if you did not worked on trigger then it may little challenging for you but trust me it will be fun.

here is an good simple example from David's blog, based on some Key value you can identify existing record and then update it. 

https://www.sfdc99.com/2013/10/19/example-how-to-write-a-deduping-trigger-for-leads-and-contacts/
trigger FindDupes on Lead (before insert, before update) {
  for (Lead myLead : Trigger.new) {
    if (myLead.Email != null) {
      List<Contact> dupes = [SELECT Id FROM Contact
                               WHERE Email = :myLead.Email];
      if (dupes.size() > 0) {
        myLead.Dupe_Contact__c = dupes[0].Id;
      } else {
        myLead.Dupe_Contact__c = null;
      }                             
    }
  }
}

Let me know if help needed on trigger. Hope it helps.