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 

Trigger to update field on Campaign from Campaign Member failing

Hi,

I'm trying to mimic a workflow field update from the Campaign Member to the Campaign (as it cannot be done via a workflow). What I want is when a picklist (Send_tutor_details_to_client__c) is updated to 'First', the contents of another field on the Campaign Member (Tutor_details_for_email__c) are copied to a corresponding field on the Campaign (Tutor_1__c).

I'm getting an error about sObjects, however, that I don't understand.

This is the trigger:

trigger UpdateTutor1onCampaign on CampaignMember (after insert, after update) {

  Map<ID, Campaign> parentCams = new Map<ID, Campaign>();
  List<Id> listIds = new List<Id>();

  for (CampaignMember childObj : Trigger.new) {
    listIds.add(childObj.Campaign);
  }

       parentCams = new Map<Id, Campaign>([SELECT id, Tutor_1__c Name,(SELECT ID, Send_tutor_details_to_client__c FROM CampaignMember) FROM Campaign WHERE ID IN :listIds]);

  for (CampaignMember childObj: Trigger:new){
     Campaign myParentCams = parentCams.get(childObj.Campaign);
     myParentCams.Tutor_1__c = childObj.Send_tutor_details_to_client__c;
  }

  update parentCams.values();
}

And this is the error:

sObject type 'Interview_Date_Time_Confirmed__c' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names.

Any help would be very welcome, thanks!

Best Answer chosen by Olivia Cannon
Vamsi KrishnaVamsi Krishna
we just need to replace Campaign with CampaignId in couple of places and also i noticed we used : instead of . in the for loop while accessing Trigger.new

here's the corrected version.. hope it works this time
trigger UpdateTutor1onCampaign on CampaignMember (after insert, after update) {

  Map<ID, Campaign> parentCams = new Map<ID, Campaign>();
  Set<Id> listIds = new Set<Id>();

  for (CampaignMember childObj : Trigger.new) {
    listIds.add(childObj.CampaignId);
  }

  parentCams = new Map<Id, Campaign>([SELECT id, Tutor_1__c ,Name FROM Campaign WHERE ID IN :listIds]);

  for (CampaignMember childObj: Trigger.new){
     Campaign myParentCams = parentCams.get(childObj.CampaignId);
     myParentCams.Tutor_1__c = childObj.Send_tutor_details_to_client__c;
  }

  update parentCams.values();
}


All Answers

Vamsi KrishnaVamsi Krishna
Olivia,
couple of changes.. there is a , missing after Tutor_1__c in the SOQL.. also you don't need to query the child records again since you get that in the trigger anyway..

try this version and see if it works for you..

trigger UpdateTutor1onCampaign on CampaignMember (after insert, after update) {

  Map<ID, Campaign> parentCams = new Map<ID, Campaign>();
  Set<Id> listIds = new Set<Id>();

  for (CampaignMember childObj : Trigger.new) {
    listIds.add(childObj.Campaign);
  }

  parentCams = new Map<Id, Campaign>([SELECT id, Tutor_1__c ,Name FROM Campaign WHERE ID IN :listIds]);

  for (CampaignMember childObj: Trigger:new){
     Campaign myParentCams = parentCams.get(childObj.Campaign);
     myParentCams.Tutor_1__c = childObj.Send_tutor_details_to_client__c;
  }

  update parentCams.values();
}

Olivia CannonOlivia Cannon
Thank you Vamsi, this is definitely a step closer! I'm still getting an error, though:

Error: Compile Error: Incompatible element type SOBJECT:Campaign for collection of Id at line 7 column 5
Vamsi KrishnaVamsi Krishna
we just need to replace Campaign with CampaignId in couple of places and also i noticed we used : instead of . in the for loop while accessing Trigger.new

here's the corrected version.. hope it works this time
trigger UpdateTutor1onCampaign on CampaignMember (after insert, after update) {

  Map<ID, Campaign> parentCams = new Map<ID, Campaign>();
  Set<Id> listIds = new Set<Id>();

  for (CampaignMember childObj : Trigger.new) {
    listIds.add(childObj.CampaignId);
  }

  parentCams = new Map<Id, Campaign>([SELECT id, Tutor_1__c ,Name FROM Campaign WHERE ID IN :listIds]);

  for (CampaignMember childObj: Trigger.new){
     Campaign myParentCams = parentCams.get(childObj.CampaignId);
     myParentCams.Tutor_1__c = childObj.Send_tutor_details_to_client__c;
  }

  update parentCams.values();
}


This was selected as the best answer
Olivia CannonOlivia Cannon
Great, thanks!