You need to sign in to do that
Don't have an account?
pranoti kokas 27
unexpected token: 'void' error while executing trailhead code
Hello Friends,
I was going through the trailhead and happened to execute the below 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;
}
But its showing error on line 1 - unexpected token: 'void'
Can anyone please help?
Thanks in advance!
I was going through the trailhead and happened to execute the below 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;
}
But its showing error on line 1 - unexpected token: 'void'
Can anyone please help?
Thanks in advance!
Please find the below code:
Go to Setup --> Apex Classes --> New and paste the above code.
Please do let me know if it helps you.
Regards,
Mahesh
Can you please explain why you did the first step?
Thanks in Advance!
Whenever you want to write any methods, it should be inside the class. A class contains member variables and methods.
Hence I have written the class at the top level and included the method inside.
Regards,
Mahesh
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;
}
still error....
Error: Compile Error: Illegal assignment from List<Account> to List<Account> at line 4 column 19
The 'updateOlderAccounts' method did not update account records as expected"
even if i did copy/paste!!
No need to create any other classes
Create an Apex Class mentions the class name which is missing in the next section Add a Method to the Class where you need to add the class name .