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
Kiril Vodenicharov 7Kiril Vodenicharov 7 

Need help to wirte Unit Test over popuplated Lead fields.

trigger PopulateKeyFieldCount on Lead (after insert) 
{
    Integer countOfKeyFields = 0;
    
    Set<String> keyFields = new Set<String>{'FirstName', 'LastName', 'Email', 'Phone', 'Website', 'Title'};
    String fieldsWithTest = '';
    
    List<Task> taskList = new List<Task>();
    
    for(Lead leadRecord : Trigger.New)
    {
        countOfKeyFields = 0;
        
        if(leadRecord.Key_Fields_Populated_c__c != null)
        {
            countOfKeyFields = leadRecord.FirstName != null ? countOfKeyFields + 1: countOfKeyFields;
            countOfKeyFields = leadRecord.LastName != null ? countOfKeyFields + 1: countOfKeyFields;
            countOfKeyFields = leadRecord.Email != null ? countOfKeyFields + 1: countOfKeyFields;
        }
        
        if(countOfKeyFields == 3)
        {
            for(String fieldName : keyFields)
            {
                Task task = new Task
                    (
                        Subject = 'Verify the ' + fieldName,
                        Priority = 'Hight',
                        Status = 'Complete',
                        Type = 'Action',
                        ownerId = leadRecord.OwnerId
                    );
                
                taskList.add(task);
            }
        }
        
        
    }
    
    if(taskList != null && !taskList.isEmpty())
    {
        insert taskList;
    }
}

This is a trigger which populates a custom Key Fields. If the trigger had at least 3 Lead key fields it will create a Task.
Best Answer chosen by Kiril Vodenicharov 7
Suraj Tripathi 47Suraj Tripathi 47

Hi Kiril,

If the data type of Key_Fields_Populated_c__c field is Number then check this test class:-

@isTest
public class PopulateKeyFieldCount_Test {
    static testMethod void test(){
        lead le = new lead();
        le.FirstName='Rahul';
        le.LastName='Rai';
        le.Company='cloud analogy';
        le.Email='rahulrai@gmail.com';
        le.Key_Fields_Populated_c__c=234;
        insert le;
        
    }
} 

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Kiril

Please check this test class:-
@isTest
public class PopulateKeyFieldCount_Test {
    static testMethod void test(){
        lead le = new lead();
        le.FirstName='Rahul';
        le.LastName='Rai';
        le.Company='cloud analogy';
        le.Email='rahulrai@gmail.com';
        le.Key_Fields_Populated_c__c='Key Field';
        insert le;
        
    }
}

Please Mark it as Best Answer if it helps.
Thanks
Kiril Vodenicharov 7Kiril Vodenicharov 7

It says "Illiegal assignment from String to Decimal." in your code sample.

Also I'm useing a Pattern down bellow in the code. It will be cool if you help me to finish it.

@IsTest public class PopulateKeyFieldCountTest
{   
    @TestSetup private static void PopulateKeyFieldSetup()
    {
        
    }
    
    @IsTest static void TestPopulatedKeyFields()
     {
         
     }
    
    public static List<Lead> CreateLead()
    {
        List<Task> taskList = new List<Task>();
        List<Lead> leadList = new List<Lead>();
        Set<String> keyFields = new Set<String>{'FirstName', 'LastName', 'Email', 'Phone', 'Website', 'Title'};
        integer countOfKeyFields = 0;
        
        Lead lead = new Lead
            (
            	FirstName = 'Petko',
            	LastName = 'Petrov',
            	Email = 'petko@abv.bg'
        	);
        
        insert lead;
        
        for(Lead leadRecord : [SELECT FirstName, LastName, Email FROM Lead])
        {
            countOfKeyFields = 0;
            
            if(leadRecord.Key_Fields_Populated_c__c != null)
        	{
                countOfKeyFields = leadRecord.FirstName != null ? countOfKeyFields + 1: countOfKeyFields;
                countOfKeyFields = leadRecord.LastName != null ? countOfKeyFields + 1: countOfKeyFields;
                countOfKeyFields = leadRecord.Email != null ? countOfKeyFields + 1: countOfKeyFields;
        	}
            
            if(countOfKeyFields == 0)
            {
                for(String fieldName : keyFields)
                {
                    Task task = new Task
                        (
                        	Subject = 'Verify the ' + fieldName,
                            Priority = 'Hight',
                            Status = 'Complete'
                    	);
                }
                insert task;
            }
        }
        
        
    }
}
Suraj Tripathi 47Suraj Tripathi 47
Hi Kiril,

What is the type of Key_Fields_Populated_c__c field?
Suraj Tripathi 47Suraj Tripathi 47

Hi Kiril,

If the data type of Key_Fields_Populated_c__c field is Number then check this test class:-

@isTest
public class PopulateKeyFieldCount_Test {
    static testMethod void test(){
        lead le = new lead();
        le.FirstName='Rahul';
        le.LastName='Rai';
        le.Company='cloud analogy';
        le.Email='rahulrai@gmail.com';
        le.Key_Fields_Populated_c__c=234;
        insert le;
        
    }
} 
This was selected as the best answer
Kiril Vodenicharov 7Kiril Vodenicharov 7
It is Number(18, 0) filed