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:

  1. Navigate to Application Composer.

  2. Select Sales to filter the object search.

  3. From the Objects View, select Standard Objects, Sales Lead, Server Scripts.

  4. Click the Object Functions tab, and then click the Add a New Object Function icon. The Create Object Function page is displayed.

  5. In the Function Name field, enter this name without spaces: isSellToExists.

  6. In the Returns field, select Boolean.

  7. 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;

  8. Click Validate.

  9. 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.

  1. Click the Add a New Object Function icon to go to the Create Object Function page.

  2. In the Function Name field, enter the this name without spaces: isPrimaryProductAssigned.

  3. In the Returns field, select Boolean.

  4. In the Function Body area enter the following: if((null != PrimaryInventoryOrgId && null != PrimaryInventoryItemId) || null != PrimaryProductGroupId) return true; else return false;

  5. Click Validate.

  6. 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.

  1. Click the Validation Rules tab and from the Object Rules area.

  2. Click the Add a new validation rule icon.

  3. In the Create Object Validation Rule page navigate to the Rule Name field and enter this rule name without spaces: qualifyLead

  4. In the Rule Definition area, enter the following: if(isAttributeChanged('StatusCode') && getAttribute('StatusCode') == 'QUALIFIED') return (isPrimaryProductAssigned() && isSellToExists()); else return true;

  5. In the Error Message area, enter this message text: Primary Product and Sell to Address are required for qualifying a Lead.

  6. 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:

  1. Navigate to the Lead Qualification, Edit Lead page.

  2. 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.