1

We have a custom object that initially is related to lead, and upon lead conversion needs to be associated to account and contact. the custom object has lookup fields to lead, account and contact. I have found the code below in the appexhcnage and wanted to test in our sandbox, but it doesn't work. when I run the test class, I get the following error message:

System.DmlException: ConvertLead failed. First exception on row 0; first error: INVALID_STATUS, invalid convertedStatus: Approved Deal Registration: []

We have 2 record types in lead ("lead" and "deal registration"), each one has it's own converted statuses.

Not sure why it should affect this code, but I'd figure it's better to ask here...

Trigger:

trigger UpdateCustomeObject_Trigger on Lead (before update) {
//This trigger will associate a Custom Object record with the contact and opportunity associated to the 
//lead after it has been converted.
//The Custom Object is associated to an opportunity only if an opportunity record exist on the Lead.
    for (Integer i = 0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
            Set<Id> leadIds = new Set<Id>();
            for (Lead lead : Trigger.new) 
                leadIds.add(lead.Id);

            Map<Id, CustomObject__c> entries = new Map<Id, CustomObject__c>([select Contact__c, Opportunity__c, Account__c, Lead__c from CustomObject__c where lead__c in :leadIds]);        
            if(!Trigger.new.isEmpty()) {
                for (Lead lead : Trigger.new)  {
                    for (CustomObject__c CustomObject : entries.values()) {
                        if (CustomObject.Lead__c == lead.Id) {
                            CustomObject.contact__c = lead.ConvertedContactId;
                            CustomObject.opportunity__c = lead.ConvertedOpportunityId;
                            CustomObject.account__c = lead.ConvertedAccountId;
                            update CustomObject;                            
                        }
                    }
                }
            }
        }
    }

}

Test class:

@isTest
//This is a test case for a situation where a lead will be converted.  The developer must explicitly call the convert lead
//method to simulate the user action.

private class TestTriggerCustomObjectUpdate {
        static testMethod void TestReferralUpdate() {
        // Insert the Lead
        List<Lead> leads = new List<Lead>();
        Lead leadt = new Lead (FirstName ='fname', LastName ='test', Company ='myCompany');
        insert leadt;
        // Insert the custom object Record 
        CustomObject__c customobject = new CustomObject__c (Lead__c = leadt.Id);
        insert customobject;        

        //Convert the Lead
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(leadt.Id);       
        LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult lcr = Database.convertLead(lc);    

        //Requery for the referral record to see if it is updated
        CustomObject__c ref_upd = [select Account__c, Contact__c, Opportunity__c from CustomObject__c where Lead__c = :leadt.Id];

        //Check that the test passed
        System.assertEquals(ref_upd.Account__c,[Select ConvertedAccountId From Lead Where Id = :customobject.Lead__c].ConvertedAccountId);
        System.assertEquals(ref_upd.Contact__c,[Select ConvertedContactId From Lead Where Id = :customobject.Lead__c].ConvertedContactId);
        System.assertEquals(ref_upd.Opportunity__c,[Select ConvertedOpportunityId From Lead Where Id = :customobject.Lead__c].ConvertedOpportunityId);       

        //Test if no opty is created
        string NoOpty = 'Y';        
        if (NoOpty =='Y'){
            Lead leadto = new Lead (FirstName ='fnameo', LastName ='testo', Company ='myCompanyo');
            insert leadto;
            // Insert the custom object record 
            CustomObject__c customobjecto = new CustomObject__c (Lead__c = leadto.Id);
            insert customobjecto;

            Database.LeadConvert lco = new database.LeadConvert();
            lco.setLeadId(leadto.Id);
            lco.isDoNotCreateOpportunity();
            lco.setDoNotCreateOpportunity(true);
            LeadStatus convertStatuso = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
            lco.setConvertedStatus(convertStatuso.MasterLabel);
            Database.LeadConvertResult lcro = Database.convertLead(lco); 

            CustomObject__c ref_updo = [select Account__c, Contact__c, Opportunity__c from CustomObject__c where Lead__c = :leadto.Id];

            //Check that the test passed
            System.assertEquals(ref_updo.Account__c,[Select ConvertedAccountId From Lead Where Id = :customobjecto.Lead__c].ConvertedAccountId);
            System.assertEquals(ref_updo.Contact__c,[Select ConvertedContactId From Lead Where Id = :customobjecto.Lead__c].ConvertedContactId);
            System.assert(ref_updo.Opportunity__c == null);
        }   
    }

    static testMethod void testBulkUpdate() {
        List<Lead> leads = new List<Lead>();       
        for (Integer i=0;i<5;i++) {
            Lead l = new Lead (FirstName ='bulk', LastName ='Test', Company ='myCompanyo');
            insert l;
            // Insert the Custom Record 
            CustomObject__c r = new CustomObject__c (Lead__c = l.Id);
            insert r;

            //Convert the Lead
            Database.LeadConvert lcb = new database.LeadConvert();
            lcb.setLeadId(l.Id);
            LeadStatus convertStatusb = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
            lcb.setConvertedStatus(convertStatusb.MasterLabel);
            Database.LeadConvertResult lcrb = Database.convertLead(lcb);

            CustomObject__c bulkup = [select Account__c, Contact__c, Opportunity__c from CustomObject__c where Lead__c =:l.Id];

            //Check that the test has passed
            System.assertEquals(bulkup.Account__c,[Select ConvertedAccountId From Lead Where Id = :r.Lead__c].ConvertedAccountId);
            System.assertEquals(bulkup.Contact__c,[Select ConvertedContactId From Lead Where Id = :r.Lead__c].ConvertedContactId);
            System.assertEquals(bulkup.Opportunity__c,[Select ConvertedOpportunityId From Lead Where Id = :r.Lead__c].ConvertedOpportunityId);
            }   
        }
}
1

The error says that you don't have a converted status of 'Approved Deal Registration.' Either set the 'Converted' flag to checked for this status, or create a new status with this value that is a converted status. The remainder of the code is acceptable, although you should look at code that actually uses bulkified lookups to avoid the expensive outer-inner loop that this code has.

0

Not 100% sure but the most possible reason for this error is due to Lead process, I believe there is no lead process in your sandbox.

Create a Lead process as:

Customize | Lead | Lead Process > new (provide name) > Assign a default value e.g; Closed-Converted()Converted) and save it.

  • I already defined a lead process for both and assigned it to each record type. – Itay B Sep 23 '13 at 6: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.