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
Ashish_PandeyAshish_Pandey 

Trailhead Challenge Error

To pass this challenge, create an Apex class that inserts a new account named after an incoming parameter. If the account is successfully inserted, the method should return the account record. If a DML exception occurs, the method should return null.
  • The Apex class must be called 'AccountHandler' and be in the public scope.
  • The Apex class must have a public static method called 'insertNewAccount'.
  • The 'insertNewAccount' method must accept an incoming string as a parameter, name the account after the parameter, insert it into the system and then return the account record.
    • The 'insertNewAccount' method must also accept an empty string, catch the failed DML and return null.
User-added image
public class AccountHandler {
    public static Account insertNewAccount (String acName){
    if(acName!=''){    
        try{
            Account a = new Account(Name=acName);
            insert a;
            System.debug('Account created');
            return a;
        } catch(Exception e){
            System.Debug('Account not created');
            return null;
        }
    } else {
        return null;
    }
     
        
    }    
}

 
Best Answer chosen by Ashish_Pandey
JyothsnaJyothsna (Salesforce Developers) 
Hi,

Please check if there is any validation rule or trigger on Account object. Please try to deactivate/delete validation rule or trigger. Please check the below code for this challenge.
 
public class AccountHandler {
public static Account insertNewAccount(String name) {
Account a = new Account();
a.Name = name;
try
{
insert a;
} catch (Exception e) {
return null;
}
return a;
}
}


Let me know if it solves your issue.
Best Regards,
Jyothsna

All Answers

JyothsnaJyothsna (Salesforce Developers) 
Hi,

Please check if there is any validation rule or trigger on Account object. Please try to deactivate/delete validation rule or trigger. Please check the below code for this challenge.
 
public class AccountHandler {
public static Account insertNewAccount(String name) {
Account a = new Account();
a.Name = name;
try
{
insert a;
} catch (Exception e) {
return null;
}
return a;
}
}


Let me know if it solves your issue.
Best Regards,
Jyothsna
This was selected as the best answer
Ashish_PandeyAshish_Pandey
Thanx Jyothsna!