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
Sarah Parise 7Sarah Parise 7 

How to add merge fields to custom settings?

Hi! I am trying to edit one of our custom settings that was created for us by a consultant "Consolidated Email Body", but I cannot figure out how to add another merge field value, as an option to added. 

Right now we have <<Session.rCEvent.Event__Start_Date_Time_c>> (which is the Start Date on a campaign sessions, that we use to register attendees for fundraising events. The Campaign Session also has a Event End Date/Time which we need to add to this as well. Does anyone know where I can edit/or add merge fields to custom settings?

This is the "Manage" option to update the text for the consolidated email
User-added image

This is more information about the custom setting:
User-added image
 

Best Answer chosen by Sarah Parise 7
Shashi PatowaryShashi Patowary
Hi Sarah,

Thanks for your reply. You have come across test classes execution problem. As the failure statement says - A trigger must have at least 1% code coverage for successful deployment. Please try to correct the test classes -

First test class failed due to insertion of duplicate records. Try to put upsert statement if you are using insert or try to avoid the duplicate records in the test class.
The other two records failed to dependency on a flow. You may check this link -
http://salesforce.stackexchange.com/questions/72951/process-builder-breaking-production-deployments

Please let me know if this is helpful.

Regards,
Shashi Patowary

All Answers

Shashi PatowaryShashi Patowary
Hi Sarah,

Where are you using this custom settings? Any idea?

Please let me know if I can help you.

regards,
Shashi
Sarah Parise 7Sarah Parise 7
It's used in an apex class that sends the email out ->


public class ConsolidatedEmailCtrl {
public ConsolidatedEmailCtrl(){
}
public void sendEmail(){
Set<Id> SetofCampaigns = new Set<Id>();

Map<String,list<String>> MapOfCamMemEventAndSessions = new Map<String,list<String>>();
Map<String,String> MapOfLeadOrContactIdAndEmailAddress = new Map<String,String>();
Map<String,String> MapOfCampaignMember = new Map<String,String>();
list<CampaignMember> LstOfCampaignMembers = new list<CampaignMember>();

list<Messaging.SingleEmailMessage> LstOfEmailsToBeSent = new list<Messaging.SingleEmailMessage>();

//Member must be registered for event and event session to recieve email.
for(CampaignMember camMem : [select 

id,campaignId,Campaign.ParentId,Lead_Contact_email__c,Lead_Contact_ID__c,Name from 
                             campaignmember where Needs_to_send_email__c = true AND 

campaign.RecordType.DeveloperName='Event_Session'
                             AND campaign.parent.RecordType.DeveloperName = 'Event' AND Lead_Contact_email__c!

=null 
                             AND rC_Event__Registered_Status__c = 'Registered' order by 

campaign.rC_Event__Start_Date_Time__c asc])
{
    
    if(MapOfCamMemEventAndSessions.containskey(camMem.Lead_Contact_ID__c+'|'+camMem.Campaign.ParentId))
    {
        MapOfCamMemEventAndSessions.get(camMem.Lead_Contact_ID__c+'|'+camMem.Campaign.ParentId).add

(camMem.campaignId);
    }
    else
    {
        MapOfCamMemEventAndSessions.put(camMem.Lead_Contact_ID__c+'|'+camMem.Campaign.ParentId,new 

list<String>{camMem.campaignId});
    }
         
    //Adding CampaignsId in Set
     SetofCampaigns.add(camMem.CampaignId);
     SetofCampaigns.add(camMem.Campaign.parentId);  
     
     //Setting Map of Lead or Contact ID with their email address
     MapOfLeadOrContactIdAndEmailAddress.put(camMem.Lead_Contact_ID__c,camMem.Lead_Contact_email__c);
     
     //Setting Map of Campaign Member
     MapOfCampaignMember.put(camMem.Lead_Contact_ID__c,camMem.Name);
     
     //Adding campaign members in list for update after processing
     camMem.Needs_to_send_email__c = false;
     LstOfCampaignMembers.add(camMem);
}

Map<Id,Campaign> MapOfCampaign = new Map<Id,Campaign>([select 

id,name,rC_Giving__Affiliation__c,Header__c,Footer__c,
                                                       rC_Event__Start_Date_Time__c,Type__C from Campaign where 

Id IN: SetOfCampaigns]);

//Query custom setting to get URL of Images
Map<String,String> MapOfLOGONameAndURL = new Map<String,String>();
for(Consolidatedemail__c cs : [select name,LOGO_URL__c from Consolidatedemail__c])
{
    MapOfLOGONameAndURL.put(cs.name,cs.LOGO_URL__c);
}

//Query custom setting to get email Body template
Map<String,String> MapOfConsolidatedBody = new Map<String,String>();
for(Consolidated_body__c cb : [select name,Body_Template__c from Consolidated_body__c])
{
    MapOfConsolidatedBody.put(cb.name,cb.Body_Template__c );
}

//start body: Filling Map of Campaign and Campaign Venue             
Map<String,List<String>> MapOfCamAndCamVen = new Map<String,List<String>>();
  for(rC_Event__Campaign_Venue__c camVen : [select 

id,name,rC_Event__Campaign__r.rC_Event__Start_Date_Time__c,rC_Event__Campaign__c,
                                            rC_Event__Venue_Name__c from rC_Event__Campaign_Venue__c where 

rC_Event__Campaign__c IN: SetofCampaigns])
  {
     if(MapOfCamAndCamVen.containskey(camVen.rC_Event__Campaign__c))           
     MapOfCamAndCamVen.get(camVen.rC_Event__Campaign__c).add(camVen.rC_Event__Venue_Name__c );
     else
     MapOfCamAndCamVen.put(camVen.rC_Event__Campaign__c,new list<String>{camVen.rC_Event__Venue_Name__c });
  }
//End body: Filling Map of Campaign and Campaign Venue 



for(String str : MapOfCamMemEventAndSessions.keyset())
{
//apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'str '+str ));
//Parse Event Id
String EventId = str.substring(str.indexof('|')+1,str.length());
String ContactLeadId = str.substring(0,str.indexof('|'));
STring ContactleadEmailAddress = MapOfLeadOrContactIdAndEmailAddress.get(ContactLeadId);


String emailBody = '<html> <body>  <img src="';
emailBody += MapOfLOGONameAndURL.get(MapOfCampaign.get(EventId).rC_Giving__Affiliation__c) + ' ';
emailBody += '"></img></body> </html>';

//Start Body of Header
String EmailHeader = MapOfCampaign.get(EventId).Header__c;
//apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'EmailHeader '+EmailHeader ));
if(EmailHeader !=null && EmailHeader  !='')
{
    //code for parsing Header
    emailBody += EmailHeader;
    if(emailBody.contains('&lt;&lt;CampaignMember.FirstName&gt;&gt;'))
    {
        emailBody = emailBody.replaceAll

('&lt;&lt;CampaignMember.FirstName&gt;&gt;','<b>'+MapOfCampaignMember.get(ContactLeadId)+'</b>' );
    }
    
    if(emailBody.contains('&lt;&lt;Event.Name&gt;&gt;'))
    {
        emailBody = emailBody.replaceAll('&lt;&lt;Event.Name&gt;&gt;','<b>'+MapOfCampaign.get(EventId).Name

+'</b>' );
    }
    
    if(emailBody.contains('&lt;&lt;Event.rC_Event__Start_Date_Time__c&gt;&gt;'))
    {
        String StartDateTime = (MapOfCampaign.get(EventId).rC_Event__Start_Date_Time__c!=null) ? 

(MapOfCampaign.get(EventId).rC_Event__Start_Date_Time__c).format():' ';
        emailBody = emailBody.replaceAll

('&lt;&lt;Event.rC_Event__Start_Date_Time__c&gt;&gt;','<b>'+StartDateTime+'</b>');
    }            
}
//End Body fo Header
//apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'Sessions of 

campaign'+MapOfCamMemEventAndSessions.get(str) ));
//Start of Body // it must be session
for(String sessionId: MapOfCamMemEventAndSessions.get(str))
{
    String TypeOfBody = MapOfCampaign.get(EventId).Type__c;
     //apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'TypeOfBody '+TypeOfBody

+'---'+sessionId ));
    
    if(TypeOfBody!=null && TypeOfBody!='')
    {
        String EmailBodyText = MapOfConsolidatedBody.get(MapOfCampaign.get(EventId).Type__c);
       // apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'EmailBodyText '+EmailBodyText 

));
       
        emailBody += '<br/><br/>'+EmailBodyText;
        if(emailBody.contains('<<Session.Name>>'))
        {
            emailBody = emailBody.replaceAll('<<Session.Name>>','<b>'+MapOfCampaign.get(sessionId).Name

+'</b><br/>' );
        }       
        if(emailBody.contains('<<Session.rC_Event__Start_Date_Time__c>>'))
        {
            String StartDateTime = (MapOfCampaign.get(sessionId).rC_Event__Start_Date_Time__c!=null) ? 

(MapOfCampaign.get(sessionId).rC_Event__Start_Date_Time__c).format():' ';
            emailBody = emailBody.replaceAll('<<Session.rC_Event__Start_Date_Time__c>>','<b>'+StartDateTime 

+'</b><br/>' );
        } 
        
        if(emailBody.contains('<<Session.Venue>>'))
        {
            emailBody = emailBody.replaceAll('<<Session.Venue>>', MapOfCamAndCamVen.get(sessionId)!=null? 

'<b>'+MapOfCamAndCamVen.get(sessionId)+'</b>': '<b>None</b>' );
        }                     
    
    }

}

