+ Start a Discussion
pranoti kokas 27pranoti 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!
 
Mahesh DMahesh D
Hi Pranoki,

Please find the below code:
 
public class OlderAccountsUtility {
	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;
	}
}

Go to Setup --> Apex Classes --> New and paste the above code.

Please do let me know if it helps you.

Regards,
Mahesh
pranoti kokas 27pranoti kokas 27
Yes! It did work! Thank you!
Can you please explain why you did the first step?

Thanks in Advance!
Mahesh DMahesh D
Hi Pranoti,

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
pranoti kokas 27pranoti kokas 27
Thank you Mahesh!
 
Pratyush Kumar 44Pratyush Kumar 44
Hi,
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
Christophe_         LereverendChristophe_ Lereverend
Thanks Mahesh! Works perfectly
Benoit VAQUEZBenoit VAQUEZ
Hello, Now that i saw this, i need your help for the same exercise! When i want to verify, it sais: "Challenge Not yet complete... here's what's wrong: 
The 'updateOlderAccounts' method did not update account records as expected" 

even if i did copy/paste!!
Octavian-Teodor AxinteOctavian-Teodor Axinte
I am having the same issue as Benoit. I have copied and pasted the code perfectly but I still get the message: "The 'updateOlderAccounts' method did not update account records as expected" "
Dirk AgterhuisDirk Agterhuis
The exercise suggests to add the method you're creating inside the body of the class: between the braces {}, like so:
 
public class AttachmentController {
    
@AuraEnabled
    public static void updatePicturePath(String recId){
        //In Lightning Experience, Attachments are stored in ContentDocuments
        ContentDocumentLink docLink = [ SELECT ContentDocumentId
                               FROM ContentDocumentLink
                               WHERE LinkedEntityId = :recId order by Id desc Limit 1];
        //ContentVersion Id uniquely identifies the attachment
		ContentVersion ver = [SELECT Id FROM ContentVersion Where ContentDocumentId = :docLink.ContentDocumentId];
        //Update the Picture_Path field with the url of the image
        Speaker__c speaker = [SELECT Id FROM Speaker__c WHERE Id = :recId];
        speaker.Picture_Path__c = '/sfc/servlet.shepherd/version/download/'+ ver.Id;
        upsert speaker;
    }
    
}

No need to create any other classes