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
rachel watsonrachel watson 

Trailhead- Manipulating Records with DML

I am new to coding and do not have a background with OO languages.  I am trying to pass the Manipulating Records with DML module and keep getting errors.  Please help.

The following is my code:

public class AccountHandler {
    public static ID insertNewAccount(String MyStr);{
      try 
      {
            
          Account acct = new Account(Name=myStr);
            insert acct;
            
            return (acct.id);
      }
          
     Catch (DMLException e) {
      System.debug('A DML exception has occurred: '  +
                  e.getMessage());
                  Return('NULL');
    }
  }    


I am receiving these 2 errors in line 2:
unexpected token: 'List'
Method must define a body

 
Jithin Krishnan 2Jithin Krishnan 2

Hi Rachel,
The problem is you have put a ';' in the line 2. please try after removing the semicolon. 
public class AccountHandler {
    public static ID insertNewAccount(String MyStr)
{
      try  {
            
          Account acct = new Account(Name=myStr);
            insert acct;
            
            return (acct.id);
      }
          
     Catch (DMLException e) {
      System.debug('A DML exception has occurred: '  +
                  e.getMessage());
                  return null;
    }
  }    
}


Please let me know if this helped you.
Thanks!

 
rachel watsonrachel watson
I do not see a ';' in line 2.  I see '()".  What ';' are you referring to?  
Jithin Krishnan 2Jithin Krishnan 2
I removed the semicolon in the code I posted. Your code has a semicolon (I have added space in your code to understand more clearly) below,

public class AccountHandler {
    public static ID insertNewAccount(String MyStr)              ;                  {   //The semicolon here is what I am refering to. 
      try 
      {
            
          Account acct = new Account(Name=myStr);
            insert acct;
            
            return (acct.id);
      }
          
     Catch (DMLException e) {
      System.debug('A DML exception has occurred: '  +
                  e.getMessage());
                  Return('NULL');
    }
  }    


Thanks!
Jithin Krishnan 2Jithin Krishnan 2
And also the return statement in the catch block should be:
return null;
instead of:
Return('NULL');
rachel watsonrachel watson
Now it is saying that I have an unexpected town on line 4: 'try'???
Abhishek BansalAbhishek Bansal
Hi Rachel,

Please try with below code :
 
public class AccountHandler {
    public static ID insertNewAccount(String MyStr){
		try {    
			Account acct = new Account(Name=myStr);
			insert acct;
					
			return acct.id;
		}
			  
		Catch (DMLException e) {
			System.debug('A DML exception has occurred: '  + e.getMessage());
			return null;
		}
	}    
}
Let me know if you have any issue.

Thanks,
Abhishek
 
Jithin Krishnan 2Jithin Krishnan 2
Hi Rachel,
Sorry I missed the return statement in try block. 

return (acct.id);
should be:
return acct.id;   //without the brackets

I have corrected the code. Please try this:
public class AccountHandler {
    public static ID insertNewAccount(String MyStr)
{
      try  {
            
          Account acct = new Account(Name=myStr);
            insert acct;
            
            return acct.id;
      }
          
     Catch (DMLException e) {
      System.debug('A DML exception has occurred: '  +
                  e.getMessage());
                  return null;
    }
  }    
}

Thanks!