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
randombardrandombard 

Test Cass contact Account ID

Hi All,

 

My apologies for not learning more I am gradually learning Apex but I have hit an error and I am hoping its something simpler then I think it seems to be saying.

 

I am getting the following error:

 

Error: Compile Error: Invalid initial expression type for field Account, expecting: SOBJECT:Account (or single row query result of that type) at line 20 column 11

 

Code

 

@isTest
Public class TestMigrateRelatedOb{
static testMethod void myPage_Test()
{

//Test converage for the myPage visualforce page

Account newAccount = new Account (name='XYZ Organization',
BillingCity ='TestCity',
BillingCountry ='TestCountry',
BillingStreet ='TestStreet',
BillingPostalCode ='t3stcd3'
);

insert newAccount;

Contact NewContact = new Contact (
FirstName = 'xyzFirst',
LastName = 'XyZLast',
Account = newAccount.id,
Email = 'xyzmail@mail.com'
);

Insert NewContact;

Task NewTask = new Task (Who = NewContact.id,
Subject = 'TestTask',
ActivityDate = date.today()
);

Insert NewTask;

Event NewEvent = new Event (Who = NewContact.id,
Subject = 'TestEvemt',
ActivityDate = date.today()
);

Insert NewEvent;

Lead NewLead = new Lead (Company = 'XYZ Organization',
FirstName = 'xyzFirst',
LastName = 'XyZLast',
Email = 'xyzmail@mail.com',
Old_Contact_ID__c = NewContact.ID
);

Insert NewLead;
}
}

 

 

Now I am hoping that the error doeans meen I have to do and SOQL query to get the ID of the new account when inserting the new contact for the test.

 

Thanks for any help.

 

R

Best Answer chosen by Admin (Salesforce Developers) 
Rajesh SriramuluRajesh Sriramulu

Hi

 

As u show two method, the second one is correct that will insert the task because it is lookup soit will take ID of Contact or Lead.

 

Task NewTask = new Task (WhoID = NewContact.id,
Subject = 'TestTask',
ActivityDate = date.today()
);

insert NewTask;

 

Event NewEvent = new Event (WhoId = NewContact.id,
Subject = 'TestEvemt',
ActivityDate = date.today()
);
insert NewEvent;

 

This will be done.

 

 

Regards,

Rajesh.

All Answers

Rajesh SriramuluRajesh Sriramulu

Hi

 

Try to insert like this

 

Contact NewContact = new Contact (
FirstName = 'xyzFirst',
LastName = 'XyZLast',
Account = newAccount ,
Email = 'xyzmail@mail.com'
);

 

 

Regards,

Rajesh.

randombardrandombard

Thanks Rajesh,

 

Now its falling over on the Who for the New Task and New Event.

 

I have changed them to WHOID as I noticed I has done that wrong but with both of the following I get :

 

Error: Compile Error: Invalid initial expression type for field Task.WhoId, expecting: Id at line 23 column 34

 

Method 1

 

Task NewTask = new Task (WhoID = NewContact,
Subject = 'TestTask',
ActivityDate = date.today()
);

 

Method 2

 

Task NewTask = new Task (WhoID = NewContact.id,
Subject = 'TestTask',
ActivityDate = date.today()
);

Thanks again.

R

Rajesh SriramuluRajesh Sriramulu

Hi

 

As u show two method, the second one is correct that will insert the task because it is lookup soit will take ID of Contact or Lead.

 

Task NewTask = new Task (WhoID = NewContact.id,
Subject = 'TestTask',
ActivityDate = date.today()
);

insert NewTask;

 

Event NewEvent = new Event (WhoId = NewContact.id,
Subject = 'TestEvemt',
ActivityDate = date.today()
);
insert NewEvent;

 

This will be done.

 

 

Regards,

Rajesh.

This was selected as the best answer