function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gateway Bikes Group4Gateway Bikes Group4 

Trigger on Lead

Use Case
Trigger on the Lead object that populates a custom field (API Name: Key_Fields_Populated__c) number field whenever the Lead is created.
“Key Fields Populated” should count the total number of the following fields that are not null:
FirstName
LastName
Email
Phone
Website
Title
#1 example, if FirstName, LastName, and Email are populated, “Key Fields Populated” should equal to 3.
If at least 3 key fields are populated, create a new Task for each key field populated:
The name of each Task should be: Verify the <<key field name>> field

 #2  if any of the key fields contain the word “test” (in any combination of upper or lowercase letters), always:
Create a new Task with:
Subject: “WARNING”
Description: “This Lead contains the TEST keyword in the following key fields: <<comma separated list of only the key fields containing the TEST keyword>>”
 #3  If a custom Bypass_Triggers__c field is checked, skip all the logic 
 
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Gateway Bikes

TRIGGER_ON_LEAD AfterInsert { 
Integer countOfKeyFields = 0;
Set<String> keyFields = new Set<String>{'FirstName','LastName','Email'};
String fieldsWithTest = '';
List<task> lstOfTask = new List<task>();
for(Lead leadRecord : triggerNew) { 
countOfKeyFields = 0;
if(!leadRecord.Bypass_Triggers__c) { 
countOfKeyFields = leadRecord.FirstName != null ? countOfKeyFields + 1: countOfKeyFields;
countOfKeyFields = leadRecord.LastName != null ? countOfKeyFields + 1: countOfKeyFields;
countOfKeyFields = leadRecord.Email != null ? countOfKeyFields + 1: countOfKeyFields;

fieldsWithTest = leadRecord.FirstName != null && leadRecord.FirstName.containsIgnoreCase('test') ? fieldsWithTest + 'FirstName,' : fieldsWithTest;
fieldsWithTest = leadRecord.LastName != null && leadRecord.LastName.containsIgnoreCase('test') ? fieldsWithTest + 'LastName,' : fieldsWithTest;
fieldsWithTest = leadRecord.Email != null && leadRecord.FirstName.containsIgnoreCase('test') ? fieldsWithTest +  'Email,' : fieldsWithTest;

if(countOfKeyFields == 3 )
{
for(String fieldName : keyFields){
    task t = new Task( 
                 whatID = leadRecord.id,
                 Subject = 'Verify the ' + fieldName,
                 Priority = 'Normal',
                 Status = 'Not Started',
                 Type = 'Action',
                 ownerID = leadRecord.ownerID
                 );
                 lstOfTask.add(t);
}
}
if(fieldsWithTest != '')
{
task t = new Task( 
                 whatID = leadRecord.id,
                 Subject = 'Verify the ' + fieldName,
                 Priority = 'Normal',
                 Status = 'Not Started',
                 Type = 'Action',
                 Description = 'This Lead contains the TEST keyword in the following key fields: ' + fieldsWithTest;
                 ownerID = leadRecord.ownerID
                 );
                 lstOfTask.add(t);
}
}
}

if(lstOfTask != null && !lstOfTask.isEmpty())
 insert lstOfTask;


Cheers!!!