+ Start a Discussion
SennahSennah 

Trigger to change lead.Status after conversion

Hi community,

 

 I am trying to code a trigger that sets the lead's status after it was converted according to either if or if not a Oppti has been created.

 

It basically would look like this:

 

 

trigger LeadConvertStatusOnLeadUpdate on Lead (before update) {

for (Lead l : trigger.new) {

if (l.ConvertedContactId != null && l.ConvertedOpportunityId == null) {
l.Status = 'Closed - Converted to Contact';
update l;
}

if (l.ConvertedContactId != null && l.ConvertedOpportunityId != null) {
l.Status = 'Closed - Converted to Opportunity';
update l;
}
}

}

 

 But this didn't worked out. It just did not set that value.

I tried to make it a trigger "after update" but that also did not worked. It threw me an error that the lead is read-only.

 

Any ideas?

 

Thanks,

Hannes

 

Message Edited by Sennah on 10-26-2009 09:28 AM
Best Answer chosen by Admin (Salesforce Developers) 
BrianWKBrianWK

I don't think you'll be able to use a trigger to do this. The problem is that the lead is set to "Read only" once the conversion is done. The conversion process itself changes the Lead Status -- and by default triggers aren't fired. So a before trigger on the Lead being converted won't work because of the order the Trigger is fired before the lead is converted. I believe for a before trigger it's Fire trigger THEN converts. Since the convert process changes the status you're out of luck.

 

A few things you can do.

 

1. Drill it in to your staff's brains that if they create an opportunity the Converted Status should be "X"

2. Restrict lead conversion to a small handful of "sale support staff" who do the conversion and properly set the stage

3. Scrape Salesforce's standard conversion and write your own code using Apex and Visualforce.

 

I ended up doing #3. It gives you more flexibility since you can setup your own page message (validation rules) on your own convert page. It also let's you control what goes into the database.Convert process.

 

It's more work, but it gives you greater flexibility and safer data validation when working with a staff who are unable to get #1 done !:)

 

Good luck!

 

All Answers

BrianWKBrianWK

I don't think you'll be able to use a trigger to do this. The problem is that the lead is set to "Read only" once the conversion is done. The conversion process itself changes the Lead Status -- and by default triggers aren't fired. So a before trigger on the Lead being converted won't work because of the order the Trigger is fired before the lead is converted. I believe for a before trigger it's Fire trigger THEN converts. Since the convert process changes the status you're out of luck.

 

A few things you can do.

 

1. Drill it in to your staff's brains that if they create an opportunity the Converted Status should be "X"

2. Restrict lead conversion to a small handful of "sale support staff" who do the conversion and properly set the stage

3. Scrape Salesforce's standard conversion and write your own code using Apex and Visualforce.

 

I ended up doing #3. It gives you more flexibility since you can setup your own page message (validation rules) on your own convert page. It also let's you control what goes into the database.Convert process.

 

It's more work, but it gives you greater flexibility and safer data validation when working with a staff who are unable to get #1 done !:)

 

Good luck!

 

This was selected as the best answer
jkucerajkucera

The best check for whether a lead is being converted is:

 

if (lead.IsConverted) {  }

 I haven't tested this, but I believe you can update lead fields using a before update trigger even when a convertion is taking place.

 

Might be an issue checking if ID's are null.  Perhaps instead you can test the length of the ID?

 

I'd first test using lead.IsConverted, then if that changes the status, try variants to check if OpptyID or ContactID are poopulated.

SennahSennah

Hey guys,

 

a big thank you for getting me back on track with this.

 

Brian, I think writing my own conversion page would be the best solution but finding time for such a project will be hard ;) Maybe I will just wait until Salesforce allows to customize the convert page and stay with the two status values. It is okay that way but I thought I could make it a little more usable.

 

John, according to the debug log and my test class it looks like that a "before trigger" is not fired on  the lead conversion.

 

Cheers,

Hannes

BrianWKBrianWK

Sennah, you're welcome. I actually found writing the basic convert page fairly easy. What I found difficult was writing it to include all the fancy dancy stuff my sales team wanted --- and still provide the same look, feel, and validation rules.

 

I'll be happy to share with you some of my code if you're interested. Send me a message with your e-mail and I can send you the code as .txt It's an awful lot of lines, but it works. Fair warning that while my unit test does get me 99% code coverage -- I haven't updated with any system.asserts yet!

 

Here's the method that actually does the conversion:

 

public void ConvertLeads() { Database.LeadConvert lc = new database.LeadConvert(); lc.setLeadID(ThisLead.id); //Set ID of the Lead being converted lc.setOverwriteLeadSource(false); if(AttachAccount.id != null){lc.setAccountId(AttachAccount.ID);} //Set the ID of Existing Account lc.setConvertedStatus(ConvertStatus); //Set Convert Status of Lead lc.setOwnerId(RecordOwnerOpp.OwnerId); //Set the Owner of the Opportunity to be created lc.setDoNotCreateOpportunity(OpportunityCreate); //Set if an Opportunity will not be created lc.setSendNotificationEmail(NotifyOwner); //Sets if owner gets an e-mail //Set ContactID for Exisiting Contact if(AttachContact.ID != null) { lc.setContactID(AttachContact.id);system.debug('Contact Attached: '+ AttachContact.id); } //Set Name of the Opportunity to be Created if no Opportunity currently exists if(AttachOpportunity.ID == null) { lc.setOpportunityName(RecordOwnerOpp.Name); system.debug('Set Opportunity Name: ' + RecordOwnerOpp.Name); } system.debug('LeadConvert before Processed: ' + lc); /*Convert Lead and get Results of Conversion*/ lcr = null; try { lcr = database.convertLead(lc); //Converts Lead and returns results as a variable system.debug('LeadConvert Errors: ' + lcr.getErrors()); //system.assert(lcr.isSuccess(),'Lead Insert Failed: ' + lcr.getErrors()); } catch (exception e) {ApexPages.addMessages(e);} }

 

 

You can have a barebones setup. Mine is more difficult since in our industry we already know the majority of our clients. So when a lead comes in, chances are we already have an Account and a Contact created for them. This means that I had to build my own "Search for Account" and "Search for Contact"  methods so they can attach to an Existing Account and Contact.

 

If you don't have that issue -- then your page really just needs: Display Account (Lead.Company), Display Contact (Lead.Name), Checkbox to create Opportunity (True/False), Input for Opportunity Name (I used a temporary Opportunity object to do this called RecordOwnerOpp), Input for Converted Status and Checkbox for notify user.

 

That's it. If you can trust your staff not to forget to fill out the above boxes then you got a really simple easy page you can code!

jkucerajkucera

2 possible reasons you aren't having triggers fire upon conversion:

1) You don't have Apex Lead Convert enabled (Can file a support case to enable this-perm turns on triggers, workflow, & validation rules for Lead, Contact, Oppty, and Account during conversion)

2) Setup-->Customize-->Leads-->Settings: Enable Validation & Triggers from Lead Convert - set this to true

 

https://na1.salesforce.com/help/doc/user_ed.jsp?section=help&target=leads_convert.htm&loc=help&hash=topic-title

SennahSennah

Hey John,

 

thanks for that hint!

I've just opened a ticket on it.

 

Best,

Hannes

SennahSennah

I would love to answer but the Board does not allow to post this...

so I made a screenshot: