+ Start a Discussion
Kerstin KoehlerKerstin Koehler 

Help with writing a Trigger on Lead Object

I am trying to write a trigger on my Lead Object that will not allow a user to click Convert unless a boolean field is True. I need help with some sample code to get started. 

I plan to create an Approval Process on the lead record, then with the final Approver action have a field update to have my custom checkbox (Approved) set to True. 

Once this happens I want the user to be able to click Convert, but only once the checkbox is set to true. If it has not gone through the approval process, the user shoudnt be able to click convert. 

I should also note I would only want this for a specific record type of my lead object.
Best Answer chosen by Kerstin Koehler
karthikeyan perumalkarthikeyan perumal
Hello, 

here is the Trigger code for preenting Lead convertion based on boolean value. 
 
trigger leadPreventer on Lead (before insert, before update)
  {
    if(trigger.isUpdate)
     {
       for(Lead l:Trigger.new)       
        {       
           if (l.IsConverted && l.rajam_karthik__Boolean_Field__c !=true)       
            {           
              l.addError('Its not possible- Approved boolean field is not true.');
            }       
        }
     }
  }

Hope this will help you.

Thanks
karthik
 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

here is the Trigger code for preenting Lead convertion based on boolean value. 
 
trigger leadPreventer on Lead (before insert, before update)
  {
    if(trigger.isUpdate)
     {
       for(Lead l:Trigger.new)       
        {       
           if (l.IsConverted && l.rajam_karthik__Boolean_Field__c !=true)       
            {           
              l.addError('Its not possible- Approved boolean field is not true.');
            }       
        }
     }
  }

Hope this will help you.

Thanks
karthik
 
This was selected as the best answer
karthikeyan perumalkarthikeyan perumal
replace this field "rajam_karthik__Boolean_Field__c" with your boolead field 
Kerstin KoehlerKerstin Koehler
Thank you!!!