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

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);
}
}
}
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);
}
}
}
===== 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 .....
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.
========================================================
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.
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;
}
}
}
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;
}
}
}
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;
}
}
}
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;
}
}
}
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
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();
}
}