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
Natasha AliNatasha Ali 

Test class with try/catch error handling methods help!

Hi,
I have a trigger in Salesforce which converts Leads into Person Accounts if the 'Company Name' field is Null :
Trigger AutoConvert on Lead (after insert) {
     LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1
     ];
     List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

     for (Lead lead: Trigger.new) {
          if (!lead.isConverted && lead.Company == null) {
               Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Name;
               
               lc.setLeadId(lead.Id);
               lc.setDoNotCreateOpportunity(true);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               
               leadConverts.add(lc);
          }
     }

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}

I need to write some test classes to validate and deploy this code and it needs to include any error handling. How would I go about achieving this? 

Any help is much appreciated :)
Many Thanks,
Natasha  
Best Answer chosen by Natasha Ali
Deepali KulshresthaDeepali Kulshrestha
Hi Natasha,
Greetings to you!

- Please use the below separate class and trigger for your problem : -
 
Trigger : -
    Trigger AutoConvert on Lead (after insert) {
        if(trigger.isInsert && trigger.isAfter){
            autoConvertClass.autoConvertMethod(Trigger.new);
        }
    }

Class : -

    public class autoConvertClass {
        public static void autoConvertMethod(List<Lead> leadList){
            try{
                for (Lead leadInstance: leadList) {
                    if(!leadInstance.isConverted && leadInstance.Company == null) {
                        Database.LeadConvert lc = new Database.LeadConvert();
                        lc.setLeadId(leadInstance.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());
                    }
                }
            }
            catch(Exception e){
                system.debug('Line number->'+e.getLineNumber()+'Error Msg->'+e.getMessage());
            }
        }
    }


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Natasha,

Greetings to you!

Please refer to the below blog which has a sample code with the proper explanation which might help you further with the above requirement.

http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/

If an exception is thrown from the code being tested (or the test code) then the test is a fail and that is normally what you want to happen.
So there is no value in adding try-catch to a test. Reference: https://salesforce.stackexchange.com/questions/31050/can-we-use-try-catch-inside-a-test-class-is-that-a-best-practice

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi Natasha,

Try the following code it may be helpful for you:
 
Trigger AutoConvert on Lead (after insert) {
    if(trigger.isInsert && trigger.isAfter){
    try{

     for (Lead lead: Trigger.new) {
          if (!lead.isConverted && lead.Company == null) {
                Database.LeadConvert lc = new Database.LeadConvert();
                lc.setLeadId(lead.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());
          }
     }
    }
    catch(Exception e){
        system.debug('LineNo->'+e.getLineNumber()+' ,Msg->'+e.getMessage());
    }
        
    }
     
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Natasha AliNatasha Ali
Hi Ajay,
Thank you for that! The catch statement seems to be in the trigger itself (my programming knowledge is very beginner btw), shouldn't it be in a separate test class?

Many Thanks :) 
Natasha
Ajay K DubediAjay K Dubedi
Hi Natasha,

Try the following code it may be helpful for you:
Trigger:
Trigger AutoConvert on Lead (after insert) {
    if(trigger.isInsert && trigger.isAfter){
        convertLead.checkLeadCompany(trigger.new);
    }
}
Class:
public class convertLead {
    public static void checkLeadCompany(List<Lead> leadList){
        if(leadList.size()>0){
            try{
                for(Lead lead:leadList){
                    if (!lead.isConverted && lead.Company == null) {
                Database.LeadConvert lc = new Database.LeadConvert();
                lc.setLeadId(lead.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());
          }
                }
            }
            catch(Exception e){
            system.debug('LineNo->'+e.getLineNumber()+' ,Msg->'+e.getMessage());
            }
        }
    }
}
Test-Class:

@IsTest
 public class convertLead_Test {
 @IsTest
    public static void checkLeadCompany_Test(){
        Lead lstLead =   new Lead();
        lstLead.LastName = 'LASTNAME';
        lstLead.Status = 'Open';               
        insert lstLead; 
    }
 }



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Deepali KulshresthaDeepali Kulshrestha
Hi Natasha,
Greetings to you!

- Please use the below separate class and trigger for your problem : -
 
Trigger : -
    Trigger AutoConvert on Lead (after insert) {
        if(trigger.isInsert && trigger.isAfter){
            autoConvertClass.autoConvertMethod(Trigger.new);
        }
    }

Class : -

    public class autoConvertClass {
        public static void autoConvertMethod(List<Lead> leadList){
            try{
                for (Lead leadInstance: leadList) {
                    if(!leadInstance.isConverted && leadInstance.Company == null) {
                        Database.LeadConvert lc = new Database.LeadConvert();
                        lc.setLeadId(leadInstance.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());
                    }
                }
            }
            catch(Exception e){
                system.debug('Line number->'+e.getLineNumber()+'Error Msg->'+e.getMessage());
            }
        }
    }


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
This was selected as the best answer