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
clay bossclay boss 

Restricting Lead Conversion when pressing Convert

Hello,

I am running into trouble restricting lead conversion based on if certain fields are null.

I have the Industry field and a custom field Revenue Types on the lead object.  Both of these are required fields.  We also have leads coming in from the website, that are allowing leads to bypass this required field on the lead object in Salesforce.com.

Setting custom fields on the lead object that map to custom fields on the Account, Opportunities, and Contact objects, making them required, and mapping them via the User Interface seems like an overly complicated workaround for this.

I am wondering if there is a way via a trigger, or by other means, to make it so if users press "Convert" on the lead conversion process, it will give them an error if Industry and Revenue Types are null.

Setting validation rules on the Opportunity object itself doesn't seem to work for me in this, because this gets in the way of other processes that have previously been programmed.

Hopefully this makes sense.

Thanks for any information!


Best Answer chosen by clay boss
David "w00t!" LiuDavid "w00t!" Liu
I know you said validation rules weren't an option but they may become viable if you check the "Enforce validation and triggers from lead convert" setting!
http://simplysfdc.blogspot.com/2013/12/salesforce-lead-conversion-validation.html

Always better to go no code if it's an option, much easier to scale and deploy!

All Answers

Boris BachovskiBoris Bachovski
You can definitely achieve this by writing a trigger. Your trigger should fire before/after update (I think it'll work in both cases) on the lead object and check whether the lead is in process of being converted and whether your required fields are populated or not. If you return an error inside your trigger, the transaction will roll back. Basically your trigger should look something like this:
trigger Lead on Lead (before update)
{
	for (Lead lead : trigger.new)
	{
		if (lead.IsConverted && trigger.oldMap.get(lead.Id).IsConverted == false && YOURFIELD__c == null)
		{
			lead.YOURFIELD__c.addError('You must populate ' + YOURFIELD__c + ' before converting this lead');
		}
	}
}


clay bossclay boss
Excellent.......... I will work with this.  I did not know this functionality was there.   Thanks Boris!
Boris BachovskiBoris Bachovski
Good luck, let me know how you go.
David "w00t!" LiuDavid "w00t!" Liu
I know you said validation rules weren't an option but they may become viable if you check the "Enforce validation and triggers from lead convert" setting!
http://simplysfdc.blogspot.com/2013/12/salesforce-lead-conversion-validation.html

Always better to go no code if it's an option, much easier to scale and deploy!
This was selected as the best answer
clay bossclay boss
David---This is the answer I have been scouring the internet for.  In this particular situation the codeless option is what is best for me.

I l ooked so many places for the answer but didn't realize you enable that setting and then have the IsConverted option on other objects.

Thanks also to Boris for the trigger answer.

Thanks!
Joe MaguireJoe Maguire
Hey, I'll show you what we do. We work with governments so we require sales reps to enter in budgets. On principle we also require lead sources. We've found it best to alert the rep with a popup when they click "Convert." This provides immediate feedback and doesn't require the rep to go back to the record and edit it. Below I've attached the full JS code I wrote for the button. Btw, This code doesn't make an opportunity ("noopti=1")

As always our company uses bypass rules. This especially helps when cleaning old records that lack fields that are now required.

Code notes
1. Build the error
2. If the error isn't null and the disable validation rule is off for this user, alert the user to enter in the missing data 
3. If there's no error, go to the lead convert page. Note our leads are under accounts. The three acc* fields prepopulate the account so it reads "Attach to Existing." We've already created accounts for all target customers (once again, working in the government space, so we just got a list of govts)
 
var url = "/apex/Lead_Conversion_Validation?id={!Lead.Id}";
var error = "";


////Errors
//Error if no budget or no lead source
if("{!Lead.Budget__c}" == ""){
    error += "Enter a budget!\n";
}
if("{!Lead.LeadSource}" == ""){
    error += "Enter a LeadSource!\n";
}
////

//Only alert if disable validation is off
if(error != "" && {!$Setup.Disable_Validation__c.Disable_Lead_Validation__c} == false){
    window.alert(error);
}
else{
    if("{!Lead.Account__c}" == ""){
        window.location.href = "/lead/leadconvert.jsp?retURL=%2F{!Lead.Id}&id={!Lead.Id}";
    }else{
        window.location.href = "/lead/leadconvert.jsp?retURL=%2F{!Lead.Id}&id={!Lead.Id}&accid=   {!Lead.AccountId__c}&acclkid_lkid={!Lead.AccountId__c}&acclkid_lkwgt={!Lead.AccountId__c}" + "&nooppti=1"; 
    }
}