+ Start a Discussion
Arun ChaubeyArun Chaubey 

Create a method for inserting accounts

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.
Best Answer chosen by Arun Chaubey
NagaNaga (Salesforce Developers) 
Hi Arun,

Can you please let me know if the code below helps you


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;
}
}
This method takes name string parameter and makes a new Account. Then it tries to insert it, but if insert fails then it returns null. If insert succeeds, it will return that account object.

Best Regards
Naga Kiran
 

All Answers

NagaNaga (Salesforce Developers) 
Hi Arun,

Can you please let me know if the code below helps you


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;
}
}
This method takes name string parameter and makes a new Account. Then it tries to insert it, but if insert fails then it returns null. If insert succeeds, it will return that account object.

Best Regards
Naga Kiran
 
This was selected as the best answer
Arun ChaubeyArun Chaubey
Hi Naga,

Thank you very much. It is working fine. I was stuck with the last part. Thank You once again.
Jean-Christophe LEGEAYJean-Christophe LEGEAY
You could also create the new account directly with the string given in parameter :

public static Account insertNewAccount (string nom){            
     
        Account NewAccount = new Account(Name = nom);
Srinivas KoduruSrinivas Koduru
I think follwoing is what is just fine I think.

public class AccountHandler {

    public static Account insertNewAccount(String actName){
        Account act = new Account(Name=actName);
        try{
            insert act;
            return act;
        }catch(DMLException e){
            system.debug('DML Exception: ' + e);
            return null;
        }
    }
}

Check if this is working fine or not by using the following code in your Execute Anonymous Window
Satyanarayana PusuluriSatyanarayana Pusuluri
Hi,

Below code working fine for this Challenge 

public class AccountHandler {
    public static Account insertNewAccount(String name) {
        Account Acct = new Account();
        Acct.Name = name;
        try
        {
            insert Acct;
        } catch (Exception e) {
            return null;
        }
        return Acct;
    }
}

Thanks & Regards,
Satya P
Kishan MalepuKishan Malepu
public class AccountHandler {
        public static account insertNewAccount(String Name)
        {    
            Account acc = new Account();
            acc.Name  = Name;
            try{
                insert acc;
             }
            catch(DMLException e)
            {
                System.debug('A DML exception has occurred: ' + e.getMessage());
                return null;
            }
            return acc;
        }
 
}
Yazz_KYazz_K
You guys are awesome. Any recommendations on, let's say, a crash-course with Apex?
hutch doghutch dog
what to write in the apex code window for this task?
Muhammad AlaaMuhammad Alaa
Here is what I did:
 
public class AccountHandler {
    public static Account insertNewAccount(String accntName) {
        try{
            Account acct = new Account();
        	acct.Name = accntName;
        	insert acct;
			return acct;
        }catch(DmlException e){
            return null;
        }
    }
}

IMHO, the compiler has issues with Finally in TryCatch blocks, in general
Rahul Gupta 137Rahul Gupta 137
User-added image

why such error ?
Julie NJulie N
Rahul : line 5 you want the variable you've defined at line 2, so just put on line 5 :

acct.Name = Name;

I think it'll be ok then ;) 
 
Mayank shahMayank shah
public class AccountHandler {
    public static Account insertNewAccount(string Test){
       
        Account acct = new Account();
        acct.Name = Test;
         try{
        insert acct;
        }
        Catch(Exception e){
            System.debug ('An Error has occoured'+e.getMessage());
            return null;
        }
            return acct;
    }
}
Ben RowleyBen Rowley
public class AccountHandler {

    public static Account insertNewAccount(String accName){
        
        Account acc = new Account(Name=accName, Phone='(415)555-1212', NumberOfEmployees=100);
        
        Database.SaveResult sr = Database.insert(acc, false);

           if (sr.isSuccess()) {
        // Operation was successful, return
               return acc;
           } 
        else {
            //what was the error?
            for(Database.Error err : sr.getErrors()) {
                System.debug('The following error has occurred.');
                       System.debug(err.getStatusCode() + ': ' + err.getMessage());  
         }
    }
       //then return null 
       return null;

        }
    }
Loic MesseguerLoic Messeguer
For anyone getting this error : " Challenge Not yet complete... here's what's wrong: 
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 "
Try going into your Object "Account" and deactivate any Validation Rules that you have running.
Hope I helped someone.