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
Santosh Borfalkar 15Santosh Borfalkar 15 

Please help me with this assignment


Write a trigger on the Lead object that populates a custom “Key Fields Populated” (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

This is my code 

trigger KeyFieldsPopulated on Lead (before insert)
{
    List<Lead> leads = new List<Lead>();
    for (Lead L :trigger.new)
    {
        Set<String> fieldsPopulated = new Set<String>();
        
        fieldsPopulated.add(L.FirstName);
        fieldsPopulated.add(L.LastName);
        fieldsPopulated.add(L.Website);
        fieldsPopulated.add(L.Title);
        fieldsPopulated.add(L.Email);
        fieldsPopulated.add(L.Phone);
        integer count;
        
        for (string field : fieldsPopulated )
        {
            for ( integer i = 0 ; i< fieldsPopulated.size();i++)
            {
                if ( field !=null)
                    
                {
                    System.debug(field);
                        count += 1;
                      
                    L.Key_fields_Populated__c = count;
                }
            }
        }
    }
  }

 
Best Answer chosen by Santosh Borfalkar 15
Khan AnasKhan Anas (Salesforce Developers) 
Hi Santosh,

Greetings to you!

I have made few changes in your code. Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger KeyFieldsPopulated on Lead (before insert) {
    
    Set<String> fieldsPopulated = new Set<String>{'FirstName','LastName','Website','Title','Email','Phone'};
        Integer count = 0;
    
    for (Lead ld: Trigger.new) {
        for(String fieldName : fieldsPopulated){
            // Check if the field is Blank
            if(ld.get(fieldName) != null)
                count++;
        }
        ld.Fields_Count__c = count;
        count= 0;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Ajay K DubediAjay K Dubedi
Hi Santosh,
Try this code:
trigger KeyFieldsPopulated on Lead (before insert)
{
    List<Lead> leads = new List<Lead>();
    integer count = 0;
    for (Lead L : trigger.new)
    {
        if(L.FirstName == null) {
            count++;
        } 
        if(L.LastName == null) {
            count++;    
        }
        if(L.Website == null) {
            count++;    
        }
        if(L.Title == null) {
            count++;    
        }
        if(L.Email == null) {
            count++;    
        } 
        if(L.Phone == null) {
            count++;    
        }
        
        L.Key_fields_Populated__c = count;
                
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Santosh Borfalkar 15Santosh Borfalkar 15
Hi Ajay , will you please look at my code let me know , what's wrong with that ? I want to learn what I am doing wrong
 
Santosh Borfalkar 15Santosh Borfalkar 15
Itried your code , I populated only FirstName and LastName  but the result for Key_fields_Populated__C was 4
Khan AnasKhan Anas (Salesforce Developers) 
Hi Santosh,

Greetings to you!

I have made few changes in your code. Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger KeyFieldsPopulated on Lead (before insert) {
    
    Set<String> fieldsPopulated = new Set<String>{'FirstName','LastName','Website','Title','Email','Phone'};
        Integer count = 0;
    
    for (Lead ld: Trigger.new) {
        for(String fieldName : fieldsPopulated){
            // Check if the field is Blank
            if(ld.get(fieldName) != null)
                count++;
        }
        ld.Fields_Count__c = count;
        count= 0;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Santosh,
I have made some changes to the code this will check the condition that given fields are not null.
Try this:
trigger KeyFieldsPopulated on Lead (before insert)
{
    List<Lead> leads = new List<Lead>();
    integer count = 0;
    for (Lead L : trigger.new)
    {
        if(L.FirstName != null) {
            count++;
        } 
        if(L.LastName != null) {
            count++;    
        }
        if(L.Website != null) {
            count++;    
        }
        if(L.Title != null) {
            count++;    
        }
        if(L.Email != null) {
            count++;    
        } 
        if(L.Phone != null) {
            count++;    
        }
        
        L.Key_fields_Populated__c = count;
                
    }
}


Thanks,
Ajay Dubedi
Santosh Borfalkar 15Santosh Borfalkar 15
@khan Anas , I tried its still not working , code is looping through only first name and last name ..not other fields , please take a look and help me fix this
trigger KeyFieldsPopulated on Lead (before insert)
{
    List<Lead> Leads = new List<Lead>();
    Set<String> fieldsPopulated = new Set<String>{'FirstName','LastName','Website','Title','Email','Phone'};
        Integer count = 0;
    
    for (Lead ld: Trigger.new) 
    {
        for(String fieldName : fieldsPopulated)
        {
            // Check if the field is Blank
            if(ld.get(fieldName) != null)
            {
                system.debug(ld.get(fieldname));
                count++;    
            }
            ld.key_fields_populated__c = count;
        }
        leads.add(ld) ;
    }
}

 
Santosh Borfalkar 15Santosh Borfalkar 15
@Ajay , this is also not working. Even though I am populating 4 fields its showing 3 , when tried with 3 result was 2. thank you.