You need to sign in to do that
Don't have an account?

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;
}
}
}

Throwing this error, how many times i modified the class.
what is the correct class.?
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;
}
}
}
Throwing this error, how many times i modified the class.
what is the correct class.?
<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
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.
<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>
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"
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.
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.
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?
@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;
}
}
User 'acc' or something else.
Check if it helps.
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...
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
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
Hey, Thanks Buddy!
It worked without any error...
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;
}
}
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;
}
}
}
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.
Who is facing problems here only go for Account trigger section and comment that triggers that you have, one of them is broke
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
But I'm looking yours, why are you doing "return null" two times? If we are already considering both scenarios
Thank you and happy coding!