Wednesday, June 26, 2013

Salesforce: Web-to-Lead validation

In previous blog, we discuss about adding record type as hidden field in Web-to-Lead, so when Lead created in Salesforce, it automatically assigned with a specific record type and Assignment Rules, it will be assigned to specific queue or user.

This blog will discuss about validation in Web-to-Lead. Can we have validation rule on data submit by user?
Out of the box, Salesforce do not provide any validation. For example: when user submit with invalid email address, it will be rejected by Salesforce by default. So, where the lead go? I'll be not created in Salesforce.

Salesforce will email to Default Lead Creator with subject Salesforce Could Not Create This Lead. And in the email body, it will capture all data enter by user with reason why it failed, for this case, it would be InvalidEmailValue

The same if you have validation rule in Lead, Default Lead Creator will get the same email with the reason as error message set in validation rule.

But, please note if you have required custom field, Salesforce will skip this one and create the Lead, so next time if you have Lead without required field, it may be created from Web-to-Lead.

So, back to the original question, can we add validation in Web-to-Lead form? Yes, you can, you need to learn a simple javascript. Sample:

<script type='text/javascript'>
    function ValidateForm() {
        var helperMsg = '';

        if (document.getElementById('first_name').value.length == 0) {
            helperMsg += "Please enter First Name \n\r";
        if (document.getElementById('last_name').value.length == 0) {
            helperMsg += "Please enter Last Name \n\r";
        if (document.getElementById('email').value.length == 0) {
            helperMsg += "Please enter valid Email Address \n\r";
        if (document.getElementById('phone').value.length == 0) {
            helperMsg += "Please enter Phone \n\r";
         if (helperMsg.length > 0) {
            return false;
        return true;