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
Nida KhanNida Khan 

Challenge:Manipulating Records with DML

I have written this class for the Trailhead challenge. I am able to find that the account gets inserted but,still I am unable to complete the challenge.Its showing the error "Executing the 'insertNewAccount' method failed. Either the method does not exist, is not static, or does not insert the proper account." Can anyone please help?

public with sharing class AccountHandler {
    public static ID insertNewAccount(String str){
        try{
            Account acc=new Account(Name='CENA');
            insert acc;      
            return(acc.Id);
            
        }
        catch(DMLException ex){
            system.debug('Error Message thrown::'+ex.getMessage());
            return(null);
            
        }
    }
}
Waqar Hussain SFWaqar Hussain SF
public with sharing class AccountHandler {
	
	public static Account insertNewAccount(String accName){
	
		try{
			Account a = new Account(Name=accName);
			
				insert a;
				return a;
			
			}catch (exception e){
				return null;
			}
			
	}

}
Vishnu VaishnavVishnu Vaishnav
Hi Nida,

===== Here is modified code ====
public with sharing class AccountHandler {
    public static ID insertNewAccount(String str){
        try{
            Account acc=new Account(Name=str);
            insert acc;      
            return(acc.Id);
        }
        catch(DMLException ex){
            system.debug('Error Message thrown::'+ex.getMessage());
            return(null);            
        }
    }
}

Its same as your code , little bit i have changed :
       public static ID insertNewAccount(String str){

Use this code to execute in debug :
AccountHandler.insertNewAccount('Test forum');

:::======================================================================:::
Qusetion Solved ? then mark as best answer to make helpful to others .....
hari reddy 35hari reddy 35
Hi Nida Khan,

Here is the code which I modified.
It's working fine for me, hope will work for you also.

====================================================

public with sharing class AccountHandler {
    public static Account insertNewAccount(String str){
        try{
            Account acc=new Account();
            acc.Name=str;
            insert acc;      
            return acc;
        }
        catch(DMLException ex){
            system.debug('Error Message thrown::'+ex.getMessage());
            return(null);            
        }
    }
}

==================>0<==================================

If this solves your problem mark this as best answer to help others easily find the solution.
Bryan SpencerBryan Spencer
It looks like your return type on the "try" statement is incorrect. Try this:

========================================================
public class AccountHandler {
    public static Account insertNewAccount (String strName){
        Account acct = new Account(Name = strName);
       
        try {
       insert acct;
            return acct;
} catch (DmlException e) {
    System.debug('A DML exception has occurred: ' +
                e.getMessage());
    return(null);}
    }
}
=========================================================

If this solves your problem mark this as best answer to help others easily find this solution.
Nida KhanNida Khan
Hello All, Thanks for your answers. I have passed this challenge. Regards, Nida
Sivatej EarlaSivatej Earla
Simplest answer:

public class AccountHandler {
    public static Account insertNewAccount(String accName){
        try{
        Account  a = new Account(Name = accName);
        insert a;
        return a;
        }catch(exception e){
            return null;
        }
    }

}
HariprasathHariprasath
The Answer is:

public class AccountHandler {

    public static Account insertNewAccount(string name) {
       
        try{
            Account acct = new Account();
            acct.Name = name;
            insert acct;
            System.debug(acct);
            return acct;
            } catch (DMLException e) {
        System.debug('Null ');
            return null;
            }
    }
}
MANOJ CHAUHAN 1MANOJ CHAUHAN 1
public class AccountHandler {    
    public static Account insertNewAccount (string strAccName){
       try {
        Account Acct = New Account(Name=strAccName);
        insert Acct;
        return Acct; /// I was also getting same errro until i return the Account itself rather only Account ID
         }
        catch (DmlException e) {
            System.debug('A DML exception has occurred: ' +
                e.getMessage());
            return null;
        }
    }
}
Tai Wai TanTai Wai Tan
So i need to use 'try catch'? Can I use 'if else'? It didn't work by the way but just wondering.... 

public class AccountHandler {

    public static Account insertNewAccount(String T) {
        
        if (string.isBlank(T)){return null;}
        else{
        Account acct = new Account(Name = T);
          insert acct;
          return acct;
        }
}   
}
sai dharnesh YARAMsai dharnesh YARAM

There was an unexpected error in your org which is preventing this assessment check from completing: System.NullPointerException: Attempt to de-reference a null object
hi team,,
i am getting this eror
 
RangaRayapatiRangaRayapati
I have written below code for in Manipulate Records with DML trailhead CHALLENGE, it not accepting but the output is coming correctly. Can you please guide me what is wrong in it.

Create a method for inserting accounts.
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 method must accept an incoming string as a parameter, which will be used to create the Account name
The method must insert the account into the system and then return the record
The method must also accept an empty string, catch the failed DML and then return null

https://trailhead.salesforce.com/content/learn/modules/apex_database/apex_database_dml

public class AccountHandler {

    public static id insertNewAccount(String AccountName){
        Account acc = new Account(Name = AccountName);
        Database.SaveResult srList = Database.insert(acc, false);
        System.debug('srList :'+srList.getId());
        return srList.getId();
    }
}