Can we specify “without sharing” in Visualforce email templates?

We have a workflow rule that triggers when ownership of a record changes and certain conditions are met– part of the criteria is ISCHANGED(OwnerId), which causes the workflow to perform its actions. The action in question is to send an email notification to the new owner, which is done through a Visualforce template, because we’d like to control the output without the limitations of an HTML or text template.

When the ownership of the record changes from one user to another, it’s entirely possible that the original user will no longer have access to the record. If this occurs, the email template crashes and rolls back the entire transaction. There’s another question with a self-answer that would work, except we can’t use sharing rules because that would expose data that can’t be exposed to certain groups of users.

Of course, we could build our own email “template” and send it through code instead of a workflow, but this would reduce our ability to modify the template, as we have a two-month release cycle, and we can only deploy code changes during the regularly scheduled release cycle (an approximately eight hour window) without requiring approval from a dozen different managers– configuration changes, such as email templates, aren’t subject to this limitation.

So, getting back to the title: Can we enable a Visualforce template to not crash when sending an email on a record that the user no longer has access to?

Answer

Not the main template’s body as it can’t have a controller.

You can try moving your code to VF component and attach a controller to it. But I feel it’d work for other things related to the main record (when explicitly queried + without sharing), not when the main “relatedTo” becomes invisible…

It might even be that it’s not the email template usage that fails. What if it’s the act of trying to attach a completed Task to thing I can no longer see? :/

Attribution
Source : Link , Question Author : sfdcfox , Answer Author : eyescream

Leave a Comment