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
Patrick/ JamesPatrick/ James 

Trigger Help - Campaign to Campaign Member objects - Rich Text

Hi there,

I'm very new to Apex Triggers, so apologies if this is a silly question...

I'm trying to get a Rich Text field on my Campaign object - Extra_Info_for_Reminder__c - to populate a Rich Text field on my Campaign Member object - Course_Email_Info_2__c​. As far as I understand, copying the fields via a workflow strips the text of any HTML (which we need to retain).

In Developer Console the following code doesn't show up with any errors, but it not currently providing the desired affect...
 
trigger richtextupdate on Campaign (after update, after insert) {

for(Campaign C:trigger.new){
    if(C.Extra_Info_for_Reminder__c!=null){
CampaignMember.Course_Email_Info_2__c=C.Extra_Info_for_Reminder__c;
}
}
}

Hopefully there's a very simple explaination. As I say, bit of a fish out of water with this stuff, so any help is much appreciated.

Thanks very much.
Best Answer chosen by Patrick/ James
Shyama B SShyama B S
Hi,

Give this a try. This works:

trigger CampaignTrigger on Campaign (after insert, after update) {
    List<CampaignMember> memberList=new List<CampaignMember>();
    Map<Id,Campaign> CampMemMap=new Map<Id,Campaign>([select id, (select id from CampaignMembers) from Campaign 
                                                  where id IN: Trigger.new]);

    for(Campaign c: Trigger.new){
        if(c.Extra_Info_for_Reminder__c!=null && CampMemMap.get(c.id).CampaignMembers.size()>0){
            memberList.addAll(CampMemMap.get(c.id).CampaignMembers);
                for(CampaignMember m: memberList){
                    m.Course_Email_Info_2__c​=c.Extra_Info_for_Reminder__c;
                }
        }
    }
    if(memberList.size()>0){
        update memberList;
    }
}

Thanks

All Answers

Shyama B SShyama B S
HI Patrick,
Can you please try before update and before insert? 

After triggers are used to access field values that are set by the system (such as a record's Id or LastModifiedDate field), and to effect changes in other records. The records that fire the after trigger are read-only.

Thanks,
Shyama.
Patrick/ JamesPatrick/ James
Hi Shyama - thanks very much for your suggestion.

I have tried that, and its still not populating.

I've had a little route around and strangely - when I enter the code via Developer Console, this is how it comes through in the 'Triggers' view:

User-added image

I'm guessing this is probably the issue? That the code isn't actually running as I've entered it.

Thanks!
Patrick/ JamesPatrick/ James
...also, when I enter the code into the regular 'Trigger Edit' view (and avoid Developer Console) it provides the following error:

User-added image
Shyama B SShyama B S

Hi Patrick,

Can you try entering the code through developer console? 

Setup -> Build -> Apex Triggers -> Developer Console

Click on File -> new Apex Trigger

Enter the code, Save and try again.

Thanks,
Shyama
Patrick/ JamesPatrick/ James
Thank you - I did try and that. And no further luck.

Patrick
Shyama B SShyama B S
Hi,

Give this a try. This works:

trigger CampaignTrigger on Campaign (after insert, after update) {
    List<CampaignMember> memberList=new List<CampaignMember>();
    Map<Id,Campaign> CampMemMap=new Map<Id,Campaign>([select id, (select id from CampaignMembers) from Campaign 
                                                  where id IN: Trigger.new]);

    for(Campaign c: Trigger.new){
        if(c.Extra_Info_for_Reminder__c!=null && CampMemMap.get(c.id).CampaignMembers.size()>0){
            memberList.addAll(CampMemMap.get(c.id).CampaignMembers);
                for(CampaignMember m: memberList){
                    m.Course_Email_Info_2__c​=c.Extra_Info_for_Reminder__c;
                }
        }
    }
    if(memberList.size()>0){
        update memberList;
    }
}

Thanks
This was selected as the best answer
Patrick/ JamesPatrick/ James
This is great - thanks Shyama!

Is there a quick addition we could make to this so that this trigger (or a new trigger) is launched from the CampaignMember object (when it's inserted or edited) that would perform this same function, and pull this same Rich Text data from the related Campaign object?

Thanks again!