You need to sign in to do that
Don't have an account?

How to Trigger on Lead Conversion (to update a field)
I'm very, very new to Apex and at a loss. I have a business requirement to update a custom Lead picklist field called "Qualification Status" to "Converted" when a Lead is converted. I've created the following Trigger:
I'm at a loss... How can I check for IsConverted in the Trigger?
trigger LeadQualStatusToConvertedTrigger on Lead (before update) { List<Lead> convertedLeads = new List<Lead>(); for(Lead convertedLead : Trigger.New) { if (convertedLead.IsConverted) { convertedLeads.add(convertedLead); } } LeadQualStatusToConverted.setQualStatusConverted(convertedLeads); }I've tested LeadQualStatusToConverted.setQualStatusConverted() and it works. The issue is that nothing satisfies the Trigger's If clause. Here's a relevant piece of my test class:
@testSetup static void setup() { List<Lead> testLeads = new List<Lead>(); for(Integer i=0; i<10; i++) { testLeads.add(new Lead(LastName = 'Smith' + i ,Company = 'Smith Co.' ,Qualification_Status__c = 'MQL' ,IsConverted = False)); } Database.insert(testLeads); } @isTest static void TestUpdatedConvertedLead() { Lead testLead = [Select Id, Qualification_Status__c, IsConverted From Lead Where LastName='Smith1']; Test.startTest(); Database.LeadConvert lc = new Database.LeadConvert(); lc.setLeadId(testLead.Id); lc.setConvertedStatus('Qualified'); Database.LeadConvertResult lcr = Database.convertLead(lc); Test.stopTest(); System.assert(True,testLead.IsConverted); System.assert(lcr.isSuccess()); System.assertEquals('Converted',testLead.Qualification_Status__c); }The first two asserts pass, but the one that matters (the third), does not. The test result statest that the Qualification_Status__c is still the initialized value of MQL. So, it's successfully being converted, but not making it past the Trigger's If and thus not being passed to the setQualStatusConverted() method.
I'm at a loss... How can I check for IsConverted in the Trigger?
BUT...
I'm still hitting an error within the Trigger: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old (occurring on Line 11 - where LeadQualStatusToConverted.setQualStatusConverted(convertedLeads) is called).
I have no idea how to fix this. Any thoughts?
All Answers
I believe you need to query the converted lead again. You've set the Qualification Status to Converted but didn't query it afterward. In test classes, to check your assert, you need to query the lead you just updated.
Try this, this should do the trick.
That's odd. It does the trick for me. Perhaps some issue with the activation of the process? Have you checked the debug console to see if it is executed when you convert a lead?
perhaps it works if you enable this option. There is also a permission called: View and Edit Converted Leads in the permission sets. Could be very well the thing you're looking for.
In any case though, I'm still curious why my Trigger isn't working...
1. Create a new lead. Exclude the isConverted status. It's false by default.
2. run your test
3. query the new lead (so it loads the lead with the new values.
4. run your assert.
I can send over an example tomorrow. Can't test anymore since I'm travelling.
I did a little modification to your code and I see mine has achieved 100% coverage. The updated code is -
LeadQualStatusToConvertedTrigger The test class is same as what Joel has provided.
In my Dev org, I had 2 to make 2 changes to be working for your code:
Thanks. The only difference I see in your code is adding the action to the Trigger and removing it from the Class, but that isn't my problem. I've verified that the method to update the Qualification Status works fine and I don't want it in the Trigger itself.
It's not clear to me how your test data is satisfying the If IsConverted criteria though. As Joel had suggested, I added an additional SOQL query after the conversion code in the test method, but it still did not work. And, to make sure it's clear, my goal is not 100% code coverage...my goal is to actually have a functioning Trigger.
Upon running the test, this is the debug log:
Given that, I believe the Trigger isn't even firing upon conversion. This output suggests it's only firing on the initial @testSetup data creation. I have a feeling this is a very elementary problem with the way I'm setting up test data & running the tests...I just don't know what it is.
BUT...
I'm still hitting an error within the Trigger: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old (occurring on Line 11 - where LeadQualStatusToConverted.setQualStatusConverted(convertedLeads) is called).
I have no idea how to fix this. Any thoughts?
Glad to hear that it is firing now. Can you please paste your code to see what's happening in - LeadQualStatusToConverted.setQualStatusConverted(convertedLeads);
Also - can you please now give a try to my update trigger code earlier just to see if it's achieves your purpose ?
So, as it turns out, my only problem now is the test assertion. I've confirmed the Trigger to be working by manually createding & converting a Lead. The issue I posted about the error was rather simple to fix; that is, there's no need for a DML operation in a before update since the record isn't yet saved anyway. Thus, to fix it, all I had to do was remove the DML operations. Here's the working Trigger & Helper Class:
But, despite it actually working in practice and despite having 100% code coverage, I am getting the following when executing my test method: System.AssertException: Assertion Failed: Expected: Converted, Actual: MQL I'm so close, yet so far...