//Start Body of Footer
String EmailFooter = MapOfCampaign.get(EventId).Footer__c;
//apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'EmailFooter '+EmailFooter ));
if(EmailFooter !=null && EmailFooter !='')
{
    //code for parsing Header
    emailBody += '<br/><br/>'+EmailFooter;
    
}





//End of Body

// org wide email address
list<OrgWideEmailAddress> owea = new list<OrgWideEmailAddress>();
if(!test.isrunningtest())
{
    owea = [select Id from OrgWideEmailAddress where Address = 'events@ounceofprevention.org'];
}
//preparing list of email messages to be send
    
    Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
    emailToSend.setHtmlBody(emailBody );
    emailToSend.setSubject('Thank you for Registering!');
    emailToSend.setToAddresses(new List<String>{ContactleadEmailAddress});
    if(owea.size() > 0) {
        emailTosend.setOrgWideEmailAddressId(owea.get(0).Id);
    }
        
    LstOfEmailsToBeSent.add(emailToSend);    
}
    if(LstOfEmailsToBeSent.size()>0)
    {        
        //send email    
        Messaging.sendEmail(LstOfEmailsToBeSent);     
        update LstOfCampaignMembers;
       // apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'email sent'));   
    }

}
}
Sarah Parise 7Sarah Parise 7
... I think. I'm not  really sure, I am our organizations admin, we had consultants build this, but they don't work with us anymore. This is kind of uncharted territory.
Shashi PatowaryShashi Patowary
Hi Sarah,

