0

I'm trying to setup an automated calendar invite using the Microsoft Graph API endpoint and APEX. I have successfully created a HTTPcallout the sends the calendar invite, but i am having trouble getting the body of the email formatted correctly.

The body of the emails is a nice template that our marketing team made in Marketing Cloud. I have taken this HTML template and created an Email template in Saleforce and then in the call i am retrieving the HtmlValue of this template. The problem I am having is that when the value of the template is used in the callout its just displayed as text, but when hard code it into the code it works fine and displays properly as HTML.

Does anyone know why this would be? I can replicate this with any HTML. Example replacing the template value with Test displays as that text in the html body of the calendar invite when I use the variable. If I hard code 'Test into my code it works fine and displays as HTML.

The commented out line below with the html content is what works if I uncomment that and comment otu the variable line. I don't understand what is happening. I have tried formatting the variable with escapeHtml4() and JSON.seriallize() but neither work.

Does anyone have any ideas?

Thanks

This is my code:

    //getting the template body and subject
    EmailTemplate templateInfo = [Select Name, Subject, HtmlValue from EmailTemplate Where Name = :emailDetails.templateName];

    //building the jsonBody String
    JSONGenerator j = JSON.createGenerator(true);
    j.writeStartObject();
    j.writeStringField('subject', templateInfo.Subject);
    j.writeFieldName('body');
    j.writeStartObject();
    j.writeStringField('contentType', 'HTML');
    j.writeStringField('content', templateInfo.HtmlValue);
    // j.writeStringField('content', '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><style type="text/css">ReadMsgBody{ width: 100%;}    .ExternalClass {width: 100%;}.ExternalClass, .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td, .ExternalClass div {line-height: 100%;}body {-webkit-text-size-adjust:100%; -ms-text-size-adjust:100%;margin:0 !important;}p { margin: 1em 0;}table td { border-collapse: collapse;}img {outline:0;}a img {border:none;}@-ms-viewport{ width: device-width;}</style><style type="text/css">@media only screen and (max-width: 480px) {.container {width: 100% !important;}.footer { width:auto !important; margin-left:0; }.mobile-hidden { display:none !important; }.logo { display:block !important; padding:0 !important; }img { max-width:100% !important; height:auto !important; max-height:auto !important;}.header img{max-width:100% !important;height:auto !important; max-height:auto !important;}.photo img { width:100% !important; max-width:100% !important; height:auto !important;}.drop { display:block !important; width: 100% !important; float:left; clear:both;}.footerlogo { display:block !important; width: 100% !important; padding-top:15px; float:left; clear:both;}.nav4, .nav5, .nav6 { display: none !important; }.tableBlock {width:100% !important;}.responsive-td {width:100% !important; display:block !important; padding:0 !important; }.fluid, .fluid-centered {width: 100% !important;max-width: 100% !important;height: auto !important;margin-left: auto !important;margin-right: auto !important;}.fluid-centered {margin-left: auto !important;margin-right: auto !important;}/* MOBILE GLOBAL STYLES - DO NOT CHANGE */body { padding: 0px !important; font-size: 16px !important; line-height: 150% !important;}h1 { font-size: 22px !important; line-height: normal !important;}h2 { font-size: 20px !important; line-height: normal !important;}h3 { font-size: 18px !important; line-height: normal !important;}.buttonstyles {font-family:arial,helvetica,sans-serif !important;font-size: 16px !important;color: #FFFFFF !important;padding: 10px !important;}/* END OF MOBILE GLOBAL STYLES - DO NOT CHANGE */}@media only screen and (max-width: 640px) {.container { width:100% !important; }.mobile-hidden { display:none !important; }.logo { display:block !important; padding:0 !important; }.photo img { width:100% !important; height:auto !important;}.nav5, .nav6 { display: none !important;}.fluid, .fluid-centered {width: 100% !important;max-width: 100% !important;height: auto !important;margin-left: auto !important;margin-right: auto !important;}.fluid-centered {margin-left: auto !important;margin-right: auto !important;}}</style><!--[if mso]><style type="text/css">/* Begin Outlook Font Fix */body, table, td {font-family: Arial, Helvetica, sans-serif ;font-size:16px;color:#000000;line-height:1;}/* End Outlook Font Fix */</style><![endif]--></head><body bgcolor="#ffffff" text="#000000" style="background-color: #ffffff; color: #000000; padding: 0px; -webkit-text-size-adjust:none; font-size: 16px; font-family:arial,helvetica,sans-serif;"><div style="font-size:0; line-height:0;"><custom name="opencounter" type="tracking"><custom name="usermatch" type="tracking" /></div><table width="100%" border="0" cellpadding="0" cellspacing="0" align="center"><tr><td align="center" valign="top"><custom type="header"/></td></tr><tr><td align="center"><table cellspacing="0" cellpadding="0" border="0" width="600" class="container" align="center"><tr><td><table class="tb_properties border_style" style="background-color:#FFFFFF;" cellspacing="0" cellpadding="0" bgcolor="#ffffff" width="100%"><tr><td align="center" valign="top"><table align="left" border="0" cellpadding="0" cellspacing="0" width="100%"><tr><!-- added padding here --><td class="content_padding" style=""><!-- end of comment --><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr> <!-- top slot --> <td align="center" class="header" valign="top"> <table align="left" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody> <tr> <td align="left" valign="top"> <table cellspacing="0" cellpadding="0" style="width:100%"> <tbody> <tr> <td class="responsive-td" valign="top" style="width: 100%;"> <table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="background-color: #000000; min-width: 100%; " class="stylingblock-content-wrapper"><tr><td style="padding: 10px; " class="stylingblock-content-wrapper camarker-inner"><table cellspacing="0" cellpadding="0" style="width: 100%;"><tr><td><table cellspacing="0" cellpadding="0" style="width: 100%;"><tr><td valign="top" class="responsive-td" style="width: 50%; padding-right: 3px;"><table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="background-color: #000000; min-width: 100%; " class="stylingblock-content-wrapper"><tr><td style="padding: 3px; " class="stylingblock-content-wrapper camarker-inner"><div> <span style="font-size:10px;"><span style="color:#ffffff;">Booking Confirmation</span></span></div></td></tr></table></td><td valign="top" class="responsive-td" style="width: 50%; padding-left: 3px;"><table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="background-color: #000000; min-width: 100%; " class="stylingblock-content-wrapper"><tr><td style="padding: 3px; " class="stylingblock-content-wrapper camarker-inner"><div style="text-align: right;"> <span style="font-size:10px;"><a alias="View email in browser" conversion="false" data-linkto="other" href="%%view_email_url%%" style="color:#808080;text-decoration:none;" title="View email in browser"><span style="color:#ffffff;">View email in browser</span></a></span></div></td></tr></table></td></tr></table></td></tr></table></td></tr></table><table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="background-color: transparent; min-width: 100%; " class="stylingblock-content-wrapper"><tr><td style="padding: 10px; " class="stylingblock-content-wrapper camarker-inner"><table cellspacing="0" cellpadding="0" style="width: 100%;"><tr><td><table cellspacing="0" cellpadding="0" style="width: 100%;"><tr><td valign="top" class="responsive-td" style="width: 45%; padding-right: 3px;"><table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="min-width: 100%; " class="stylingblock-content-wrapper"><tr><td class="stylingblock-content-wrapper camarker-inner"><table width="100%" cellspacing="0" cellpadding="0" role="presentation"><tr><td align="left"><img data-assetid="1289" src="http://image.email.hollyburn.com/lib/fe241171716404747c1d76/m/1/ed41fce3-4009-4227-a88f-7bc0295f8b99.png" alt="" height="47" width="200" style="display: block; padding: 0px; text-align: left; height: 47px; width: 200px; border: 0px;"></td></tr></table></td></tr></table></td><td valign="top" class="responsive-td" style="width: 55%; padding-left: 3px;"><table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="background-color: transparent; min-width: 100%; " class="stylingblock-content-wrapper"><tr><td style="padding: 17px 0px 0px; " class="stylingblock-content-wrapper camarker-inner"><div style="line-height: 100%; text-align: right;"> </div><div style="line-height: 100%;"> Unfurnished | Furnished | Blog | Contact Us</div></td></tr></table></td></tr></table></td></tr></table></td></tr></table> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </td> </tr> <tr> <!-- main slot --> <td align="center" class="header" valign="top"> <table align="left" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody> <tr> <td align="left" valign="top"> <table cellspacing="0" cellpadding="0" style="width:100%"> <tbody> <tr> <td class="responsive-td" valign="top" style="width: 100%;"> <table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="background-color: transparent; min-width: 100%; " class="slot-styling"><tr><td style="padding: 0px; " class="slot-styling camarker-inner"></td></tr></table> </td> </tr> <tr> <!-- main slot --> <td align="center" class="header" valign="top"> <table align="left" border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody> <tr> <td align="left" valign="top"> <table cellspacing="0" cellpadding="0" style="width:100%"> <tbody> <tr> <td class="responsive-td" valign="top" style="width: 100%;"> <table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="background-color: transparent; min-width: 100%; " class="stylingblock-content-wrapper"><tr><td style="padding: 32px; " class="stylingblock-content-wrapper camarker-inner"><strong>{{First_Name}},</strong><br> <br> We look forward to helping you find your new home at <b>{{Building_Name}}</b>. Please find your virtual viewing confirmation details below.<br> <br> <strong>Confirmation details:</strong><br> &nbsp;<br> <strong>Name: {{First_Name}} {{Last_Name}}</strong><br> <strong>Email: {{Email_Address}}</strong><br> <strong>Phone Number: {{Phone_Number}}&nbsp;</strong><br> <strong>Booking Date &amp; Time: {{Booking_Date}}<br> Booking Type: Virtual Viewing<br> Resident Manager :</strong>&nbsp;<b>{{RM_Name}}<br> Resident Manager Phone Number: -----</b><br> <strong>Building Address :</strong>&nbsp;<b>{{Building_Address}}</b><br> <br> <i>The Resident Manager will contact you prior to your appointment to determine your preferred&nbsp;method for this virtual viewing:</i><ul> <li> <i>Whatsapp</i></li><li> <i>Facetime</i></li><li> <i>Microsoft Teams</i></li></ul><b>Preferences:</b><br> <br> <strong>Budget: {{Budget}}<br> Unit Type: {{Unit_Type}}<br> Desired Move-in Date: {{Desired_Move_In}}<br> Additional Information: {{Additional_Info}}</strong><br> <br> Thank you,<br> Hollyburn Properties<br> &nbsp;<br> <strong>Cancellations:</strong><br> Should you need to cancel or reschedule your appointment, please contact the Resident Manager to let them know. We will do our best to accommodate your needs.<br> <br> <strong>Got questions? We&rsquo;re here to help!</strong><br> Email: <b>{{Leasing_advisor_email}}</b></td></tr></table><table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="background-color: #000000; min-width: 100%; " class="stylingblock-content-wrapper"><tr><td style="padding: 23px 10px 10px 20px; " class="stylingblock-content-wrapper camarker-inner"><div> <span style="font-size:14px;"><span style="color:#ffffff;">FOLLOW US ON</span></span></div></td></tr></table><table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="background-color: #000000; min-width: 100%; " class="stylingblock-content-wrapper"><tr><td style="padding: 0px 0px 0px 3px; " class="stylingblock-content-wrapper camarker-inner"><table cellpadding="0" cellspacing="0" class="socialshare-wrapper" width="100%"><tr><td align="left"><table cellpadding="0" cellspacing="0" align="left"><tr><td align="left"><!--[if mso]><table border="0" cellspacing="0" cellpadding="0"><tr><td style="padding-right:10px;"><![endif]--><table class="socialshare-innertable" style="display: inline-block"><tr><td style="padding:5px 10px"><a href="https://www.instagram.com/hollyburnproperties/" alias="instagram follow"><img src="https://image.s4.exct.net/lib/fe911573736c007d7d/m/2/11a4a1aa-4e8a-432a-8f1f-f8898206da39.png" alt="Instagram" width="24" height="24" style="display: block;; width: 24px !important; height: 24px !important"></a></td></tr></table><!--[if mso]></td><td style="padding-right:10px;"><![endif]--><table class="socialshare-innertable" style="display: inline-block"><tr><td style="padding:5px 10px"><a href="https://www.facebook.com/HollyburnPpty" alias="facebook follow"><img src="https://image.s4.exct.net/lib/fe911573736c007d7d/m/2/24b84e22-8d38-4d6c-98db-80812ca4de5f.png" alt="Facebook" width="24" height="24" style="display: block;; width: 24px !important; height: 24px !important"></a></td></tr></table><!--[if mso]></td><td style="padding-right:10px;"><![endif]--><table class="socialshare-innertable" style="display: inline-block"><tr><td style="padding:5px 10px"><a href="https://twitter.com/HollyburnPpty" alias="twitter follow"><img src="https://image.s4.exct.net/lib/fe911573736c007d7d/m/2/a14c2439-7025-4aab-a01f-423077193687.png" alt="Twitter" width="24" height="24" style="display: block;; width: 24px !important; height: 24px !important"></a></td></tr></table><!--[if mso]></td><td style="padding-right:10px;"><![endif]--><table class="socialshare-innertable" style="display: inline-block"><tr><td style="padding:5px 10px"><a href="https://www.youtube.com/user/HollyburnProperties1" alias="youtube follow"><img src="https://image.s4.exct.net/lib/fe911573736c007d7d/m/2/21512fbe-b12b-487d-b71f-5eb6388a49d1.png" alt="YouTube" width="24" height="24" style="display: block;; width: 24px !important; height: 24px !important"></a></td></tr></table><!--[if mso]></td><td><![endif]--><table class="socialshare-innertable" style="display: inline-block"><tr><td style="padding:5px 10px"><a href="https://www.linkedin.com/company/hollyburn-properties-limited" alias="linkedin follow"><img src="https://image.s4.exct.net/lib/fe911573736c007d7d/m/2/44cae9a3-eba9-48b9-911b-705f7777cd0e.png" alt="LinkedIn" width="24" height="24" style="display: block;; width: 24px !important; height: 24px !important"></a></td></tr></table><!--[if mso]></td></tr></table><![endif]--></td></tr></table></td></tr></table></td></tr></table><table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="background-color: #000000; min-width: 100%; " class="stylingblock-content-wrapper"><tr><td style="padding: 10px 20px 0px; " class="stylingblock-content-wrapper camarker-inner"><span style="font-size:12px;"><span style="color:#ffffff;">You received this email because you subscribed to updates from Hollyburn Properties. Want to take a break? Unsubscribed from our email updates.<br> Caution: Unsubscribing will terminate&nbsp;<i>all&nbsp;</i>Hollyburn emails, waitlists, and registrations. To Unsubscribe from just this list,&nbsp;<u><a alias="manage your subscription" conversion="false" data-linkto="other" href="%%profile_center_url%%" style="color:#FFFFFF;text-decoration:none;" title="manage your subscription">manage your subscription</a></u><br> <br> <font face="verdana">%%Member_Busname%%<br> %%Member_Addr%% %%Member_City%%, %%Member_State%%, %%Member_PostalCode%%, %%Member_Country%%</font></span></span><br> <br> <br> &nbsp;</td></tr></table> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> <tr> <td valign="top"> <custom type="footer" /> </td> </tr> </table> </body> </html>');
    j.writeEndObject();
    j.writeFieldName('start');
    j.writeStartObject();
    j.writeDateTimeField('dateTime', emailDetails.startDate);
    j.writeStringField('timeZone', 'Pacific Time Zone');
    j.writeEndObject();
    j.writeFieldName('end');
    j.writeStartObject();
    j.writeDateTimeField('dateTime', emailDetails.endDate);
    j.writeStringField('timeZone', 'Pacific Time Zone');
    j.writeEndObject();
    j.writeFieldName('location');
    j.writeStartObject();
    j.writeStringField('displayName', 'my location name here');
    j.writeEndObject();
    j.writeFieldName('attendees');
    j.writeStartArray();

    //adding attendees here
    for (Integer i = 0; i < emailDetails.toEmails.size(); i++) {
        if (emailDetails.toEmails[i] != null) {
        j.writeStartObject();
        j.writeFieldName('emailAddress');
        j.writeStartObject();
        j.writeStringField('address', emailDetails.toEmails[i]);
        j.writeEndObject();
        j.writeStringField('type', 'required');
        j.writeEndObject();
        }
    }

    j.writeEndArray();
    j.writeEndObject();

    String jsonBody = j.getAsString();
    //system.debug(jsonBody);
    sendRequest(jsonBody, 'https://graph.microsoft.com/v1.0/users/' + emailDetails.fromEmail + '/calendar/events', 'POST');

1 Answer 1

1

I solved my own problem.

The variable being read in the SOQL was being pre-escaped because it as a rich text field. I used unescapeHtml4() String function on it and now it renders properly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .