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
spoorthisri spspoorthisri sp 

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.

Here is the code that i have written:

public class AccountHandler {
    public Static Account insertNewAccount(String AccountName)
    {
        Account acc=new Account(name=AccountName)
        Database.SaveResult[] saveResultList = Database.insert(acc,false);
    }

}

Is it the right solution?, If yes please suggest me what is the execution code for this solution.
Best Answer chosen by spoorthisri sp
Amit Chaudhary 8Amit Chaudhary 8
Please save same class from Salesforce UI means
Setup-->Apex Classess

Then try your challenage again

All Answers

spoorthisri spspoorthisri sp
public class AccountHandler {
    public Static Account insertNewAccount(String AccountName)
    {
        Account acc=new Account(name=AccountName);
        try{
            insert acc;
        }
        catch(DMLException e){
            return null;
        }
        return acc;
    }

}

This is the another solution I have tried.Please suggest me which one is correct and what  is the execution statement for the following code?
Amit Chaudhary 8Amit Chaudhary 8
Hi,

There are multiple way to do the same code .

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.
 
public class AccountHandler
{
    public static Account insertNewAccount(String sName)
    {
        Account acc= new Account();
        acc.Name = sName;
        try
        {
            insert acc;
            return acc;
        }
        catch(Exception ee)
        {
            return null;
        }
    }
}

As per this module we need to use Try Catch. Then you can try above code.

To Execute above code you need to try below code in developer console

For Success

AccountHandler obj = new AccountHandler();
obj.insertNewAccount('Test');

......................

For Fail case

AccountHandler obj = new AccountHandler();
obj.insertNewAccount(null);


Let us know if this will help you
 
spoorthisri spspoorthisri sp
I am facing this error message
Amit Chaudhary 8Amit Chaudhary 8
Please save same class from Salesforce UI means
Setup-->Apex Classess

Then try your challenage again
This was selected as the best answer
spoorthisri spspoorthisri sp
Hi Amit,
Thank you.I followed the Setup-->Apex Classess and tried with my another code.. Its executed.

public class AccountHandler {
    
 public Static Account insertNewAccount(String an){
        Account acc=new Account(name=an);
            try{
                insert acc;
                return acc;
            }
        catch(DMLException e){
            return null;

        }
    }
 
}
Muhammad Saad JavedMuhammad Saad Javed
hello every one, i am not being able to resolve this task, please help me , my class is as

public class AccountHandler {
    public Static Account insertNewAccount(String sName)
    {
        Account acc=new Account();
        acc.Name = sName;
        try{
        insert acc;
        return acc;
        }Catch(DMLexception e){
        return null;
        }
    }

}

Trailhead is giving 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
Maksim KramerMaksim Kramer
Static ≠ static
Vani K. GaurVani K. Gaur
Hi Muhammad

As stated by Maksim, I suppose you must be able to identify your error. 
keyword "static" should have small "s".

Following code is succeeded by Trailhead.

public class AccountHandler {
    public static Account insertNewAccount(String s){
        Account acct = new Account(Name=s);
        try 
        {
            insert acct;
            return acct;
        }
        catch(DmlException e)
        {
            return null;
        }
    }
}

While executing it, you can call the method as : AccountHandler.insertNewAccount('Your String');
Dharti ShahDharti Shah
I was still encountering the error using the suggestions above.
Launching the brand new/different trailhead playground from the challenge, and than creating class from strach help me passed this challenge.
below are the steps I followed.
1. I was encountering error as below
'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'
2. launch new playground : trial head playground 3
3. dev console > apex class (new ) > create method
4. execute the : AccountHandler.insertNewAccount('give string');
 
saurabhkher19saurabhkher19
Use below Code : 

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

Pass below to test the class in dubug window : 

Account showAccount = new Account();
showAccount = AccountHandler.insertNewAccount('SFDC New Account');
System.debug('New Account Name: '+showAccount);

Your result must be : New Account Name: SFDC New Account

 
Account showAccount = new Account();
showAccount = AccountHandler.insertNewAccount('');
System.debug('New Account Name: '+showAccount);

Your result must be : New Account Name:null

Please let me know if that code works.
Hunter AkersHunter Akers
This worked for me and makes the most sense...check to see if string is not empty and perform task 1. Otherwise cause the like they show in the DML example.

