+ Start a Discussion
woodmanzeewoodmanzee 

Trigger Issues - are the records saving?

I am having a few issues with this trigger I am writing.  I thought it would be pretty simple, but apparently I'm not good enough at apex.  I'm trying to update a custom date field (Last_Campaign_Date__c) in the Contact standard object that holds the date of the most recent campaign they were a member of.  When I run the test, my debug statements say that the date was updated, but my assertion still fails.  I don't really know why it isn't saving.  Anyone have ideas?

 

Trigger Code:

trigger addLastCampaignDateForMembers on Campaign (before insert, before update) {

    // do this for each camapign in the set affected by trigger
    for(Campaign current : Trigger.new) {
    
        // only update contacts in the campaign
        if(current.NumberOfContacts > 0) {
            ID campaign_id = current.Id;
            
            // get all contacts for this campaign
            Contact[] members = [
                select id, firstname, lastname, Last_Campaign_Date__c
                from contact 
                for update
            ];
                
            // update the Last Campaign Date for each contact
            for(Contact c : members) {
                //system.debug(c.Last_Campaign_Date__c);
                c.Last_Campaign_Date__c = current.StartDate;
                //system.debug(c.Last_Campaign_Date__c);
                try {
                    update c;
                    system.debug(current.StartDate);
                    system.debug(c.Last_Campaign_Date__c);
                }
                catch(DMLException e) {
                    system.debug(e);
                }
            }
           
        }
    }
}

And here is my test code:

 

@isTest
private class TestLastCampaignDateUpdate {
    
    static testMethod void testDateChange() {
        Campaign[] campaign = [
            select id, name
            from Campaign
            where parentid = '701S00000009Qkq'
        ];
        
        CampaignMember[] cmembers = [
            select id, contactid, campaignid
            from CampaignMember
            where campaignid in :campaign
        ];
        
        Contact[] members = [
            select id, firstname, lastname, Last_Campaign_Date__c
            from contact 
            where id in (select contactId from campaignMember where campaignId in :campaign)
        ];
        
        system.debug(members[0].lastname);
        
        update campaign;
        system.debug(members[0].Last_Campaign_Date__c);
        system.assert(members[0].Last_Campaign_Date__c != null);
    }

}

 Thanks!

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I suspect the underlying issue is that the records don't update in your cached list, rather you have to requery them.

 

E.g.

 

 

 Contact[] members = [
            select id, firstname, lastname, Last_Campaign_Date__c
            from contact 
            where id in (select contactId from campaignMember where campaignId in :campaign)
        ];
        
system.debug(members[0].lastname);
        
update campaign;

members = [
            select id, firstname, lastname, Last_Campaign_Date__c
            from contact 
            where id in (select contactId from campaignMember where campaignId in :campaign)
        ];
        
system.debug(members[0].Last_Campaign_Date__c);
system.assert(members[0].Last_Campaign_Date__c != null);

 

 

As an aside, this section will retrieve all contacts rather than just the ones affected by the campaign:

 

 

// get all contacts for this campaign
            Contact[] members = [
                select id, firstname, lastname, Last_Campaign_Date__c
                from contact 
                for update
];

 

Finally, it would be better to store the updated contacts in a list and update them in one go rather thanwhen updating each one.