How to Create a Sales Lead Validation Rule Using Application Composer
Lead management users can change the status of a lead to Qualified, regardless of whether the lead customer is a sales account or has a primary product associated with the lead.
However, to enforce compliance with your company's lead management business processes, you want to create business rules to control when to set a lead to a Qualified status.
This example describes how to use Application Composer to create a validation rule that ensures a sell-to address and a primary product exist before users change the lead's status to Qualified.
Create a Validation Rule for Sell-to Address
Here's how to create a rule that validates the existence of a sell-to addresses for a sales lead:
-
Navigate to Application Composer.
-
Select Sales to filter the object search.
-
From the Objects View, select Standard Objects, Sales Lead, Server Scripts.
-
Click the Object Functions tab, and then click the Add a New Object Function icon. The Create Object Function page is displayed.
-
In the Function Name field, enter this name without spaces:
isSellToExists
. -
In the Returns field, select Boolean.
-
In the Function Body area, enter the following:
if(null != CustomerId){ def partySites = newView('Address'); def criteria = partySites.createViewCriteria(); def criteriaRow = criteria.createRow(); criteria.insertRow(criteriaRow); def criteriaItem = criteriaRow.ensureCriteriaItem('PartyId'); criteriaItem.setValue(CustomerId) partySites.appendViewCriteria(criteria) partySites.executeQuery(); while(partySites?.hasNext()) { def partySite = partySites.next(); def partySiteUses = partySite?.getAttribute('PartySiteUse'); while (partySiteUses?.hasNext()) { def partySiteUse = partySiteUses.next(); if ('SELL_TO' == partySiteUse?.getAttribute('SiteUseType')) { return true; } } } } return false;
-
Click Validate.
-
Click Save and Close.
Create an Enforcement Rule for Primary Product
Next, you want to create a rule for enforcing the primary product association for the sales lead.
-
Click the Add a New Object Function icon to go to the Create Object Function page.
-
In the Function Name field, enter the this name without spaces:
isPrimaryProductAssigned
. -
In the Returns field, select Boolean.
-
In the Function Body area enter the following:
if((null != PrimaryInventoryOrgId && null != PrimaryInventoryItemId) || null != PrimaryProductGroupId) return true; else return false;
-
Click Validate.
-
Click Save and Close to return to the Server Scripts Sales Lead page.
Create a Validation Rule for Lead Qualification
Next you want to create a validation rule that checks for the existence of a sell-to address and a primary product when a user sets the Qualified status for a sales lead.
-
Click the Validation Rules tab and from the Object Rules area.
-
Click the Add a new validation rule icon.
-
In the Create Object Validation Rule page navigate to the Rule Name field and enter this rule name without spaces:
qualifyLead
-
In the Rule Definition area, enter the following:
if(isAttributeChanged('StatusCode') && getAttribute('StatusCode') == 'QUALIFIED') return (isPrimaryProductAssigned() && isSellToExists()); else return true;
-
In the Error Message area, enter this message text:
Primary Product and Sell to Address are required for qualifying a Lead.
-
Click Save and Close.
This completes the task of using Application Composer to create validation rules for the primary product and sell-to address fields when qualifying a lead.
Test the Rules
Here's how to validate the rules:
-
Navigate to the Lead Qualification, Edit Lead page.
-
From the Actions menu, select Qualify.
If the Primary Product and Sell to Address fields contain no data, you should receive the following error message text:
Primary Product and Sell to Address are required for qualifying a Lead.