More

    How to use Lookup field in Web To Lead form in Salesforce

    Salesforce’s Web-to-Lead is a great tool to generate leads via HTML forms embedded in any website or an app. A Web-to-Lead form is essentially an HTML form that includes our choice of Lead’s fields that we want the user to fill. We can use multiple types of fields in the form in question, However, Salesforce restricts us from using a Lookup field in the Web-to-Lead form. But rest assured, there is a workaround to solve this issue.

    Also Read: How to start Process Builder after one minute in Salesforce

    In order to populate a Lookup field via the Web-to-Lead form, you have to follow the steps mentioned below.

    Create a custom Text field

    Let’s say our Lookup field on a Lead object is called “Lead Source” which references to an Account record. For this, we would create a custom text field called “Lead Source Text”. This field will only be used in the backend, so no need to display it on the layout.

    Add custom Text field in Web-to-Lead form

    Now, instead of trying to add the original Lookup field in Web-to-Lead form, add our custom Text field instead. Basically, we would take the ID of the record in String form using this custom Text field.

    Write a Trigger on Lead object

    In case you haven’t already written a Trigger on the Lead object, you are required to use the Trigger to make this work. Create a new Apex Trigger called “Lead Trigger” and a new class called “LeadTriggerHandler”. Follow the Trigger Handler Pattern for best practices, which is to handle all the logic in the handler and just use the Trigger to call the Handler methods.

    In your Trigger Handler, add the following function:

    
    private void convertTextToLeadSource()  // Name as per your logic
    {
        for(Lead l : (List<Lead>) Trigger.new)
        {
           if(Trigger.isInsert && l.Lead_Source_Text__c != null)
           {
    
                // Perform Validation here
                l.Lead_Source__c = l.Lead_Source_Text__c;
           }
        }
    }

    This method checks if a lead is inserted and the Lead Source Text field is filled, then insert the value of the custom text field – that should be a valid ID – to Lead Source (Lookup field). Make sure you perform the required validations in order to check that the input String coming from the Web-to-Lead form represents a valid ID and that ID belongs to an existing record or not.

    Write Test Class

    Lastly, write the Test Class for your Trigger Handler in order to unit test your functionality and complete the code coverage for deployment purposes.

    By following these steps, you can easily populate a Lookup field through the Web-to-Lead form. For more guides, keep following Retrology.

    Talha Saqib
    Talha Saqib
    Founder of Retrology. Been actively following the gaming and media industry for the past 15 years. Also, a full-time software engineer. My day-to-day tasks include writing, editing, strategizing content, managing my team, and handling the complete back-end.

    2 COMMENTS

    Subscribe
    Notify of
    guest
    2 Comments
    Oldest
    Newest Most Voted
    Inline Feedbacks
    View all comments