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
Olivia CannonOlivia Cannon 

Test class error: Invalid initial expression type for field

Hi,

I'm writing a trigger which copies data from the Campaign Member to its Campaign (as cross object workflows don't work from Campaign Members).

I've hit a problem with my test class that I can't get around:

Invalid initial expression type for field Campaign, expecting: SOBJECT:Campaign (or single row query result of that type)

The error is on Line 40, which says:

        CampaignMember cm = new CampaignMember(Campaign = cam.Id, Contact = c.Id, Send_tutor_details_to_client__c = 'First');

This is the whole of the test class:

@isTest 
public class TestUpdateTutor1onCampaign{

        static testMethod void TestUpdateTutor1onCampaign() {
       // create an Account, a Tutor, a Job and a Job Member
  
       Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
       User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
       EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
       LocaleSidKey='en_US', ProfileId = p.Id, 
       TimeZoneSidKey='America/Los_Angeles', UserName='4323f1a8-2698-44c4-a234-dfcf80fa6a6c@testorg.com');
        
       test.StartTest();
       insert u;
       u = [SELECT Id FROM User WHERE Alias = 'standt' AND Email='standarduser@testorg.com'];
      
       Account acc = new Account(Name = 'Test Account');
       insert acc;

       acc = [SELECT Id FROM Account WHERE Name = 'Test Account'];
       
       //Get the Contact record types
       List<RecordType> listRecordTypes = [Select Name, Id From RecordType where sObjectType='Contact' and isActive=true];
       Map<String,String> mapRecordTypes = new Map<String,String>();
       for(RecordType rt: listRecordTypes) {
           mapRecordTypes.put(rt.Name,rt.Id);
       }      

       Contact c = new Contact(AccountID = acc.Id, FirstName = 'Test', LastName = 'Contact', Main_Profile__c = 'www.google.com',
                               RecordTypeId = mapRecordTypes.Get('Tutor') 
                               );
        insert c;
        c = [SELECT Id FROM Contact WHERE FirstName = 'Test'];
        
        Campaign cam = new Campaign(Name = 'Test Job');
        insert cam;
        cam = [SELECT Id FROM Campaign WHERE Name = 'Test Job'];
            
        // insert a Campaign Member
        CampaignMember cm = new CampaignMember(Campaign = cam.Id, Contact = c.Id, Send_tutor_details_to_client__c = 'First');
        insert cm;
        cm = [SELECT Id FROM CampaignMember WHERE Contact =: c.Id AND Send_tutor_details_to_client__c = 'First' ];
        
        system.assertEquals(cam.Tutor_1__c, cm.Tutor_details_for_email__c);
        
        List<Campaign> camtest = [SELECT Id FROM Campaign WHERE Tutor_1__c =: cm.Tutor_details_for_email__c];
        
        system.assertEquals(1, camtest.size());

       test.StopTest();
    }

}

Any help would be greatly appreciated!

Best Answer chosen by Olivia Cannon
Vinit_KumarVinit_Kumar
Change your code from 

CampaignMember cm = new CampaignMember(Campaign = cam.Id, Contact = c.Id, Send_tutor_details_to_client__c = 'First');

to

CampaignMember cm = new CampaignMember(CampaignId = cam.Id, ContactId = c.Id, Send_tutor_details_to_client__c = 'First');

All Answers

CheyneCheyne
Try referencing the CampaignId and ContactId fields instead of Campaign and Contact:

CampaignMember cm = new CampaignMember(CampaignId = cam.Id, ContactId = c.Id, Send_tutor_details_to_client__c = 'First');
Vinit_KumarVinit_Kumar
Change your code from 

CampaignMember cm = new CampaignMember(Campaign = cam.Id, Contact = c.Id, Send_tutor_details_to_client__c = 'First');

to

CampaignMember cm = new CampaignMember(CampaignId = cam.Id, ContactId = c.Id, Send_tutor_details_to_client__c = 'First');
This was selected as the best answer