+ Start a Discussion
Akshay KopardeAkshay Koparde 

Not able to find the error in the below Trailhead/projects/QuickStart Apex/Add method to a class module.

This is a standard code from trailhead module when  I copy paste on developer console i get an error.

Trailhead Code
public static void updateOlderAccounts() {
    // Get the 5 oldest accounts
    Account[] oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];
    // loop through them and update the Description field
    for (Account acct : oldAccounts) {
        acct.Description = 'Heritage Account';
   }
   // save the change you made
   update oldAccounts;
}
I have manipulated the above code to debug the errors with below code statements
User-added image
Still not able to resolve the problem. Kindly requesting to please help me identifying the mistake and propbable solution.
Thanks!
Jayanth ThathapudiJayanth Thathapudi
Hi Akshay,
Follow the below steps
1.Create the class with name "OlderAccountsUtility"
2.Copy the below code insde the class
public static void updateOlderAccounts() {
    // Get the 5 oldest accounts
    Account[] oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];
    // loop through them and update the Description field
    for (Account acct : oldAccounts) {
        acct.Description = 'Heritage Account';
   }
   // save the change you made
   update oldAccounts;
}

your overall code should look like as below
public class OlderAccountsUtility {
public static void updateOlderAccounts() 
{
    Account[] oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];

	    // loop through them and update the Description field

	    for (Account acct : oldAccounts) {

	        acct.Description = 'Heritage Account';

	   }

	   // save the change you made
	   update oldAccounts;
}
}

Regards
Jay


Please mark as best answer if it helps you
 
sfdcMonkey.comsfdcMonkey.com
hi Akshay Koparde 
you got this error because  you try to write code in direct class body you can not write code in class body in the updateOlderAccounts class create a method and then save it
public class OlderAccountsUtility {
  // put your code in method  
  public static void updateOlderAccounts() {
    // Get the 5 oldest accounts
    Account[] oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];
    // loop through them and update the Description field
    for (Account acct : oldAccounts) {
        acct.Description = 'Heritage Account';
   }
   // save the change you made
   update oldAccounts;
}
    }

thanks
i hope it helps you let me inform if it helps you  :)