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
Deepu BDeepu B 

Trailhead class error

This is the Challenge:

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 '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.


My Class:

public class AccountHandler {

    public static Account insertNewAccount (String accName, Account a) {
         try{   
          a.name = accName;
    
         insert a;
        return a;
        }
        catch(Exception e) {
            return null;
        }
    }    
}



User-added image


Throwing this error, how many times i modified the class.
what is the correct class.?
 
Best Answer chosen by Deepu B
Andrea IanniAndrea Ianni
Following the Bob's advices...

<code>
public class AccountHandler {

    public static Account insertNewAccount (String accName){
        
        
    if(accName!=''){    
        try{
            Account a = new Account(Name=accName);
            insert a;
            System.debug('Bravo Andrè! Account created');
            return a;
        } catch(Exception e){
            System.Debug('Account not created');
            return null;
        }
    } else {
        return null;
    }
     
        
    }    
}
</code>

All Answers

bob_buzzardbob_buzzard
I think this line has you confused:

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.

It isn't saying that there should be an account parameter following the string parameter, its saying that the method should take a single string parameter and use that as the name of the account. Thus the account should be a method variable rather than a parameter.
Andrea IanniAndrea Ianni
Following the Bob's advices...

<code>
public class AccountHandler {

    public static Account insertNewAccount (String accName){
        
        
    if(accName!=''){    
        try{
            Account a = new Account(Name=accName);
            insert a;
            System.debug('Bravo Andrè! Account created');
            return a;
        } catch(Exception e){
            System.Debug('Account not created');
            return null;
        }
    } else {
        return null;
    }
     
        
    }    
}
</code>
This was selected as the best answer
Deepu BDeepu B
Hi Andrea lanni, 

Your code has worked well thank you, could you please reveal on thing for me. 

what is mean of this line: "name the account after the parameter" 
Andrea IanniAndrea Ianni
It sounded a little "weird" to me too. I interpreted it as: "use the String passed as parameter for giving the name to the Account".
DmonikaDmonika
while i am executing  this program i am facing this error like 

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.DmlException: Delete failed. First exception on row 0 with id 0019000001Q83SEAAZ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Account has relatedList so deletion is not possible: []

I wrote a trigger for delete. from where this error is rising but why is it checking over it.
Thanks in advance.
DmonikaDmonika
after deleting this delete trigger then it works..but why can any explain it.
Andrea IanniAndrea Ianni
The problem is that your account had "children", as Contacts, Opportunities, and so on. When you tried to delete it Salesforce returned the error because otherwise the look-ups on the "children" pointing to that account you wanted to delete would have pointed to something not existing.

I guess that you deleted the "children" with your trigger. Then you were able to delete the Account too.

The only question is: why the deletion of the Account? Maybe you have something that triggers the deletion somewhere conncted to that code. I'm sorry I cannot help you more than this. 
santosh lakshminarasappa 1santosh lakshminarasappa 1
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.DmlException: Delete failed. First exception on row 0 with id 0012800000dHS7mAAG; first error: DELETE_FAILED, Your attempt to delete My Test Account could not be completed because it is associated with the following cases.: 00001073 : []
 i am getting the above error ...
but above mentioned case number is not there in my org..can any one help?
prabakaran r 7prabakaran r 7
still i am getting this error. please help me on this
prabakaran r 7prabakaran r 7
Can anyone help me on above error please?
 
Jai Prakash GuptaJai Prakash Gupta
@prabakaran r 7 Can you please share your code which you have written to check for this error?
prabakaran r 7prabakaran r 7

@Hi Jai,    Please see below                                                                                                                                                                                                    
 

public class AccountHandler {

    public static Account insertNewAccount(String name) {

        Account account = new Account(Name = name);

        try {

            insert account;

        } catch (System.DmlException e) {

            return null;

        }

        return account;

    }

}

Jai Prakash GuptaJai Prakash Gupta
Do not use 'account' in  Account account = new Account(Name = name);
User 'acc' or something else.
Check if it helps.
Andrew Smith 40Andrew Smith 40
@Andrea Ianni - could you break down your code a bit, just to help my understanding? I had a very similar peice of code as yours, but mine didn't work because instead of:
public static Account insertNewAccount (String accName)

I had

public static Account insertNewAccount (String asd) 

