+ Start a Discussion
Rica SaportaRica Saporta 

Troubles with Trailhead challenge: Apex Basics & DB, Manipulating Records with DML

hello all,
i have having issues with this challange and am not sure how to get past them ...below is a screen capture of the challange and my latest error.Challenge and issue

here is the code i have ...
==================
public class AccountHandler {
    
    public static Account insertNewAccount(String AcctName) {
        
        Account acct = new Account();
        if (AcctName <> ''){
            acct.Name = AcctName;
            return acct;
        } else {
            return Null;
        }
    }
}
===============

i have already started a new Development Environment to try and get rid of the "org" related issue, but this did not help .. obviously i am doing something very basic wrong ... 
HELP
thank you
rica
Best Answer chosen by Rica Saporta
Amit Chaudhary 8Amit Chaudhary 8
Please check below post.
1) https://developer.salesforce.com/forums/?id=906F0000000B37aIAC
2) https://developer.salesforce.com/forums/?id=906F0000000BH6yIAG

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 name) {
Account a = new Account();
a.Name = name;
try
{
insert a;
} catch (Exception e) {
return null;
}
return a;
}
}
NOTE:- your DML should be inside Try Catch.

Let us know if this will help you

Thanks
Amit Chaudhary
 

All Answers

DeepthiDeepthi (Salesforce Developers) 
Hi Rica,

Please check the below code:
public class AccountHandler {
    public static Account insertNewAccount(String n){
        if(n!=''){
            Account acc = new Account(Name=n);
            try{  
                insert acc;
               }
            catch(DmlException e){
                System.debug('Error Message is:'+e.getMessage());
            }
            return acc;
        }
        else
            return null;
    }
}


Hope this helps you!
Best Regards,
Deepthi
Amit Chaudhary 8Amit Chaudhary 8
Please check below post.
1) https://developer.salesforce.com/forums/?id=906F0000000B37aIAC
2) https://developer.salesforce.com/forums/?id=906F0000000BH6yIAG

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 name) {
Account a = new Account();
a.Name = name;
try
{
insert a;
} catch (Exception e) {
return null;
}
return a;
}
}
NOTE:- your DML should be inside Try Catch.

Let us know if this will help you

Thanks
Amit Chaudhary
 
This was selected as the best answer
Rica SaportaRica Saporta
hi guys - i tried the code from both Deepthi and Amit, and got the following error from both ...

==========
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
==========

not sure what to do next - it's the "Attempt to de-reference a null object" that is really throwing me off
Rica SaportaRica Saporta
Hi gang ...
i tried again and this time made sure i was pointing to the right DE (i.e. the new one) and this time the code provided by Amit worked ...
thank you kindly for the assistance
rica