Your apex class is doing the actual work - filling up the template with proper data in the template provided by your custom settings.This means if you modify your Custome settings, you need to update your apex class as well to send updated mail template.Let me see how I can help you.If possible, can you please send the generated mail (highlighing the update that you need).

Regards,
Shashi Patowary
Sarah Parise 7Sarah Parise 7
Hi Shashi, 

Sorry for the delayed response here is the email that is generated, we need to add the field Event.rC_Event__End_Date_Time__c_Date_Time__c next to Session End Date:

User-added image

I updated the apex class to read like this:


public class ConsolidatedEmailCtrl {

public ConsolidatedEmailCtrl(){

}


public void sendEmail(){

Set<Id> SetofCampaigns = new Set<Id>();


Map<String,list<String>> MapOfCamMemEventAndSessions = new Map<String,list<String>>();
Map<String,String> MapOfLeadOrContactIdAndEmailAddress = new Map<String,String>();
Map<String,String> MapOfCampaignMember = new Map<String,String>();
list<CampaignMember> LstOfCampaignMembers = new list<CampaignMember>();

list<Messaging.SingleEmailMessage> LstOfEmailsToBeSent = new list<Messaging.SingleEmailMessage>();

//Member must be registered for event and event session to recieve email.
for(CampaignMember camMem : [select id,campaignId,Campaign.ParentId,Lead_Contact_email__c,Lead_Contact_ID__c,Name from 
                             campaignmember where Needs_to_send_email__c = true AND campaign.RecordType.DeveloperName='Event_Session'
                             AND campaign.parent.RecordType.DeveloperName = 'Event' AND Lead_Contact_email__c!=null 
                             AND rC_Event__Registered_Status__c = 'Registered' order by campaign.rC_Event__Start_Date_Time__c asc])
{
    
    if(MapOfCamMemEventAndSessions.containskey(camMem.Lead_Contact_ID__c+'|'+camMem.Campaign.ParentId))
    {
        MapOfCamMemEventAndSessions.get(camMem.Lead_Contact_ID__c+'|'+camMem.Campaign.ParentId).add(camMem.campaignId);
    }
    else
    {
        MapOfCamMemEventAndSessions.put(camMem.Lead_Contact_ID__c+'|'+camMem.Campaign.ParentId,new list<String>{camMem.campaignId});
    }
         
    //Adding CampaignsId in Set
     SetofCampaigns.add(camMem.CampaignId);
     SetofCampaigns.add(camMem.Campaign.parentId);  
     
     //Setting Map of Lead or Contact ID with their email address
     MapOfLeadOrContactIdAndEmailAddress.put(camMem.Lead_Contact_ID__c,camMem.Lead_Contact_email__c);
     
     //Setting Map of Campaign Member
     MapOfCampaignMember.put(camMem.Lead_Contact_ID__c,camMem.Name);
     
     //Adding campaign members in list for update after processing
     camMem.Needs_to_send_email__c = false;
     LstOfCampaignMembers.add(camMem);
}

Map<Id,Campaign> MapOfCampaign = new Map<Id,Campaign>([select id,name,rC_Giving__Affiliation__c,Header__c,Footer__c,
                                                       rC_Event__Start_Date_Time__c,Type__C from Campaign where Id IN: SetOfCampaigns]);

//Query custom setting to get URL of Images
Map<String,String> MapOfLOGONameAndURL = new Map<String,String>();
for(Consolidatedemail__c cs : [select name,LOGO_URL__c from Consolidatedemail__c])
{
    MapOfLOGONameAndURL.put(cs.name,cs.LOGO_URL__c);
}

//Query custom setting to get email Body template
Map<String,String> MapOfConsolidatedBody = new Map<String,String>();
for(Consolidated_body__c cb : [select name,Body_Template__c from Consolidated_body__c])
{
    MapOfConsolidatedBody.put(cb.name,cb.Body_Template__c );
}

//start body: Filling Map of Campaign and Campaign Venue             
Map<String,List<String>> MapOfCamAndCamVen = new Map<String,List<String>>();
  for(rC_Event__Campaign_Venue__c camVen : [select id,name,rC_Event__Campaign__r.rC_Event__Start_Date_Time__c,rC_Event__Campaign__r.rC_Event__End_Date_Time__c,rC_Event__Campaign__c,
                                            rC_Event__Venue_Name__c from rC_Event__Campaign_Venue__c where rC_Event__Campaign__c IN: SetofCampaigns])
  {
     if(MapOfCamAndCamVen.containskey(camVen.rC_Event__Campaign__c))           
     MapOfCamAndCamVen.get(camVen.rC_Event__Campaign__c).add(camVen.rC_Event__Venue_Name__c );
     else
     MapOfCamAndCamVen.put(camVen.rC_Event__Campaign__c,new list<String>{camVen.rC_Event__Venue_Name__c });
  }
//End body: Filling Map of Campaign and Campaign Venue 



for(String str : MapOfCamMemEventAndSessions.keyset())
{
//apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'str '+str ));
//Parse Event Id
String EventId = str.substring(str.indexof('|')+1,str.length());
String ContactLeadId = str.substring(0,str.indexof('|'));
STring ContactleadEmailAddress = MapOfLeadOrContactIdAndEmailAddress.get(ContactLeadId);


String emailBody = '<html> <body>  <img src="';
emailBody += MapOfLOGONameAndURL.get(MapOfCampaign.get(EventId).rC_Giving__Affiliation__c) + ' ';
emailBody += '"></img></body> </html>';

//Start Body of Header
String EmailHeader = MapOfCampaign.get(EventId).Header__c;
//apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'EmailHeader '+EmailHeader ));
if(EmailHeader !=null && EmailHeader  !='')
{
    //code for parsing Header
    emailBody += EmailHeader;
    if(emailBody.contains('&lt;&lt;CampaignMember.FirstName&gt;&gt;'))
    {
        emailBody = emailBody.replaceAll('&lt;&lt;CampaignMember.FirstName&gt;&gt;','<b>'+MapOfCampaignMember.get(ContactLeadId)+'</b>' );
    }
    
    if(emailBody.contains('&lt;&lt;Event.Name&gt;&gt;'))
    {
        emailBody = emailBody.replaceAll('&lt;&lt;Event.Name&gt;&gt;','<b>'+MapOfCampaign.get(EventId).Name+'</b>' );
    }
    
    if(emailBody.contains('&lt;&lt;Event.rC_Event__Start_Date_Time__c&gt;&gt;'))
    {
        String StartDateTime = (MapOfCampaign.get(EventId).rC_Event__Start_Date_Time__c!=null) ? (MapOfCampaign.get(EventId).rC_Event__Start_Date_Time__c).format():' ';
        emailBody = emailBody.replaceAll('&lt;&lt;Event.rC_Event__Start_Date_Time__c&gt;&gt;','<b>'+StartDateTime+'</b>');
    } 
if(emailBody.contains('&lt;&lt;Event.rC_Event__End_Date_Time__c&gt;&gt;'))
    {
        String EndDateTime = (MapOfCampaign.get(EventId).rC_Event__End_Date_Time__c!) ? (MapOfCampaign.get(EventId).rC_Event__End_Date_Time__c).format():' ';
        emailBody = emailBody.replaceAll('&lt;&lt;Event.rC_Event__End_Date_Time__c_Date_Time__c&gt;&gt;','<b>'+EndDateTime+'</b>');
    }   
}
//End Body of Header
//apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'Sessions of campaign'+MapOfCamMemEventAndSessions.get(str) ));
//Start of Body // it must be session
for(String sessionId: MapOfCamMemEventAndSessions.get(str))
{
    String TypeOfBody = MapOfCampaign.get(EventId).Type__c;
     //apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'TypeOfBody '+TypeOfBody+'---'+sessionId ));
    
    if(TypeOfBody!=null && TypeOfBody!='')
    {
        String EmailBodyText = MapOfConsolidatedBody.get(MapOfCampaign.get(EventId).Type__c);
       // apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'EmailBodyText '+EmailBodyText ));
       
        emailBody += '<br/><br/>'+EmailBodyText;
        if(emailBody.contains('<<Session.Name>>'))
        {
            emailBody = emailBody.replaceAll('<<Session.Name>>','<b>'+MapOfCampaign.get(sessionId).Name+'</b><br/>' );
        }       
        if(emailBody.contains('<<Session.rC_Event__Start_Date_Time__c>>'))
        {
            String StartDateTime = (MapOfCampaign.get(sessionId).rC_Event__Start_Date_Time__c!=null) ? (MapOfCampaign.get(sessionId).rC_Event__Start_Date_Time__c).format():' ';
            emailBody = emailBody.replaceAll('<<Session.rC_Event__Start_Date_Time__c>>','<b>'+StartDateTime +'</b><br/>' );
        } 
         if(emailBody.contains('<<Session.rC_Event__End_Date_Time__c>>'))
        {
            String EndDateTime = (MapOfCampaign.get(sessionId).rC_Event__End_Date_Time__c!=null) ? (MapOfCampaign.get(sessionId).rC_Event__End_Date_Time__c).format():' ';
            emailBody = emailBody.replaceAll('<<Session.rC_Event__End_Date_Time__c>>','<b>'+EndDateTime +'</b><br/>' );
        } 
        
        if(emailBody.contains('<<Session.Venue>>'))
        {
            emailBody = emailBody.replaceAll('<<Session.Venue>>', MapOfCamAndCamVen.get(sessionId)!=null? '<b>'+MapOfCamAndCamVen.get(sessionId)+'</b>': '<b>None</b>' );
        }                     
    
    }

}

//Start Body of Footer
String EmailFooter = MapOfCampaign.get(EventId).Footer__c;
//apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'EmailFooter '+EmailFooter ));
if(EmailFooter !=null && EmailFooter !='')
{
    //code for parsing Header
    emailBody += '<br/><br/>'+EmailFooter;
    
}





//End of Body

// org wide email address
list<OrgWideEmailAddress> owea = new list<OrgWideEmailAddress>();
if(!test.isrunningtest())
{
    owea = [select Id from OrgWideEmailAddress where Address = 'events@ounceofprevention.org'];
}
//preparing list of email messages to be send
    
    Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
    emailToSend.setHtmlBody(emailBody );
    emailToSend.setSubject('Thank you for Registering!');
    emailToSend.setToAddresses(new List<String>{ContactleadEmailAddress});
    if(owea.size() > 0) {
        emailTosend.setOrgWideEmailAddressId(owea.get(0).Id);
    }
        
    LstOfEmailsToBeSent.add(emailToSend);    
}
    if(LstOfEmailsToBeSent.size()>0)
    {        
        //send email    
        Messaging.sendEmail(LstOfEmailsToBeSent);     
        update LstOfCampaignMembers;
       // apexpages.addmessage(new apexpages.message(apexpages.severity.confirm,'email sent'));   
    }




}



}

but when I attempt to deploy the change into our production account i get the following errors:
User-added image
Shashi PatowaryShashi Patowary
Hi Sarah,

Thanks for your reply. You have come across test classes execution problem. As the failure statement says - A trigger must have at least 1% code coverage for successful deployment. Please try to correct the test classes -

First test class failed due to insertion of duplicate records. Try to put upsert statement if you are using insert or try to avoid the duplicate records in the test class.
The other two records failed to dependency on a flow. You may check this link -
http://salesforce.stackexchange.com/questions/72951/process-builder-breaking-production-deployments

Please let me know if this is helpful.

Regards,
Shashi Patowary
This was selected as the best answer
Sarah Parise 7Sarah Parise 7
Thank you for the link - the extra information is super helpful!