2

I am a bit stuck writing a trigger that checks if the Lead Status was changed to a converted status without converting.

As per standard, "Qualified" is the only status that can be used as a converted status. But it is possible to have more than one converted status.

If a user selects one of the converted status in the standard editor, no actual conversion happens, but we want the users to not be able to do that at all. So my trigger should react to this behaviour with an error.

If I have a fixed status, I can of course hard code that, but I think that's suboptimal, I would like to do it more generic.

So: from Apex, can I find out if the current Lead Status is one that is marked as converted?

2
  • Can you please add the code here so we can take a look? Commented Dec 13, 2019 at 11:20
  • I am asking for the code. In general the functionality works, what I would like to know is: how can I get all Lead Status picklist values that are marked as "converted". Commented Dec 13, 2019 at 11:23

1 Answer 1

3

This is possible, you just need to check IsConverted field on Lead, therefore, if it is unchecked, than you need to prevent lead update if new status is contained in converted statuses.

trigger LeadTrigger on Lead (after insert, after update) {
    List<LeadStatus> convertedStatuses = [
            SELECT Id, MasterLabel
            FROM LeadStatus
            WHERE IsConverted = true
    ];
    Set<String> convertedStatusesNames = new Set<String>();

    for (LeadStatus status_i : convertedStatuses) {
        convertedStatusesNames.add(status_i.MasterLabel);
    }

    for (Lead lead_i : Trigger.NEW) {
        if (!lead_i.IsConverted && convertedStatusesNames.contains(lead_i.Status)) {
            lead_i.addError('You cannot set status to converted without actually converting lead');
        }
    }
}
4
  • Thank you, but "add your statuses here" means hard coding. I don't want to do that, I want to generically react to all status that are marked as converted so that the code is future-proof. Commented Dec 13, 2019 at 11:24
  • Noted, will update the answer in few minutes. Commented Dec 13, 2019 at 11:27
  • 1
    @FlorianHeer Updated the answer, now you can dynamically get converted statuses. Commented Dec 13, 2019 at 11:34
  • Thank you, perfect. The fact that I could query for LeadStatus was what I was missing. Commented Dec 13, 2019 at 11:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .