3

I have a field that is used in an email template for which no FLS is granted in any profiles.

The Apex code that sends the email using messaging.sendEmail() is invoked from a trigger so I assume it to be running in system mode and that FLS is not relevant.

However, when that email is sent, and the trigger is executed by a record update, made when logged into a community as a user with profile BoringUser, the referenced field is not rendered into the email.

If I add read permissions for the System Administrator profile and perform the same operations. Log into community as BoringUser, cause a record update. Trigger fires and sends email, then the email received contains the expected merge field.

Why does granting read permission to the System Administrator profile influence field level access for code that should be running in System Mode?

5
  • 2
    one has to be a bit careful sometimes when logging into a community when already logged in as an employee. Can you verify the above when logging into the community in an incognito window as a BoringUser
    – cropredy
    Jan 24, 2020 at 19:58
  • @cropredy Thanks for the idea, unfortunately I have been testing this with a real login from an incognito window. No 'login as' involved.
    – RobD
    Jan 24, 2020 at 23:16
  • 1
    Is there a controller extension involved that makes the update to the record from the community that enters the trigger? Jan 27, 2020 at 23:32
  • Have you resolved this issue? I have very similar one and not sure who is the context of EmailTemplate generation... Even if sent from admin using anonymous apex it still seems to not have access to some records. Class already marked as without sharing Sep 12, 2022 at 15:50
  • Unfortunately I never reached a definitive understanding of what was going on. I suspect that the developers that built Salesforce know but it doesn't seem to be documented. Note that my observations were around FLS, not record sharing but it seems like its probably the same situation
    – RobD
    Sep 13, 2022 at 17:01

1 Answer 1

0

In order to make trigger run in System Mode you have to mark the class as without sharing (public without sharing class MyClass). This would give BoringUser access to the fields he shouldn't have only to send the email.

1
  • Thanks for the suggestion @Attilio Capocchiani as it happens the code concerned is annotated with withoutSharing already. I don't think that this is a factor though, sharing shouldn't affect FLS, only record visibility.
    – RobD
    Jan 27, 2020 at 10:08

You must log in to answer this question.

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