3

I am trying to write to simple apex but it is giving me Unexpected token:{ error. this is the first time apex class is being executed in my developer org. I am getting similar error for any class I have written.

I opened this anonymous execution mode in developer console - tried this one from trailhead.

List<Vehicle__c> v={SELECT Id, Name from Vehicle__c where Name='X'};
v[0].Name ='Y';
upsert v;
1

You need to write your query. In query we don't use curly brackets. Curly brackets are used in method body declareation.

List<Vehicle__c> v=[SELECT Id, Name from Vehicle__c where Name='X']; 
v[0].Name ='Y'; 
upsert v;

But In this you might get list index out of bound exception. So you should check list size before using it something like.

List<Vehicle__c> v=[SELECT Id, Name from Vehicle__c where Name='X']; 
if(v.size() > 0)
{
    v[0].Name ='Y'; 
    upsert v;

}
1
  • 2
    Except, this code would only update the first record in the list, which is also probably not what you want.
    – sfdcfox
    Sep 17 '16 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.