+ Start a Discussion
masthan khanmasthan khan 

Create A trigger with calculating lead score when a lead is inserted

Hai,
       We have scenario like this when ever new lead is created lead score should be calculated   and inserted in feild name Leadscore__c  using before Trigger.

considering
Email -10 points,
Date of Birth- 10 points
Annual Revevenue 10points
Phone--10  Points
Contact Address - 10 Points
Company -10 points
Position__c- 10 points.

With Regards
Masthan Khan.
masthan khanmasthan khan
Hai, Nishant 
I tried with above code it is throwing following error.
Error: Compile Error: Incompatible types since an instance of Lead is never an instance of Lead at line 3 column 24.

With Regards.
Nishant Prajapati 10Nishant Prajapati 10
trigger CalculateLeadScore on Lead (Before insert){
    for(Integer i=0; i< trigger.size; i++){
        Integer score = 0;
        
        if(Trigger.new[i].Email <> null)
            score += 10;
        //DOB__C = Date of Birth
        if(Trigger.new[i].DOB__C <> null)
            score += 10;
        if(Trigger.new[i].AnnualRevenue <> null)
            score += 10;
        if(Trigger.new[i].Phone <> null)
            score += 10;
        if(Trigger.new[i].Address <> null)
            score += 10;
        if(Trigger.new[i].Company  <> null)
            score += 10;
        if(Trigger.new[i].Position__c <> null)
            score += 10;
        
        Trigger.new[i].Leadscore__c  = score;
    }
}

Try this now
masthan khanmasthan khan
hai,
I am trying to write this code on before trigger on lead object, it is throwing following error.;
Error: Compile Error: Variable does not exist: firstname at line 4 column 6

trigger leadscore on Lead (before insert) {
integer score=0;
for(lead a:Trigger.new){
if(a.firstname !=null)
score=score+10;
}}

With Regards
Masthan Khan
masthan khanmasthan khan
hai,
I am trying to write this code on before trigger on lead object, it is throwing following error.;
Error: Compile Error: Variable does not exist: firstname at line 4 column 6

trigger leadscore on Lead (before insert) {
integer score=0;
for(lead a:Trigger.new){
if(a.firstname !=null)
score=score+10;
}}

With Regards
​Masthan Khan
v varaprasadv varaprasad
Hi Masthan,

Please check below sample code : 
 
trigger CalculateLeadScore on Lead (Before insert){
   

   for(Lead l : trigger.new){
        Integer score = 0;
        
        if(l.Email <> null)
            score += 10;
       
        if(l.AnnualRevenue <> null)
            score += 10;
        if(l.Phone <> null)
            score += 10;
        if(l.Address <> null)
            score += 10;
        if(l.Company  <> null)
            score += 10;
        
        
        l.Leadscore__c  = score;
		
    }
}




Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For  Support: varaprasad4sfdc@gmail.com