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
Amit ShrivastavaAmit Shrivastava 

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.
 
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? 
 
Ajay K DubediAjay K Dubedi
Hi Amit,

Please try the below code it will help you:
public class AccountHandler
{
    public static Account insertNewAccount(String sName)
    {
        Account acc= new Account();
        acc.Name = sName;
        try
        {
            insert acc;
            return acc;
        }
        catch(Exception ee)
        {
            return null;
        }
    }
}

Please make it Best Answer if it helps you.

Thanks,
Ajay Dubedi
Amit ShrivastavaAmit Shrivastava
Hi Ajay K 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.
Ajay K DubediAjay K Dubedi

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
 

Amit ShrivastavaAmit Shrivastava
Thanks, Ajay,

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.
 
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) {
           acct = null;

        }
         return acct;
    }
}