1

I have the following trigger that is working properly

trigger LeadConversionTrigger on Lead (after update) {
for (Lead l : Trigger.new){
    if (checkRecursive.runOnce()){
            // only run if there is an order and an approved certification on the lead
        if (l.Order_Count__c > 0 && l.Certification_Current_State__c == 'Approved'
        && l.Potential_VIP_Customer__c == false) {

            // create an instance of the lead convert object
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(l.id);

            // set the status to a converted stage of qualified
            LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus 
                                        WHERE IsConverted=true LIMIT 1];
            lc.setConvertedStatus(convertStatus.MasterLabel);

            // log some stuff
            System.debug('### Lead ID = ' + lc.leadid );
            System.debug('### Is Converted = '+ l.IsConverted );
            lc.setDoNotCreateOpportunity(TRUE);         

            Database.LeadConvertResult lcr = Database.convertLead(lc);

            // log some more stuff
            System.debug('### Result = '+ lc );
            system.debug('### Lead conversion result: '+lc );  

            System.assert(lcr.isSuccess());

        }
    }
}       

}

My first test is failing because of the following error:

System.DmlException: ConvertLead failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, There was an error converting the lead. Please resolve the following error and try again: LeadConversionTrigger: execution of AfterUpdate caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: CANNOT_UPDATE_CONVERTED_LEAD, This lead was already converted to the contact TestLastName on 02/08/2018.: [] Trigger.LeadConversionTrigger: line 22, column 1: []

Here is the test class:

    @isTest
private class LeadConversionTest {
    static testMethod void successfulConversionTest(){

        Lead objLead = new Lead();
        objLead.Company = 'Test12';
        objLead.Status = 'Open';
        objLead.LastName = 'TestLastName';
        objLead.Potential_VIP_Customer__c = false;
        objLead.Order_Count__c = 2;
        objLead.Certification_Current_State__c = 'Approved';

        insert objLead;

        Database.Leadconvert lc = new Database.Leadconvert();
        lc.setLeadId(objLead.id);
        LeadStatus convertStatus = [select id,MasterLabel from LeadStatus where isConverted=true limit 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult lcr = Database.convertLead(lc);

        System.assert(objLead.convertedAccountId != null);  
    }
}

If anyone could show me what I'm missing here it would be greatly appreciated.

2

You're trying to convert the lead twice; once in the test, and once in your trigger. What your test should be doing is simply inserting then updating the lead, then verify the output:

Lead l = new Lead(...);
insert l;
// Change the values for the lead, then...
update l;
l = [SELECT ConvertedAccountId FROM Lead WHERE Id = :l.Id];
System.assertNotEquals(null, l.ConvertedAccountId, 'Expected lead to be converted.');

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.