2

We have a report that aggregates all of our leads based upon status. The person who actually cares about this data would like to distinguish between a converted (status: qualified) lead and a converted lead that had an opportunity associated with it (custom status: qualified with opportunity).

I wrote a simple afterUpdate trigger which accomplishes this, however it doesn't work because you can't update a lead once it has been converted:

//Update the lead status if there is a related opportunity and the status hasn't previously been set
if (updatedLead.ConvertedOpportunityId != null && updatedLead.Status != STATUS_QUALIFIED_OPP) {
    updatedLead.Status = STATUS_QUALIFIED_OPP;
    update updatedLead;
}

I seemingly can't use a beforeUpdate trigger because the ConvertedOpportunityId hasn't been set so I don't know if there's a related opportunity.

Does anyone have an idea how I can accomplish changing the status once I know the lead has a related opportunity?

Edit

Here is the trigger:

trigger LeadTrigger on Lead (after delete, after insert, after undelete, after update, before delete, before insert, before update) {
    LeadTriggerHandler handler = new LeadTriggerHandler(Trigger.size);

    if(Trigger.isUpdate && Trigger.isAfter){
        handler.OnAfterUpdate(Trigger.new);
    }
}

And the OnAfterUpdate method:

public void OnAfterUpdate(Lead[] updatedLeads) {
    this.VerifyLeadStatus(updatedLeads);
}

public void VerifyLeadStatus(Lead[] updatedLeads) {
    for (Lead updatedLead : updatedLeads) {
        System.debug(updatedLead.Status + ' ' + updatedLead.ConvertedOpportunityId);            

        if(updatedLead.Status == 'Qualified' && updatedLead.ConvertedOpportunityId != null) {
            updatedLead.Status.addError('You cannot use Qualified as a status value when creating an opportunity');
        }

        if(updatedLead.Status == 'Qualified with Opportunity' && updatedLead.ConvertedOpportunityId == null) {
            updatedLead.Status.addError('You cannot use Qualified with Opportunity as a status value when not creating an opportunity');
        }
    }
}
2

The answer to this question is practically in the question: you can't detect conversion in a "before update lead" database event, so you have no opportunity (pun intended) to alter the status of the converted lead during the conversion process. Instead, you will need to simply validate if the status is correct for the type of conversion that occurs:

trigger LeadTrigger on Lead (after update) {
    for(Lead record: Trigger.new) {
        if(record.Status == 'Qualified' && record.ConvertedOpportunityId != null) {
            record.status.addError('You cannot use Qualified as a status value when creating an opportunity');
        }
        if(record.Status == 'Qualified with Opportunity' && record.ConvertedOpportunityId == null) {
            record.status.addError('You cannot use Qualified with Opportunity as a status value when not creating an opportunity');
        }
    }
}

Also note that it appears that IsConverted is also set to false until the after update event fires during lead conversion.

Important Note

If your instance was created prior to 2008 you might not have the "Enable Validation and Triggers from Lead Convert" setting enabled. If this setting is not enabled your lead triggers will not fire during lead conversion. To enable the feature open a case with Salesforce and ask them to enable it.

  • Thanks for the reply, however, I haven't been able to get this to work. The status value is always the old value on the lead; it never has the value specified during the conversion process. – Justin Helgerson Feb 3 '14 at 20:37
  • @Ek0nomik Could you update your question with the full version of your trigger as it stands now? This code works verbatim in my developer organization, so I suspect there's simply some small tweak you need to apply to your version. – sfdcfox Feb 3 '14 at 21:05
  • I added the code. I wonder if it has something to do with the setting: "Enable Validation and Triggers from Lead Convert". I keep seeing this mentioned online, but this setting doesn't exist in our instance. – Justin Helgerson Feb 3 '14 at 21:09
  • @Ek0nomik That feature was removed, apparently-- it's now always executed from what I can tell (ran into this writing the test version). That aside, your code appears sound. You may need some debug logs to try and track down what's going on. – sfdcfox Feb 3 '14 at 21:22
  • When I debugged the trigger with that System.debug call, I saw the original status (none of the "Qualified" values), but I also saw the converted opportunity id. It's as if the trigger is executing after it has been converted, but before some of the conversion process has finished. – Justin Helgerson Feb 3 '14 at 21:52
1

To achieve your requirement couldn't you use a formula field instead of code?

For example (I haven't tested this), if (!ISNULL(ConvertedOpportunityId) && TEXT(Status) != STATUS_QUALIFIED_OPP, 'STATUS_QUALIFIED_OPP', TEXT(Status) )

Then you could use this field for your reports....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.