I am playing around SFDC and wrote a trigger to count the total Contacts in the system. I wrote the test class and when I am trying to save the class I get the following error:

Error: Compile Error: Expression cannot be assigned at line -1 column -1

This is my test class:

public class TestCountContacts{
  static testMethod void TestCountcontacts(){

    Contact newContact = new Contact();

    Contact.FirstName = 'John';
    Contact.LastName = 'Jones';

    insert newContact;

    List<Contact> total = [SELECT Id From Contact];


You need to use the instance variable not the class!

Contact.FirstName = 'John';
Contact.LastName = 'Jones';

Should be:

newContact.FirstName = 'John';
newContact.LastName = 'Jones';
  • 1
    I must headout of the office right now!! Dam...Thank you
    – Lut
    Sep 23 '14 at 1:05
  • Note that you could use the Schema.SObjectField reference (i.e. class variable) like so: newContact.put(Contact.FirstName, 'John'), which I do frequently in utility classes.
    – Adrian Larson
    Mar 27 '15 at 17:32

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.