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
Marco PinderMarco Pinder 

Apex Test Fail - Please Help

Hi,

 

I have created a trigger on the CampaignMember object as below:

 

trigger CampaignMemberUpdate on CampaignMember (after insert, after update) {
    for (CampaignMember cm : trigger.new){
        Contact contact = [SELECT Id from Contact where id = :cm.ContactId];
        contact.Campaign_Member_Update_Date__c = cm.LastModifiedDate;
        upsert contact;
    }
}

 

This trigger works well and updates the Campaign_Member_Update_Date__c field as expected.

 

I am now writing the test method for the trigger and have created the following:

 

@isTest
private class CampaignMemberUpdateTest{
        static testMethod void CampaignMemberUpdateTest() {
            
            //Set up the Contact record.
            Contact c = new Contact(Firstname='Test',LastName='Test');
            insert c;
            
            //Set up the Campaign record.
            Campaign cg = new Campaign(Name='Test');
            insert cg;
            
            //Set up the CampaignMember record.
            CampaignMember cm = new CampaignMember(CampaignId=cg.id,ContactId=c.id);
            
            //Cause the Trigger to execute.
            insert cm;
            
            //Verify that the results are as expected.
            c = [select Id,Campaign_Member_Update_Date__c from contact where id = :cm.ContactId];
            System.assertEquals(cm.LastModifiedDate,c.Campaign_Member_Update_Date__c);
            }
        }

 

However, when i run this test I get an error at the 'System.assertEquals' line with the following message:

 

System.AssertException: Assertion Failed: Expected: null, Actual: 2011-09-08 12:30:31

 

I then tried to set a 'cm.LastModifiedDate' value when creating the CampaignMember record, however I am unable to save the test method as the 'cm.LastModifiedDate' field is not writeable.

 

Does anyone know how I can fix this please?

 

Thanks,

 

Marco 

Best Answer chosen by Admin (Salesforce Developers) 
Marco PinderMarco Pinder

Thanks very much Jake, that worked great.

 

For anyone that is interested, the final test method I created was:

 

@isTest
private class CampaignMemberUpdateTest{
        static testMethod void CampaignMemberUpdateTest() {
            
            //Set up the Contact record.
            Contact c = new Contact(Firstname='Test',LastName='Test');
            insert c;
            
            //Set up the Campaign record.
            Campaign cg = new Campaign(Name='Test');
            insert cg;
                                    
            //Set up the CampaignMember record.
            CampaignMember cm = new CampaignMember(CampaignId=cg.id,ContactId=c.id);
                     
            //Cause the Trigger to execute.
            upsert cm;
            
            //Verify that the results are as expected.
            Contact c2 = [select Id,Campaign_Member_Update_Date__c from contact where id = :cm.ContactId];
            CampaignMember cm2 = [select LastModifiedDate from CampaignMember where ContactId=:c.id]; 
            System.assertEquals(cm2.LastModifiedDate,c2.Campaign_Member_Update_Date__c);
            }
        }

All Answers

V'NathV'Nath

Campaign_Member_Update_Date__c   value is null in your test class , please assign value for it , when  you are  inserting  contact .  

 

 

Regards,

VNath.

Marco PinderMarco Pinder

Thanks for your reply.

 

I just tried that, however I still get the same error.

 

I changed the contact insertion code to:

 

//Set up the Contact record.
            Contact c = new Contact(Firstname='Test',LastName='Test',Campaign_Member_Update_Date__c=date.today());
            insert c;

 

Do you have any more ideas?

 

Regards,

 

Marco

 

 

Jake GmerekJake Gmerek

The last modified date on cm is never set on insert.  You are trying to verify that the last modified date and Campaign Member Update date are the same so you should do this:

 

 

 c = [select Id,Campaign_Member_Update_Date__c, LastModifiedDate from contact where id = :cm.ContactId];

 System.assertEquals(c.LastModifiedDate,c.Campaign_Member_Update_Date__c);

 

 

That will tell you if your trigger is working correctly.

Marco PinderMarco Pinder

Thanks for your reply.

 

I'm not sure I fully understand your suggestion though.

 

Do you mean that the cm.LastModifiedDate only gets a value on any subsequent edits to the cm record? I would have thought that it would equal the CreatedDate on insert?!

 

The trigger works when creating a new cm record because the Campaign_Member_Update_Date__c field on the contact record gets populated with the LastModifiedDate value from the cm record.

 

Also, in your select statement you are asking me to select the LastModifiedDate field from the contact record, yet that is not what I am interested in.

 

Can you elaborate please?

 

Regards,

 

Marco

Jake GmerekJake Gmerek

Nevermind I misread, what you need to do is requery the cm record so like this:

 

c = [select Id,Campaign_Member_Update_Date__c, from contact where id = :cm.ContactId];

cm = [select LastModifiedDate from CampaignMember where id = cm.id;

System.assertEquals(cm.LastModifiedDate,c.Campaign_Member_Update_Date__c);

 

I am fairly sure that that will work, but you may have to create a new object (i.e. cm1 or cmtest) to store the LastModifiedDate in.

 

Sorry about the confusion, that will teach me to respond without working all the way through the problem :).

 

 

Marco PinderMarco Pinder

Thanks very much Jake, that worked great.

 

For anyone that is interested, the final test method I created was:

 

@isTest
private class CampaignMemberUpdateTest{
        static testMethod void CampaignMemberUpdateTest() {
            
            //Set up the Contact record.
            Contact c = new Contact(Firstname='Test',LastName='Test');
            insert c;
            
            //Set up the Campaign record.
            Campaign cg = new Campaign(Name='Test');
            insert cg;
                                    
            //Set up the CampaignMember record.
            CampaignMember cm = new CampaignMember(CampaignId=cg.id,ContactId=c.id);
                     
            //Cause the Trigger to execute.
            upsert cm;
            
            //Verify that the results are as expected.
            Contact c2 = [select Id,Campaign_Member_Update_Date__c from contact where id = :cm.ContactId];
            CampaignMember cm2 = [select LastModifiedDate from CampaignMember where ContactId=:c.id]; 
            System.assertEquals(cm2.LastModifiedDate,c2.Campaign_Member_Update_Date__c);
            }
        }

This was selected as the best answer
Jake GmerekJake Gmerek

No problem