+ Start a Discussion
Nitish KulkarniNitish Kulkarni 

Invalid constructor syntax, name=value pairs can only be used for SObjects: contact

// Create a list of contacts
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()};
            
// Bulk insert all contacts with one DML call
Database.SaveResult[] srList = Database.insert(conList, false);
// Iterate through each returned result
for (Database.SaveResult sr : srList) {
    if (sr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully inserted contact. Contact ID: ' + sr.getId());
    } else {
        // Operation failed, so get all errors
        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());
	 }
    }
}
I am getting invalid constructor syntax error in anonymous apex. this code is from trailhead in which database method is used.
 
Best Answer chosen by Nitish Kulkarni
cvuyyurucvuyyuru
Hi Nitish,
Just check if you have an apex class named Contact.
If so delete that class or rename it.

All Answers

cvuyyurucvuyyuru
Hi Nitish,
Just check if you have an apex class named Contact.
If so delete that class or rename it.
This was selected as the best answer
Nitish KulkarniNitish Kulkarni
thanks charan its working now.
sahiti@sfdcsahiti@sfdc
Hi Nitish, I came across the same error but couldn't understand how to overcome it! Can you tell me what change you did to the code. I haven't created any class just in the Anonymous window I tried to execute the code
 
cvuyyurucvuyyuru
Hi Sahiti,
Was it fixed? What issue your are getting exactly?
Nitish KulkarniNitish Kulkarni
Hi sahiti I didn't changed anything in code.I created a class with name Contact that's why that error occurred in my case.Check if u have any class with name Contact , if not then please copy paste your code here and the error message you are getting.