You need to sign in to do that
Don't have an account?
spoorthisri 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.
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.
Setup-->Apex Classess
Then try your challenage again
All Answers
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?
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.
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
Setup-->Apex Classess
Then try your challenage again
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;
}
}
}
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
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');
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');
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.
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;
}
}
}
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.
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');
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');
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');
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
Can somebody help me on this line public static Account insertNewAccount(String Name) {
why we have to use Account name after static keyword ?
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;
}
}
}