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
Eric BEric B 

Trigger on the Lead object that populates a custom “Key Fields Populated” field?

Hi,
I need to write a trigger that populates the “Key Fields Populated” custom field with the count of the total number of the following fields that are not null:
● FirstName
● LastName
● Email
● Phone
● Website
● Title
Any suggestions?
Thanks
Best Answer chosen by Eric B
Maharajan CMaharajan C
Hi Eric,

If you want to use the trigger then go with the below steps:

1. Create the Custom Label : Name it as LeadFields

2. Values to be stored as in Custom Label :   FirstName,LastName,Email,Phone,Website,Title  --> If you are using the custom labels then you can add or remove any field names in future

3.  Use the below trigger :

trigger PopulateKeyFieldsCount on Lead (before insert,before update) {
    
    Set<String> leadFieldsSet = new Set<String>(Label.LeadFields.split(','));
    
    if(Trigger.IsInsert)
    {
        
        for(Lead leadrec : Trigger.New){
            Integer count = 0;
            for(String str : leadFieldsSet){
                string fieldvalue = String.valueOf(leadrec.get(str));
                if(!String.isEmpty(fieldvalue))
                {
                    system.debug('@@@  '+ str + ' ==>'+ leadrec.get(str));
                    count += 1;
                }
            }
            leadrec.KeyFieldsCount__c = count;
        }
    }
    
    else if(Trigger.IsUpdate)
    {
        for(Lead leadrec : Trigger.New){
            Boolean countField = false;
            for(String str : leadFieldsSet){
                string fieldvalue = String.valueOf(leadrec.get(str));
                if(!String.isEmpty(fieldvalue) && (Trigger.OldMap.get(leadrec.Id).get(str) == null || Trigger.OldMap.get(leadrec.Id).get(str) == ''))
                {
                    countField = true;        
                }
                
                else if(String.isEmpty(fieldvalue) && (Trigger.OldMap.get(leadrec.Id).get(str) != null || Trigger.OldMap.get(leadrec.Id).get(str) != '')){
                    countField = true;
                }
            }
            if(countField){
                Integer count = 0;
                for(String str : leadFieldsSet){
                    string fieldvalue = String.valueOf(leadrec.get(str));
                    if(!String.isEmpty(fieldvalue))
                    {
                        system.debug('@@@  '+ str + ' ==>'+ leadrec.get(str));
                        count += 1;
                    }
                }
                leadrec.KeyFieldsCount__c = count;
            }
        } 
    }    
}

Thanks,
Maharajan.C

All Answers

Naren9Naren9
Hi Eric,
You can do this by Formula Field.
I have used this my Org to count whether key fields are populated and added.

Change according to your requirement.
IF(ISBLANK(Email) , 0, 1) + IF(ISBLANK(Phone) , 0, 1) + IF(ISBLANK(Company) , 0, 1) + IF(ISBLANK(Title) , 0, 1) + IF( ISPICKVAL(Industry , ""), 0, 1)

Thanks,
Narendar
 
Eric BEric B
Thanks Naren9,
but what i'am trying to do is:
Write a trigger on the Lead object that populates a custom “Key Fields Populated”
number field whenever the Lead is created or edited.
“Key Fields Populated” should count the total number of the following fields that are not null:
● FirstName
● LastName
● Email
● Phone
● Website
● Title
For example, if FirstName, LastName, and Email are populated, “Key Fields Populated” should
equal to 3.
Naren9Naren9
 Hi,
 We can do this requirement by 
 1. Formula Field
 2. Process Builder with Field Update.
 3. Triggers (We can do anything with the Triggers and this has to be last option).
 
 As you are using the KeyFieldUpdate field and it is not a Formula Field.
 So we have the 2 Options - Process Builder or Triggers.
 I have added the Process Builder Option.
 I will look into the Triggers options.
 
 But Salesforce recommends that, if we are not able to acheive with Declarative options, then go for the Triggers and Apex.

Process Builder Steps:
User-added image
User-added image
User-added image
This is the Formula and you can add some more fields:
IF(ISBLANK([Lead].FirstName) , 0, 1) + IF(ISBLANK([Lead].LastName) , 0, 1) +  IF(ISBLANK([Lead].Title) , 0, 1)
 
 Thanks,
 Narendar
 
Maharajan CMaharajan C
Hi Eric,

If you want to use the trigger then go with the below steps:

1. Create the Custom Label : Name it as LeadFields

2. Values to be stored as in Custom Label :   FirstName,LastName,Email,Phone,Website,Title  --> If you are using the custom labels then you can add or remove any field names in future

3.  Use the below trigger :

trigger PopulateKeyFieldsCount on Lead (before insert,before update) {
    
    Set<String> leadFieldsSet = new Set<String>(Label.LeadFields.split(','));
    
    if(Trigger.IsInsert)
    {
        
        for(Lead leadrec : Trigger.New){
            Integer count = 0;
            for(String str : leadFieldsSet){
                string fieldvalue = String.valueOf(leadrec.get(str));
                if(!String.isEmpty(fieldvalue))
                {
                    system.debug('@@@  '+ str + ' ==>'+ leadrec.get(str));
                    count += 1;
                }
            }
            leadrec.KeyFieldsCount__c = count;
        }
    }
    
    else if(Trigger.IsUpdate)
    {
        for(Lead leadrec : Trigger.New){
            Boolean countField = false;
            for(String str : leadFieldsSet){
                string fieldvalue = String.valueOf(leadrec.get(str));
                if(!String.isEmpty(fieldvalue) && (Trigger.OldMap.get(leadrec.Id).get(str) == null || Trigger.OldMap.get(leadrec.Id).get(str) == ''))
                {
                    countField = true;        
                }
                
                else if(String.isEmpty(fieldvalue) && (Trigger.OldMap.get(leadrec.Id).get(str) != null || Trigger.OldMap.get(leadrec.Id).get(str) != '')){
                    countField = true;
                }
            }
            if(countField){
                Integer count = 0;
                for(String str : leadFieldsSet){
                    string fieldvalue = String.valueOf(leadrec.get(str));
                    if(!String.isEmpty(fieldvalue))
                    {
                        system.debug('@@@  '+ str + ' ==>'+ leadrec.get(str));
                        count += 1;
                    }
                }
                leadrec.KeyFieldsCount__c = count;
            }
        } 
    }    
}

Thanks,
Maharajan.C
This was selected as the best answer