You need to sign in to do that
Don't have an account?
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
This is more information about the custom setting:
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
Where are you using this custom settings? Any idea?
Please let me know if I can help you.
regards,
Shashi
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('<<CampaignMember.FirstName>>'))
{
emailBody = emailBody.replaceAll
('<<CampaignMember.FirstName>>','<b>'+MapOfCampaignMember.get(ContactLeadId)+'</b>' );
}
if(emailBody.contains('<<Event.Name>>'))
{
emailBody = emailBody.replaceAll('<<Event.Name>>','<b>'+MapOfCampaign.get(EventId).Name
+'</b>' );
}
if(emailBody.contains('<<Event.rC_Event__Start_Date_Time__c>>'))
{
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
('<<Event.rC_Event__Start_Date_Time__c>>','<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'));
}
}
}
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
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:
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('<<CampaignMember.FirstName>>'))
{
emailBody = emailBody.replaceAll('<<CampaignMember.FirstName>>','<b>'+MapOfCampaignMember.get(ContactLeadId)+'</b>' );
}
if(emailBody.contains('<<Event.Name>>'))
{
emailBody = emailBody.replaceAll('<<Event.Name>>','<b>'+MapOfCampaign.get(EventId).Name+'</b>' );
}
if(emailBody.contains('<<Event.rC_Event__Start_Date_Time__c>>'))
{
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('<<Event.rC_Event__Start_Date_Time__c>>','<b>'+StartDateTime+'</b>');
}
if(emailBody.contains('<<Event.rC_Event__End_Date_Time__c>>'))
{
String EndDateTime = (MapOfCampaign.get(EventId).rC_Event__End_Date_Time__c!) ? (MapOfCampaign.get(EventId).rC_Event__End_Date_Time__c).format():' ';
emailBody = emailBody.replaceAll('<<Event.rC_Event__End_Date_Time__c_Date_Time__c>>','<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:
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