function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AngelikaAngelika 

Apex Test Method Help: Error Expression cannot be assigned at line -1 column -1

I have created a an Apex Birthday Scheduler that sends an email to a manager when a employees birthday is two days away. I have run tests in sandbox and my code works. 

 

I am currently building a test method. I get the error,  Error: Compile Error: Expression cannot be assigned at line -1 column -1 when I try to save my code.

 

This confuses me because my code ran before I added the test method. Why is the compiler saying there is an error at line -1 column -1...before my code? How is that even possible? 

 

Here is my code With test method:

 

global class BirthdayName implements Schedulable{ 
global void execute (SchedulableContext ctx) 
{ 

sendBirthdayEmail(); 

}
public void sendBirthdayEmail()
{

   for(Contact con : [SELECT name, Id, Birthdate FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2) AND Account.Name = 'Projetech Inc.'])
 
  {
  String conId = con.Id;
  String conName = con.name;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
//mail.setTemplateId('00XJ0000000M31w'); 

mail.setTargetObjectId('005J0000000JWYx');
// mail.setToAddresses('');
mail.setsubject('Birthday Reminder');
mail.setHtmlBody('This is a scheduler-generated email to notify you that the birthday of Name:  <b>  ' + con.name + '  </b> is in two days. Birthdate of: ' + con.Birthdate + '. Please wish them a happy Birthday.');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail }); 
}

}

static testMethod void myTestBirthday() {

//create the required test data needed for the test scenario
//In this case, I need to create a new contact, with the account name of Projetech and has a birthday 2 days away

Contact testContact = new Contact();
testContact.firstName='Jack';
testContact.lastName='Dell';
insert testContact;

testContact.birthdate=system.Today().addDays(2);

update testContact;

//verify that the birthdate field was updated in the database

Contact updatedContact = [SELECT  birthdate FROM Contact WHERE Id =:testContact.Id];
System.assertEquals(system.Today().addDays(2) , updatedContact.birthdate);

// do the same for account name

Account.name='Tests.';
update testContact;

Contact nextUpdatedContact = [SELECT Account.name FROM Contact WHERE Id =:testContact.Id];
System.assertEquals(system.Today().addDays(2) , nextupdatedContact.birthdate);

}



}

 

 

 

 

sfdcfoxsfdcfox
Account.name='Projetech Inc.';

You're trying to assign a value to Schema.Account.Name, which is invalid, as that is a read-only system value. You need to create an account variable, create the account, assign that to the contact's accountid field, and use that variable instead.

 

 

AngelikaAngelika

How do I create an account variable? How do I link th new account that I just made with the contact I created?

 

Would it be:

 

testAccount.name = testContact.id;

 

Here are the changes I made( to create an account)

 

Account testAccount = new Account(name='Projetech Inc.');

 

insert testAccount;

 

Full Code

 

 

 

AngelikaAngelika

Full Revised Code:

 

static testMethod void myTestBirthday() {

//create the required test data needed for the test scenario
//In this case, I need to create a Projetech test account, new contact and has a birthday 2 days away

Account testAccount = new Account(name='Projetech Inc.');
insert testAccount;

testAccount.name = testContact.id;

Contact testContact = new Contact();
testContact.firstName='Jack';
testContact.lastName='Dell';
insert testContact;

testContact.birthdate=system.Today().addDays(2);

update testContact;

//verify that the birthdate field was updated in the database

Contact updatedContact = [SELECT  birthdate FROM Contact WHERE Id =:testContact.Id];
System.assertEquals(system.Today().addDays(2) , updatedContact.birthdate);

// do the same for account name

//Account.name='Projetech Inc.';
//update testContact;

//Contact nextUpdatedContact = [SELECT Account.name FROM Contact WHERE Id =:testContact.Id];
//System.assertEquals(system.Today().addDays(2) , nextupdatedContact.birthdate);



}

 

 

Anoop AsokAnoop Asok

hi,

This will link your account and contact.

 

//testAccount.name = testContact.id; -> NOT REQUIRED

Contact testContact = new Contact();
testContact.AccountId = testAccount.id; // NEW STATEMENT
testContact.firstName='Jack';
testContact.lastName='Dell';
insert testContact;

 

Thanks,

Anoop