I am writing a test to test lead conversion. The test is failing because the lead has a failing lead validation rule that checks a custom field value that is set using a Before trigger. However, even though we have the Enforce Validations and Triggers
setting turned on, during the test, the Before trigger doesn't appear to be firing.
Here's the relevant bits of the test code:
// convert the lead with signed status
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(l.id);
LeadStatus convertStatus = [select Id, MasterLabel from LeadStatus where IsConverted = true limit 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
Database.LeadConvertResult lcr = Database.convertLead(lc);
// assert the lead was converted
System.assert(lcr.isSuccess());
In the trigger, I placed this line to see what's going on. During a normal run the output works. During the test, it doesn't fire at all.
System.debug('Lead Trigger: ' + Trigger.operationType);
There's only one trigger for Lead and the first line is the debug statement. The trigger just isn't getting fired for some reason.
Does anyone know if a Before triggers should fire whilst testing the lead conversion process?
Is there anything I need to set before executing the conversion process?
insert l;
statement in your testmethod, then the only reason I can see for before triggers not to fire isWhen the request comes from other sources, such as an Apex application or a SOAP API call, Salesforce validates only the foreign keys. Before executing a trigger, Salesforce verifies that any custom foreign keys do not refer to the object itself.
before update
was firing, the validation error wouldn't occur since thebefore update
trigger sets the appropriate value.convertLead
. (In the UI, these two events are in separate transactions