• DC-
  • NEWBIE
  • 0 Points
  • Member since 2017
  • Visier

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I've come across a weird issue in Salesforce where merge fields on a VF email template are not being resolved correctly when it is sent by "System". I've tried both a Workflow Email Alert on create and regular assignment alert (Support Settings > Case Assigned Template). Both fail to correctly resolve all fields when it is ran by System (i.e. when System handles an email-to-case event).

The exact same template manage to resolve all fields properly when I create a case (i.e. CreatedBy != System), and update the existing case's assignment (the one created by System). In both cases, the email merged correctly whether it was sent via Workflow, and manual case re-assignment, and template preview and all fields were merged correctly. 

It seems that when the Standard Controller used for email templates when executing under the System user, it is only able to pull certain fields. 

Testing template for reference:
<messaging:emailTemplate subject="{!'A Ticket has been assigned to you: '+relatedTo.Subject+ '('+relatedTo.CaseNumber+')')}" recipientType="User" relatedToType="Case">
<messaging:htmlEmailBody >
        <p>Hello {!relatedTo.Owner.Name},</p>
        <p>A support ticket has been assigned to you: </p>
        <p>
        Company: {!relatedTo.Account.Name} <br />
        Contact Name: {!relatedTo.Contact.Name} <br />
        Contact Email: {!relatedTo.Contact.Email} <br />
        <br />
        Case #: {!relatedTo.CaseNumber} <br />
        Case ref: {!relatedTo.Case_Reference__c} <br />
        Priority: {!relatedTo.Priority} <br />
        Subject: {!relatedTo.Subject} <br />
        Description: {!relatedTo.Description} 
        </p>
</messaging:htmlEmailBody>
</messaging:emailTemplate>

Case Number, Owner, Case Id are the only merge fields that are resolved when running as System. 

Although redundant, I've also verified that System does indeed have access to these fields via an Apex Test using runAs:
 
User systemUser = [select id from user where name = 'System' limit 1];
		System.runAs(systemUser)
		{
			for(String field : Schema.SObjectType.Case.fields.getMap().keySet())
			{
				Map<String, SObjectField> fieldMap = Schema.SObjectType.Case.fields.getMap();
				System.debug(LoggingLevel.DEBUG, field + ' is Accessible by System: ' + fieldMap.get(field).getDescribe().isAccessible());
			}
		}

The debug logs show the correct values being accessible by System. 

Does any one know why the fields aren't resolved when System is the one sending the emails?
  • August 29, 2019
  • Like
  • 0
I've come across a weird issue in Salesforce where merge fields on a VF email template are not being resolved correctly when it is sent by "System". I've tried both a Workflow Email Alert on create and regular assignment alert (Support Settings > Case Assigned Template). Both fail to correctly resolve all fields when it is ran by System (i.e. when System handles an email-to-case event).

The exact same template manage to resolve all fields properly when I create a case (i.e. CreatedBy != System), and update the existing case's assignment (the one created by System). In both cases, the email merged correctly whether it was sent via Workflow, and manual case re-assignment, and template preview and all fields were merged correctly. 

It seems that when the Standard Controller used for email templates when executing under the System user, it is only able to pull certain fields. 

Testing template for reference:
<messaging:emailTemplate subject="{!'A Ticket has been assigned to you: '+relatedTo.Subject+ '('+relatedTo.CaseNumber+')')}" recipientType="User" relatedToType="Case">
<messaging:htmlEmailBody >
        <p>Hello {!relatedTo.Owner.Name},</p>
        <p>A support ticket has been assigned to you: </p>
        <p>
        Company: {!relatedTo.Account.Name} <br />
        Contact Name: {!relatedTo.Contact.Name} <br />
        Contact Email: {!relatedTo.Contact.Email} <br />
        <br />
        Case #: {!relatedTo.CaseNumber} <br />
        Case ref: {!relatedTo.Case_Reference__c} <br />
        Priority: {!relatedTo.Priority} <br />
        Subject: {!relatedTo.Subject} <br />
        Description: {!relatedTo.Description} 
        </p>
</messaging:htmlEmailBody>
</messaging:emailTemplate>

Case Number, Owner, Case Id are the only merge fields that are resolved when running as System. 

Although redundant, I've also verified that System does indeed have access to these fields via an Apex Test using runAs:
 
User systemUser = [select id from user where name = 'System' limit 1];
		System.runAs(systemUser)
		{
			for(String field : Schema.SObjectType.Case.fields.getMap().keySet())
			{
				Map<String, SObjectField> fieldMap = Schema.SObjectType.Case.fields.getMap();
				System.debug(LoggingLevel.DEBUG, field + ' is Accessible by System: ' + fieldMap.get(field).getDescribe().isAccessible());
			}
		}

The debug logs show the correct values being accessible by System. 

Does any one know why the fields aren't resolved when System is the one sending the emails?
  • August 29, 2019
  • Like
  • 0