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
Brandon BrownBrandon Brown 

Can someone tell me why this won't pass the Trailhead Challenge for Manipulating Records with DML?

Well, all I can say is that it does add an account, and if you send it a blank string, it does return a NULL, so I am wondering what I did wrong. Here is the code:

public class AccountHandler {

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

Thanks in advance for the help,

Feeling dumb
Best Answer chosen by Brandon Brown
Frédéric TrébuchetFrédéric Trébuchet
Hi,

As said in the challenge description, "If the account is successfully inserted, the method should return the account record".
So, replace
return(acct.id);
by
return acct;

Hope this helps,
Fred

All Answers

Frédéric TrébuchetFrédéric Trébuchet
Hi,

As said in the challenge description, "If the account is successfully inserted, the method should return the account record".
So, replace
return(acct.id);
by
return acct;

Hope this helps,
Fred
This was selected as the best answer
Brandon BrownBrandon Brown
I figured I did something dumb!
 
Gayathri Adoni04Gayathri Adoni04
Hi 
Brandon Brown,

Can you help me in this challenge,because i couldn complete this challenge so please help me out.I am sharing my code,please help me out.


public class AccountHandler {

    public static  Account insertNewAccount(String a) {
      system.debug('HEloo' );
    
     try {
        
        Account acct = new Account();
         if(a!=null){
         acct.name=a;
         insert acct;
         system.debug('acct'+acct.name);
         return acct;  
         }
         else{
         acct.name=a;
         insert acct;
         return null; 
         }
        }
 catch (DmlException e) {
    System.debug('A DML exception has occurred: ' +e.getMessage());
    return null;
}
         
}
}

Thanks,
Gayathri
Stephen Johnson 13Stephen Johnson 13
Also need to change the method to be:
public static Account insertNewAccount(String myStr) {
instead of:
public static ID insertNewAccount(String myStr) {


 
Manish Anand 10Manish Anand 10
Hi,
For this challenge, I have written below code:

public class AccountHandler {
    public static Account insertNewAccount(string str1)
    {
        try{
        Account acct=new Account(Name=str1);
        insert acct;
        return acct;
         }
        catch (DMLException e)
        {
            System.debug('A DML exception has occurred: ' +
                         e.getMessage());
          return (NULL);
        }
        
    }
}
This shows 0 compile time error, as well as I got the points for this.
However, when I debug it through, 'Open Execute Anonymus Window' by passing paramter 'AcmeTest' , as shown below.
AccountHandler.insertNewAccount(acmeTest);
It gives error- Line 1, column:33
Variable doesn't exist:acmeTest

what's the reason for it?

 
Frédéric TrébuchetFrédéric Trébuchet
Hi,

Try 
AccountHandler.insertNewAccount('acmeTest');
Because acmeTest is a string representing the Account name.

Hope this helps,
Fred
Manish Anand 10Manish Anand 10
Ops my bad.Thanku.
Bob Gillis 7Bob Gillis 7
I am getting "Only top-level class methods can be declared static".  I am missing something simple?

public class AccountHandler {
    public static  insertNewAccount(String parameter) {
        try {
            Account acct = new Account(Name=parameter);
            insert acct;
            return acct;
            
        } catch (DmlException e) {
            System.debug('A DML exception has occurred:' + e.getMessage());
            return(NULL);
        }
    return acct;
    }
}
 
Bob Gillis 7Bob Gillis 7
public static  Account insertNewAccount(String parameter) {
Darren BellDarren Bell
Hi, like Bob I am getting the same error: "Only top-level class methods can be declared static". However I really can't see what I'm doing wrong on this one - can someone help? 

public class AccountHandler {
    public static Account insertNewAccount(String AccountName){ 
    
        // Create the account sObject 
        Account acct = new Account(Name=AccountName);
        
        // Insert the account by using DML
        Database.SaveResult[] saveResultList = Database.insert(acct,false);
}
Yves Asselin 8Yves Asselin 8
This worked for me...

public class AccountHandler {

    public static Account  insertNewAccount(String myStr) {
        try {
            Account acct = new Account(Name=myStr);
            insert acct;
            system.debug('acct'+acct.name);
             return acct;

            
        } catch (DmlException e) {
            System.debug('A DML exception has occurred:' + e.getMessage());
            return(NULL);
        }
        
    }
            
}