You need to sign in to do that
Don't have an account?
Manipulate Records with DML: Developer Beginner - Apex Basics & Database
Hi
While attempting this challenge, I am not able to pass it after verifying this challenge, based on the challenge details I have to write following pice of code which is totally correct, but still, it gave me an error to check code.
While attempting this challenge, I am not able to pass it after verifying this challenge, based on the challenge details I have to write following pice of code which is totally correct, but still, it gave me an error to check code.
public class AccountHandler { public static Account insertNewAccount(string acctName){ Account acct = null; try { acct = new Account(Name=acctName); insert acct; return acct; } catch (DmlException e) { System.debug('A DML exception has occurred: ' + e.getMessage()); } return acct; } }but after correcting this error, I need to write following code to pass this challenge
public class AccountHandler { public static Account insertNewAccount(string acctName){ try { Account acct = new Account(Name=acctName); insert acct; return acct; } catch (DmlException e) { return null; } } }Can anyone let me know what wrong with the first code snippet, is it due to the System.Debug statement in the catch block? or is it completely checking the return type in a catch block?
Please try the below code it will help you:
Please make it Best Answer if it helps you.
Thanks,
Ajay Dubedi
Thanks for the reply, I have already done and pass the challenge, my question is what the difference both the script is making where my first script is getting failed and second is getting passed as both will give the same output and also the first script is more standardize as compare to the second one.
Hi Amit,
You can not initialize the sObject with Null. And make sure exception return type Null so that you can avoid this type of error.
Thanks,
Ajay Dubedi
Even when I change the original snipped with following it worked, I don't think its an issue of null, it more seems that they are explicitly checking the code without using System.debug or maybe due to this it is falling the challenge validation. it's just a thought that why I need to understand what exactly causing the issue.