+ Start a Discussion
sooria narayanan Balasubramaniamsooria narayanan Balasubramaniam 

I am unable to complete Trailhead challenge for "Manipulating records with DML" chapter

User-added image

I am trying to insert an account (That is what required of me to complete the challenge). But it throws the error related to "Deletion". FYI, I will furnish the trailhead challenge and the code


Create a method for inserting accounts.
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.


CODE : 
public class AccountHandler {

    public static Account insertNewAccount(String a)
    {
        String aName=a;
        Account newAcc = new Account(Name=aName);
    try {            
            insert newAcc;
        }
        catch(DMLException e)
        {
            system.debug('A DML exception has occured' + e.getMessage());
            return null;
        }    
                return newAcc;
    }
               
}
Andy BoettcherAndy Boettcher
It looks as if either you have existing code (or Workflow/Flow/Process Builder) in your DE that is creating cases, or you created a case manually off of that account.

Best recommendation is to spin up a fresh DE for Trailhead exercises so you don't have any conflicting code.
Uvais KomathUvais Komath
public class AccountHandler {
public static Account insertNewAccount(String s)
{
     Account act = New Account();
        act.Name = s;
    try {
  
        insert act;
        }
    catch (DmlException e) {

	   return null;
                           }
    return act;
}
}

Might be problem with org.
Above code worked for me
 

If the problem persist check whether you have any account named "My Test Account"

and any cases related. if so delete it and retry.

 

 

Tawseef Wani 24Tawseef Wani 24
There is a little modification in your code :
return newAcc should be in try block .. It should work now ,,
Thank You..

public class AccountHandler {

    public static Account insertNewAccount(String a)
    {
        String aName=a;
        Account newAcc = new Account(Name=aName);
    try {            
            insert newAcc;
            return newAcc;
        }
        catch(DMLException e)
        {
            system.debug('A DML exception has occured' + e.getMessage());
            return null;
        }    
             
    }
               
}