• Ezhil Robert 6
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
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.

 
Hello all, I am pretty new to Apex classes, triggers, apex reports and visual force dashboards. Can anyone please give me an example of reports and dashboards using Apex code, or visual force. 

Your reply is highly appreciated 

Thanks,
Hello all, I am pretty new to Apex classes, triggers, apex reports and visual force dashboards. Can anyone please give me an example of reports and dashboards using Apex code, or visual force. 

Your reply is highly appreciated 

Thanks,

Hi all,

 When I merge account, I want to get the deleted record id and update a field value for the newly merged record.

 

 

trigger accountMerge on Account (after delete) {
public ID MasterRecordIdfield = null;
Account[] acc= Trigger.old; 
MasterRecordIdfield = acc[0].MasterRecordId;
if(MasterRecordIdfield != null && String.valueOf(MasteRecordIdfield).length() >0)
{
Account[] acct = [select Id, name from account where id =:MasterRecordIdField];
for(Account acct:acc)
{
acct.Field_to_update__c = acct.name;
update acct;
}
}
}

 

 

When I execute this, I m getting the following error, I dont know where I am going wrong. Help Please.

 

Apex trigger then.accountMerge caused an unexpected exception, contact your administrator: then.accountMerge: execution of AfterDelete caused by: System.DmlException: Update failed. First exception on row 0 with id 0019000000K7lxGAAR; first error: SELF_REFERENCE_FROM_TRIGGER, Object 0019000000K7lxGAAR is currently in a merge operation, therefore a trigger can not update it.: []: Trigger.then.accountMerge: line 12, column 1