Write a Trigger for, When ever new lead is created , calculate the leadscore using before inserting

By | November 16, 2019

When ever new lead is created , calculate the leadscore using before inserting

Field Name  NOT NULL    NULL

Email       10      0

Phone       10      0

Industry    20      0


Note : First Create a custom field LeadScore on Lead object

Trigger Code

This trigger code will calculate the Lead Score…

trigger leadscoreTrig on Lead (before insert) {
    for(Lead ld: Trigger.New){
       Integer count=0;
        if(ld.email!=''&& ld.email!=null){
            count=count+10;
        }
        if(ld.phone!='' && ld.phone!=null ){
            count=count+10;
        }
        if(ld.industry!='' && ld.industry!=null){
            count=count+20;
        }
        ld.leadScore__c=count;
    }
}

Test Class for the Trigger

@isTest
private class LeadScoreTest {
	@isTest
    static void testme(){
        Lead ld=new Lead();
        ld.lastname='Salesforce';
        ld.company='SalesforceCodes';
        ld.phone='111';
        ld.email='salesforcecodes123@gmail.com';
        ld.industry='Banking';
        try{
        	insert ld;
        }catch(Exception e){
            
        }
		Lead newLead=[select LeadScore__c from Lead where id=:ld.id];
		System.assertEquals(newLead.LeadScore__c,40);        
    }
}
Share this...
Share on Facebook
Facebook
Email this to someone
email

Leave a Reply

Your email address will not be published. Required fields are marked *