function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sumithasumitha 

Test class error for lead coversion

Hi All,
      Here is my apex trigger,
trigger LeadConversionTrigger on Lead (after update) {
    /*
     * Create maps of Ids to store the records
     * associated with the converted leads
     */
    Map<Id,Id> convertedOpportunityIds = new Map<Id,Id>();
    
    /*
     * Loop through the leads submitted through this
     * trigger.  Populate the appropriate maps of Ids
     * for each lead with populated values.
     */
    for (Lead l : Trigger.New) {
        
        // Get a reference to the old values associated
        // with this lead.
        Lead oldLead = trigger.OldMap.get(l.Id);
        
        // Determine if the lead was converted or not.
        // If the previous status was not converted and
        // the current status is converted, then this
        // trigger executed because of a conversion.
        if (!oldLead.IsConverted && l.isConverted) {
        
            // if lead was converted and converted opp id got populated
            if (l.convertedOpportunityId != null && oldLead.convertedOpportunityId == null ) {
                convertedOpportunityIds.put(l.Id, l.convertedOpportunityId);
            }
        }
    }
    
    List<Dealer_Registration_Product__c> dealerProducts = [SELECT Id, Name, Lead__c, Opportunity_Name__c FROM Dealer_Registration_Product__c 
                                                                WHERE Lead__c IN: convertedOpportunityIds.keySet() ];
    if ( dealerProducts != null && !dealerProducts.isEmpty() ) {
        for ( Dealer_Registration_Product__c product: dealerProducts ) {
            product.Opportunity_Name__c = convertedOpportunityIds.get( product.Lead__c);
        }
        update dealerProducts;
    }                                                        
}
and i have written the tes class as,
 
@IsTest
public class testLeadTriggers {
    public static testmethod void testContactIdAndConversion() 
    {
        Account acc = new Account( Name = ' name');
        insert acc;
        Contact cont = new Contact( FirstName = 'tt', LastName = 'Contact', AccountId = acc.Id);
        insert cont;
        Lead lead = new Lead( LastName = 'Lead', Company = ' company', Status = 'New', Contact_Id__c = cont.Id );
        insert lead;
        Lead testLead = [SELECT Id, Referred_By__c, Referred_End_User_Account__c FROM Lead WHERE Id =: lead.Id];
        System.AssertEquals( cont.Id, testLead.Referred_By__c );
        System.AssertEquals( acc.Id, testLead.Referred_End_User_Account__c );
        Opportunity opptyLine = new Opportunity (
            Name = 'discount 2',
            AccountID = acc.ID ,
            LeadSource= 'Inbound Call',
            Type ='Quota',
            CloseDate=Date.today(),
            StageName='5.Decision',
           Reseller__c = acc.ID,
            Distributor_Discount_Product_Temp__c=49.5,
            Distributor_Discount_Service_Temp__c=25,
                AIMS_Partner__c='Cerner'
             );
            insert opptyLine;
        
        Dealer_Registration_Product__c dealer = new Dealer_Registration_Product__c( Lead__c = lead.Id );
        insert dealer;
        
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(testLead.id);
        //lc.setDoNotCreateOpportunity(true);
        //lc.setOwnerId(testUser1.id);
        lc.setConvertedStatus('Converted');
        
       Database.LeadConvertResult lcr = Database.convertLead(lc);
      System.assert(lcr.isSuccess());
      opportunity opp = [select id from opportunity where AccountID=: acc.id];
        
        
        Dealer_Registration_Product__c testDealer = [SELECT Id, Opportunity_Name__c FROM Dealer_Registration_Product__c  WHERE Id =: dealer.Id ];
      //  System.Assert( testDealer.Opportunity_Name__c != null );
   
    }
}
But I am getting error as,

System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, There was an error converting the lead. Please try again. If the problem persists, please contact your administrator.: []

Please advice to write the testclass.


Thanks and regards,
Sumitha.P


 
JAYABALAJI TKM 1JAYABALAJI TKM 1
Hi Sumitha

Could you check the below link. Hope this will solve your pbm.

https://developer.salesforce.com/forums/?id=906F0000000BLZFIA4
 
sumithasumitha
Hi JayaBalaji,

Thanks for your reply.I had gone through the link and implemented as like but it showing same error in below line. 

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

error:
System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, There was an error converting the lead. Please try again. If the problem persists, please contact your administrator.: []

Thanks,
Sumitha.P
JAYABALAJI TKM 1JAYABALAJI TKM 1
Hi Sumitha,

For me lead convertion test class covering all lines. Here I am posting the code.
 
@isTest
public class TestdefaultLeadConvert {
    public static  testmethod  void test(){
        Lead newLead=new Lead(LastName='Test',FirstName='TestF',Company='Test');
        insert newLead; 
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(newLead.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());
    }
    public static  testmethod  void test1(){
        Lead lead=new Lead(LastName='Test',FirstName='TestF',Company='Test',Status='Converted');
        insert lead;                
        lead=  [SELECT Id,Status, FirstName,LastName,Company,LeadSource FROM Lead WHERE Id = :lead.Id];
        System.assertEquals('Converted', lead.Status);
        update  lead;
    }
 Pls check above code help for u make it Best Ans

Thanks 
Jayabalaji
sumithasumitha
Hi JayaBalaji ,

      Thanks for code .I have tried the code.I think there is problem in Adminstration part.the test1() method is running and got 58% and but shows same error for test() method.Once I got the test coverage 75% let you know. 

Thanks,
Sumitha. P