Now, does accName have to be something specific? I don't quite understand...
Andrew Smith 40Andrew Smith 40
Also, I found this is bare essential, minimum amount of code to make this work (I tend to like the SIMPLIEST version of anything, then add on what's needed from there):
 
public class AccountHandler {

    public static Account insertNewAccount (String accName){
          
            try{
                Account asd = new Account(Name=accName);
                insert asd;
            } catch(Exception e){
                return null;
            }
        
    }
    
}

 
MUSTAPHA ELMADIMUSTAPHA ELMADI
If you have this error :
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Delete failed. First exception on row 0 with id 0019000001Q83SEAAZ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Account has relatedList so deletion is not possible: []

You should look in your Account triggers and delete the one causing the probleme.
i spent almost 1h:30 on this error loool
Anamika JakhmolaAnamika Jakhmola

Hi all,

try using the code.

public class AccountHandler {
    public static Account insertNewAccount(String accName){
       Account asd = new Account();
               asd.name = accName;
        try{
            insert asd;
                      
        system.debug('insreted'+asd.name);
        return asd;
        }
        catch(Exception e){
        system.debug('Exception');   
        return null;
    }
    }
    }

 

And below on anonymous window
AccountHandler.insertNewAccount('testsample');

 

 

Rgards,

Anamika

Allison RalphAllison Ralph
Thank you! Nothing else worked!
akash yadav 10akash yadav 10
@ prabakaran r 7
Hey, Thanks Buddy!
It worked without any error...
akash yadav 10akash yadav 10
I just tried following;-
check it, might be helpful..

public class AccountHandler {

    public static Account insertNewAccount(String name) {

        Account acc = new Account(Name = name);

        try {

            insert acc;

        } catch (DmlException e) {

           // System.debug('Exception occured: '+ e.getMessage());
            return null;

        }

        return acc;

    }

}
 
RajasekharReddy KotellaRajasekharReddy Kotella
Thanks a lot its working 
public class AccountHandler {
    
        public static Account  insertNewAccount(string n){
            if(n !=''){  
            try{
            account a=new account ();
            a.Name=n;
               insert a;
            return a;
          }
    
          Catch (DmlException e){
                System.debug('A DML exception has occurred: ' +e.getMessage());
                return null;
        }}
                else{
                    return null;
                }
    }
}
Ram PalakodetyRam Palakodety
If you've been getting this error: 
EXCEPTION_THROWN [11]|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Account number must be 8 characters long.: []

Then there's a good chance you worked on Trailhead's 'Formulas and Validations' module and you might've created a condition to make sure account ID is 8 characters long. You'll need to go to Object Manager > Account > Validation Rules, and deactivate or delete that rule. 
Olavo AlexandrinoOlavo Alexandrino
" There was an unexpected error in your org which is preventing this assessment check from completing" 

Who is facing problems here only go for Account trigger section and comment that triggers that you have, one of them is broke
srujith chintha 4srujith chintha 4
public class AccountHandler {
    
        public static Account  insertNewAccount(string n){
            if(n !=''){  
            try{
            account a=new account ();
            a.Name=n;
               insert a;
            return a;
          }
    
          Catch (DmlException e){
                System.debug('A DML exception has occurred: ' +e.getMessage());
                return null;
        }}
                else{
                    return null;
                }
    }
}
thanks rajeshkarreddy its working 
Martin CardarilliMartin Cardarilli
Hey guys, I just completed the task, this is my code (looking slightly different than yours, but working). 
 
public class AccountHandler {

    
    public static Account insertNewAccount(String VARAccountName){
        
        Account acct = new Account(Name=VARAccountName);
        
        try {
  
    // Insert the account 
    insert acct;
            return acct;
} catch (DmlException e) {
    System.debug('A DML exception has occurred: ' +
                e.getMessage());
    return null;
}
    }
    
    
}


But I'm looking yours, why are you doing "return null" two times? If we are already considering both scenarios
 
public static Account  insertNewAccount(string n){
            if(n !=''){  
            try{
            account a=new account ();
            a.Name=n;
               insert a;
            return a;
          }
    
          Catch (DmlException e){
                System.debug('A DML exception has occurred: ' +e.getMessage());
                return null;
        }}
                else{
                    return null;
                }
    }
}

Thank you and happy coding!