2

i am working on this APEX trigger and unit test class. What it does for a existing lead -> a check out let's called "Ready to Convert" once is CHECKED and saved, the lead will be converted to Person Acct, and it works for mass field update as well.

the current unit test code seems only cover 44% which is suppose to >75% enable to prepare to deploy, please help to check

these lines of code not been covered

Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(mylead.Id);
        lc.setConvertedStatus('Converted');
        lc.setDoNotCreateOpportunity(true);
        leadCollectionArray.add(lc);

trigger

trigger ConvLeadtrigg02 on Lead (after insert, after update) {

Database.LeadConvert[] leadCollectionArray = new Database.LeadConvert[]{};
for(Lead mylead : trigger.new){
    if((myLead.isConverted == false) && (mylead.Ready_to_Convert__c == true)){
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(mylead.Id);
        lc.setConvertedStatus('Converted');
        lc.setDoNotCreateOpportunity(true);
        leadCollectionArray.add(lc);
    }
}
Database.LeadConvertResult[] lcr = Database.convertLead(leadCollectionArray,false);

}

Unit test class

@isTest
private class UnitTest_ConvLeadtrigg02 {

static testMethod void ConvLeadtrigg02() {

   test.StartTest();

   // Create the Lead object
   Lead testLead = new Lead(
   FirstName='Demo 100800',
   LastName = 'Demo 100800 UnitTest'
   //Status='Prospect',
   //Ready_to_Convert__c = true
   );

   insert testLead;
   System.debug('Created and inserted lead');

   Database.LeadConvert lc = new database.LeadConvert();
   lc.setLeadId(testLead.Id);

   LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
   lc.setConvertedStatus(convertStatus.MasterLabel);
   Database.LeadConvertResult lcr = Database.convertLead(lc);

   // Make sure conversion was successful
   System.assert(lcr.isSuccess());

   test.stopTest();
   }
   }

many thanks in advance.

4

Looks like you have a line commented out in your test class.

//Ready_to_Convert__c = true

Since you aren't inserting a lead with this field, it won't meet the criteria. Try uncommenting that line.

3
  • Hi Daniel, i did try and error still there. error: ConvertLead failed. First exception on row 0; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: [] line with error: Database.LeadConvertResult lcr = Database.convertLead(lc); since this is "after insert/after update" trigger, may be the reason not fire the trigger ? Thanks
    – webon100
    Aug 23 '13 at 17:12
  • Why are you doing a convert in the test? Shouldn't the trigger be doing that? Aug 23 '13 at 17:16
  • Hi Daniel, thanks, and you are right, the test class only check the insert and that is. thanks for the help.
    – webon100
    Aug 23 '13 at 18:30

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.