+ Start a Discussion
Danny HartleyDanny Hartley 

unexpected token '<' in trailhead

I am receiving this error:

"Line: 1, Column: 5
unexpected token: '<'"

when executing this code:

List<Contact> conList = new List<Contact> {
    new Contact(FirstName='Joe', LastName='Smith', Department='Finance'),
    new Contact(FirstName='Kathy', LastName='Smith', Department='Technology'),
    new Contact(FirstName='Caroline', LastName='Roth', Department='Finance'),
    new Contact();
}

Database.SaveResult[] srList = Database.insert(conList, false);

for (Database.SaveResult sr : srList) {
    if(sr.isSuccess()) {
        System.debug('Successfully inserted contact. Contact ID: ' + sr.getID());
    } else{
        for(Database.Error err : sr.getErrors()) {
            System.debug('The following error has occurred.');
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Contact fields that affected this error: ' + err.getFields());
        }
    }
}

in the execute anonymous window in the developer console (this is a trailhead exercise). Thanks in advance for any help.
James LoghryJames Loghry

Not sure if this is the only issue, but looks like the very first semi-colon is in the wrong place.  Try the following:

 

List<Contact> conList = new List<Contact> {
    new Contact(FirstName='Joe', LastName='Smith', Department='Finance'),
    new Contact(FirstName='Kathy', LastName='Smith', Department='Technology'),
    new Contact(FirstName='Caroline', LastName='Roth', Department='Finance'),
    new Contact()
};

Database.SaveResult[] srList = Database.insert(conList, false);

for (Database.SaveResult sr : srList) {
    if(sr.isSuccess()) {
        System.debug('Successfully inserted contact. Contact ID: ' + sr.getID());
    } else{
        for(Database.Error err : sr.getErrors()) {
            System.debug('The following error has occurred.');
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Contact fields that affected this error: ' + err.getFields());
        }
    }
}
Rahul Jain 152Rahul Jain 152
Just you have to put semicolon on correct place , please refer below code i am execute and check this one  .
List<Contact> conList = new List<Contact> {
    new Contact(FirstName='Joe', LastName='Smith', Department='Finance'),
    new Contact(FirstName='Kathy', LastName='Smith', Department='Technology'),
    new Contact(FirstName='Caroline', LastName='Roth', Department='Finance'),
    new Contact()
};

Database.SaveResult[] srList = Database.insert(conList, false);

for (Database.SaveResult sr : srList) {
    if(sr.isSuccess()) {
        System.debug('Successfully inserted contact. Contact ID: ' + sr.getID());
    } else{
        for(Database.Error err : sr.getErrors()) {
            System.debug('The following error has occurred.');
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Contact fields that affected this error: ' + err.getFields());
        }
    }
}

 
Wilson Busaka 3Wilson Busaka 3
Hi guys,

I'm also receiving a similiar error in ananymous window.
Can someone please help me figure out what doing wrong? Below is my code
List<Program__c> listitem= [SELECT Id, Name, Description__c FROM 
                                   Program__c WHERE Program_Status__c='Active']

Thanks
Rahul Jain 152Rahul Jain 152
HI Wilson ,

Syntax is corect > this is working just changed object name from your code .Try onemore time .

List<Account> listitem= [SELECT Id, Name FROM 
                                   Account ];

Thanks & Regards
Rahul Jain 
Suraj Tripathi 47Suraj Tripathi 47
Hi Danny Hartley,

You have the semicolon in wrong place.
You can try the below code.
List<Contact> conList = new List<Contact> {
            new Contact(FirstName='Joe', LastName='Smith', Department='Finance'),
                new Contact(FirstName='Kathy', LastName='Smith', Department='Technology'),
                new Contact(FirstName='Caroline', LastName='Roth', Department='Finance'),
                new Contact()
        };
        
        Database.SaveResult[] srList = Database.insert(conList, false);
        
        for (Database.SaveResult sr : srList) {
            if(sr.isSuccess()) {
                System.debug('Successfully inserted contact. Contact ID: ' + sr.getID());
            } else{
                for(Database.Error err : sr.getErrors()) {
                    System.debug('The following error has occurred.');
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Contact fields that affected this error: ' + err.getFields());
                }
            }
        }

In case you find any other issue please mention. 
If you find your Solution than mark as this as a best answer. 
Thanks and Regards
Suraj Tripathi.