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
Ezhil Robert 6Ezhil Robert 6 

Trigger between 2 independent objects but fields are same.

Hi Experts,

I have a requirement to update CampaignMember field by comparing a custom object which is not link to Campaign Member table. But the fields are same in both the objects.

My scenario is when the campaign, contact and status is added or updated in custom object, the latest entry with the status should be updated in CampaignMember table against by comparing CampaignId, ContactID and Status of Campaign Member.

The below is my code.


Trigger updateCampaignStatus on Responses__c (before update,after insert, after delete) {
 
  if(trigger.isinsert && trigger.isbefore && trigger.isupdate){
  map<id,Responses__c> mapcam = trigger.newmap;
  list<CampaignMember> resp = new list<CampaignMember>();
  list<CampaignMember> cam = [select id, CampaignId, Contactid, Status from CampaignMember where CampaignId in : mapcam.keyset()];
  for(CampaignMember c : cam){
   c.Status = mapcam.get(c.Id).Response_Status__c;
  // c.SystemModstamp = mapcam.get(c.Id).LastModifiedDate;
   resp.add(c);
   
   insert resp;

  }
  update resp;
}
  if(trigger.isafter && trigger.isdelete){
   map<id,Responses__c> delres = trigger.oldmap;
   list<CampaignMember> ccc = [select id, CampaignId,Contactid, Status from CampaignMember where CampaignId in : delres.keyset()];
   delete ccc;
  
 
  }
}


But it is not updating the status of CampaignMember by comparing the CampaignID, Contact ID.

Your response will be highly appreciated,

Thanks in advance.

 
Veena Sundara-HeraguVeena Sundara-Heragu
Hi,

There are quite a few issues here:

1. on Responses__c (before update,after insert, after delete) - Why is is that you want to do this "before" update but "after" for insert and delete ?
Generally, if we are updating another object, we should do it in "after trigger" so I would change "before update" to "after update"

2.  if(trigger.isinsert && trigger.isbefore && trigger.isupdate) - This will NEVER be true so that block of code will not execute at all.  I think you need to change this to : if(trigger.isafter && (trigger.isinsert || trigger.isupdate))

3. mapcam is a map of the object Responses__c.
So, the statment "select id, CampaignId, Contactid, Status from CampaignMember where CampaignId in : mapcam.keyset()" is incorrect because you are trying to get records from the CampaignMember records using a list of Responses__c ids.
you need to collect the campaignids from the Response__c objects into a list or set and use that to query against CampaignMember like this:
Set<Id> setCampaignIds = new Set<Id>();
for(Response__c resp : trigger.new)
{
   setCampaignIds.add(resp.CampaignId__c); // or whatever that lookup field is called in your response__c object
}
list<CampaignMember> cam = [select id, CampaignId, Contactid, Status from CampaignMember where CampaignId in : setCampaignIds];

You need to do something similar for deletes as well.

4. You are performing this on every insert/update/delete.  For update, it would be a good idea to check if the Status or Contact field has actually changed

5. The for loop has an insert inside it and update outside it.  I am not sure what you are trying to do there.  You should not be inserting the list of CampaignMember records every time you add one to the list.

for(CampaignMember c : cam){
   c.Status = mapcam.get(c.Id).Response_Status__c;
  // c.SystemModstamp = mapcam.get(c.Id).LastModifiedDate;
   resp.add(c);
   
   insert resp;

  }
  update resp;

Hope this gives you enough to get started