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
Ruben VascoRuben Vasco 

Error in module "Manipulating Records with DML"

Hi, 
I have a problem with module "Manipulating Records with DML"
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.
My code is: 

public class AccountHandler {

    public static ID insertNewAccount(String nombre){
        ID prueba =null;
         Account cuent= new Account();
                try {
                  cuent = new Account(Name=nombre);
                  insert cuent;
                       prueba = cuent.Id;
               
            } catch (DmlException e) {
                  System.debug('A DML exception has occurred: ' +
                        e.getMessage());
              }
        System.debug('**********' +prueba);
        System.debug('**********' +cuent.Name);
        return prueba;
    }
}
I would like you ayudaseis I find the error.
Thank you
Best Answer chosen by Ruben Vasco
Esther OnemaEsther Onema
Instead of returning an ID you are to return an Account object. Thus, you must first check if the incoming string is null, if not create an account object and name it after the string. Then simply insert the account and return it. Try this and let me know if this solution worked for you. 
public class AccountHandler 
{
    public static Account insertNewAccount(String par)
    {
        //If the string is empty return null
        if(par=='')
            return NULL;
        
        //If not, name the account after the string
        //Insert the account into the system 
        //and return the account record
        else
        {
            Account acct = new Account(Name=par);
            insert acct;
            return acct;             
        }
        
    }
    
}
Thank you,
Esther Onema