0

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?

  • 1
    Do you have any trigger handler framewok? You then need to insert a record to enable triggers. salesforce.org/table-driven-trigger-management-matters – Pranay Jaiswal Sep 9 '19 at 16:09
  • No, there's currently no trigger framework in this org. Also updated the question with more detail. – Swisher Sweet Sep 9 '19 at 17:38
  • assuming you actually have an insert l; statement in your testmethod, then the only reason I can see for before triggers not to fire is When 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. – cropredy Sep 9 '19 at 17:44
  • @cropredy the error occurs when the convert process tries to update the lead status, but the new status causes a validation failure on another field. If before update was firing, the validation error wouldn't occur since the before update trigger sets the appropriate value. – Swisher Sweet Sep 9 '19 at 19:58
  • 1
    ok -- I know you said there is no trigger framework but do you have recursion control? Inserting a lead in a testmethod and then converting it will occur in the same transaction and any static variables will not be cleared between the original mocking of the test lead and the execution of the convertLead. (In the UI, these two events are in separate transactions – cropredy Sep 9 '19 at 20:15
0

As it turns out, there was code that abruptly ended the execution of the trigger because of a missing Custom Setting in the test context.

After removing this code, the trigger executed as expected.

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.