You need to sign in to do that
Don't have an account?

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 :
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
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
Greetings to you!
- Please use the below separate class and trigger for your problem : -
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
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
Try the following code it may be helpful for you:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
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
Try the following code it may be helpful for you:
Trigger:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Greetings to you!
- Please use the below separate class and trigger for your problem : -
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.