1

i create the trigger and unit test for convert lead process, the trigger seems work, but the unit test code has error.

here is the trigger code

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

    Database.LeadConvert[] leadCollectionArray = new Database.LeadConvert[]{};
    for(Lead mylead : trigger.new){
        if(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);
}

here is the unit test code

@isTest
private class UnitTest_ConvLeadtrigg02 {

    static testMethod void ConvLeadtrigg02() {

       test.StartTest();

       // Create the Lead object
       Lead testLead = new Lead(
           FirstName='Demo name800',
           LastName = 'Demo name888 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);
       System.assert(lcr.isSuccess());

       test.stopTest();
   }
}

Please help to check, thanks

3
  • whats the erro? – Mohith Shrivastava Aug 19 '13 at 17:03
  • 1
    In the future, please try and format your code properly to make it easier to read. I have gone ahead and formatted for you here, but you will get better feedback in the future if you give well written questions. – Jesse Altman Aug 19 '13 at 17:07
  • What is this Lead trigger behavior supposed to be doing and when and under what conditions? – Mark Pond Aug 19 '13 at 19:07
2

Once a lead is converted, the Lead record cannot be updated. Your trigger will need to check the leads in the update operation to ensure that they have not already been converted, else you will receive an error. "cannot reference converted lead"

The Lead object has an IsConverted property that you can check to see if it has been converted.

You should be able to simply change the if statement in your code block to include this field.

if(myLead.isConverted && mylead.Ready_to_Convert__c == true){
2
  • hi, Mark, thanks, i did modify the condition check part as suggest, something like change under APEX trigger if((myLead.isConverted == false) && (mylead.Ready_to_Convert__c == true)){ other code here... } But, the error still occur under unit test code which is need to modify as well i guess. Thanks – webon100 Aug 19 '13 at 18:30
  • @webon100 can you post the solution to the problem as an answer of your own and then accept that answer or accept one of the answers given? – Mark Pond Aug 22 '13 at 23:41

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.