Insert a Groovy Script to Prevent Users Creating Duplicate Leads

Use a groovy script to prevent users from creating duplicate leads. You write groovy scripts using Application Composer's expression builder, which appears in many places as you modify existing objects or create new custom ones.

You can create a goovey script on the Sales Lead object in Application Composer to prevent salespeople from creating duplicate leads by checking if the phone number or email for a lead already exists in your sales application.

The following is a sample of code to insert before the Lead update trigger to ensure that salespeople can't create duplicated leads. The script checks against leads created over as period of 30 days but you can modify the script to extend or limit the checks to a period of time of your choice.

def vo = newView('Lead')
def vc = newViewCriteria(vo)
def vcr = vc.createRow()
def vci_1 = vcr.ensureCriteriaItem('LeadId')
vci_1.setOperator('!=')
vci_1.setValue(LeadId)
def vci_2 = vcr.ensureCriteriaItem('LeadAging')
vci_2.setOperator('<')
vci_2.setValue(30)
def vci_3 = vcr.ensureCriteriaItem('PrimaryPhoneNumber')
vci_3.setOperator('=')
vci_3.setValue(PrimaryPhoneNumber)
def vci_4 = vcr.ensureCriteriaItem('ContactEmail')
vci_4.setOperator('=')
vci_4.setValue(ContactEmail)
vc.insertRow(vcr)
vo.appendViewCriteria(vc)
vo.executeQuery()
def count=vo.getEstimatedRowCount()
if (vo.hasNext()&& count>0){
   throw new oracle.jbo.ValidationException('A lead record with the same phone number and email already exists!')
} 

For more information about groovy scripting, see the Oracle Applications Cloud Groovy Scripting Reference.