0

I am finally getting close with my trigger. I at at the point where I need to take the picklist value from one field and update the same value into a picklist with the same value on an unrelated object. So, Task.Call_Dispositon__c needs to carry over and update CampaignMember.Call_Disposition__c so that they have the same value. Both are picklists. Suggestions on how to do this? I am not certain if when working with the picklists if they return the picklist value id or the text of the picklist field.

trigger CallDispositionOnCampaignMember on Task (before insert) {

    list<CampaignMember> myCampaignMembers = new list<CampaignMember>();
    myCampaignMembers = [Select ID, Call_Disposition__c From CampaignMember];

    list<Task> myTasks = new list<Task>();
    myTasks = [Select ID, Campaign_Member_ID__c, Call_Disposition__c From Task Where Campaign_Member_ID__c != null];

    map<string,Task> memIDtoTaskMap = new map <string,Task>();
    for (Task t: myTasks){
        memIDtoTaskMap.put(t.Campaign_Member_ID__c, t );
    }

    for (CampaignMember a: myCampaignMembers){
        if (memIDtoTaskMap.containsKey(a.id)){
            list<Task> newTask = new list <task> (myTasks);
            newTask = [Select ID, Campaign_Member_ID__c, Call_Disposition__c from Task Where Campaign_Member_ID__c = : a.id];

            for (task c: newTask){
                if (c.Campaign_Member_ID__c == a.id){     
                   a.Call_Disposition__c = c.Call_Disposition__c;
                }
                update myCampaignMembers;
            }
        }   

    }
}
2
  • What is the relationship between CampaignMember record and Task record? From what I understand, the Task record has a lookup to CampaignMember record. And all you want to do in the trigger is, before inserting a Task record, set the Call_Disposition__c field of the related CampaignMember record to be the same as the Call_Disposition__c field of the triggering Task record. Am I correct? – navD87 May 1 '15 at 17:42
  • I created a custom button on the Campaign Member page layout to create a task related to the contact or lead. Yes, you are correct that is exactly what I am trying to do. – Lori May 1 '15 at 17:45
0

Here is what I would do to achieve this,

trigger CallDispositionOnCampaignMember on Task (before insert) {

    //get the ids of the related CampaignMember records of the triggering tasks
    List<Id> campaignMemberIds = new List<Id>();
    for(Task t : Trigger.new) {
        if(t.Campaign_Member_ID__c != null) {
            campaignMemberIds.add(t.Campaign_Member_ID__c);
        }
    }

    //query out the related CampaignMember records
    Map<Id, CampaignMember> myCampaignMembers = new Map<Id, CampaignMember> ([Select ID, Call_Disposition__c FROM CampaignMember WHERE Id IN :campaignMemberIds]);

    for(Task t : Trigger.new) {
        if(t.Campaign_Member_ID__c != null && myCampaignMembers.containsKey(t.Campaign_Member_ID__c)){
            myCampaignMembers.get(t.Campaign_Member_ID__c).Call_Disposition__c = t.Call_Disposition__c;
        }
    }
    update myCampaignMembers.values();

}
4
  • So I tried with the code here, and got Error: Compile Error: Illegal assignment from Schema.SObjectField to String at line 16 column 60 which is line: myCampaignMembers.get(t.Campaign_Member_ID__c).Call_Disposition__c = task.Call_Disposition__c; – Lori May 1 '15 at 18:01
  • I made a mistake in my original code, had task.Call_Disposition__c, instead of t.Call_Disposition__c. check the new code – navD87 May 1 '15 at 18:05
  • You are a Salesforce GURU! Thank you so much!!! I was making this tremendously harder than I needed to. You totally rock! It works perfectly!!! – Lori May 1 '15 at 18:08
  • Glad to help. please mark as the answer – navD87 May 1 '15 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.