public class AccountHandler {
    public static Account insertNewAccount(String accountName){
        if(accountName != ''){
            Account name = new Account(Name = accountName);
            insert name;
            return name;
        }
        
        try {
            Account name = new Account();
            insert name;
            return name;
        } catch(DMLexception e) {
            return null;
        }
    }
}
Manju Abraham 6Manju Abraham 6
This works 

Apex Class

public class AccountHandler {
    
    public static Account insertNewAccount(String Name) {
        Account  ac = new Account() ; 
        String accountName ; 
        accountName = Name; 
        System.debug('Parameter Name' +accountName) ; 
       
           try{
             
              System.debug('Inside try block') ; 
              ac.Name = accountName ;  
              System.debug('Parameter Name' +Name) ; 
             
              insert ac ;   
                         
           }
           catch(DMLException e ){
              System.debug('Inside catch block') ; 
              System.debug(' A DML Exception has occurred ' +e.getMessage()) ;              
              return null; 
           }
        
        return ac; 
            
    }

}




Execute the following in Developer Console -> Debug -> Execute Anonymous Window to test it out 

AccountHandler.insertNewAccount('acc1 ') ;
This will create a Account with name acc1 . Verfiy the result by checking the Account Tab. A new Account should be created. 

AccountHandler.insertNewAccount(' ') ;
This will generate an exception. 
Abilash SenthilkumarAbilash Senthilkumar
Few people got System.NullPointerException: Attempt to de-reference a null object that's because you might have given any validation rules in the account object. The validation rule acts as a mandatory field.
Deactivate those and try the below code.

public class AccountHandler {
    Public static Account insertNewAccount(String AccName)
    {
        Account acc= new Account(Name = AccName);
           try {
            insert acc;
                return acc;
               }
            catch (DMLException e)
            {          
             system.debug('Failed due to  '+e.getMessage());   
             return null;              
            }
    }
}


Open the developer console -- > AccountHandler.insertNewAccount('Your String');
 
Vrushabh LengadeVrushabh Lengade
We can also solve this challenge without using the try cath exceptions as,
public class AccountHandler {
    public static Account insertNewAccount(String AccountName) {
        if(AccountName.length()>=1){
            Account acc = new Account(Name=AccountName);
            insert acc;
            return acc;
        }
        else {
                return null;      
               }
        }
}
//To verify enter this line of code in Execute Anonymous window:
AccountHandler.insertNewAccount('trailhead');
SOUVIK SAHA 7SOUVIK SAHA 7
Why I am getting this error while executing 

User-added image

My code is :

public class AccountHandler {
    public static Account insertNewAccount(String s){
        Account Acc = new Account( Name = s);
        try {
            
        
            insert Acc;
            
            
        }catch (DmlException e) {
            system.debug('Failed due to  '+e.getMessage());
            return Null;
        }
        
       return Acc; 
    }
    
    

}


For Executing it I have written :

AccountHandler obj = new AccountHandler();
obj.insertNewAccount('Test');

 
Varinderjit KaurVarinderjit Kaur
public class AccountHandler {
public static Account insertNewAccount (String Name){ 
   
       try{
           Account acct = new Account(Name='pharma Account');
insert acct;
// Once the account is inserted, the sObject will be 
// populated with an ID.
// Get this ID.
ID acctID = acct.ID;
// Add a contact to this account.
Contact mario = new Contact(
    FirstName='Mario',
    LastName='Ruiz',
    Phone='415.555.1212',
    AccountId=acctID);
insert mario;
         
           System.debug('Account created');
           return acct;
       } catch(Exception e){
           System.Debug('Account not created');
           return null;
       }
   } 
}
here is code but no challege is passed,, i don.t know the reaso
Suribabu MallipamuSuribabu Mallipamu
Hi All,

Can somebody help me on this line public static Account insertNewAccount(String Name) {

why we have to use Account name after static keyword ? 
 
André HenriqueAndré Henrique

I used the following code and it worked.

 

public class AccountHandler {
    public static Account insertNewAccount(String accountName) {
        Account newAccount = new Account(Name = accountName);
        
        try {
            insert newAccount;
            return newAccount;
        } catch (DMLException e) {
            System.debug('An error occurred while inserting the account: ' + e.getMessage());
            return null;
        }
    }
}