function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RedmossSubC4iRedmossSubC4i 

Future Method Switching Between System and User Context

I feel like I'm going crazy with a future method that was running in user context this morning, then switched to system context in the afternoon, and now just switched back to user context again.  I have not made any updates or code changes so I'm pretty baffled.

Background:
I have a trigger/class that updates fields on Contacts associated with Customer Community User records and also uses a Future method to update the user profile depending on which type of Account they are placed.  This is triggered when a user updates the Account Name field on the Contact record.  Everything runs perfect except when the Future method is called.  If the user that invoked the trigger/class does not have "Manage Users" enabled on "System Permissions" in their profile then they get the following error: You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary.

I've tried configuring the class that contains the Future method as both "public class" and "public without sharing class", but still the Future method seems to run in User Context and users get that error.  The terrible solution, that has proven successful, is to grant those users the profile permission of "Manage Users", but that is much to dangerous to grant to non-admins.  All of the other permissions in the "System Permissions" section of "Users" must also be selected for that to be enabled on the profile and that is crazy to give to regular users.

Questions:
  1. Do Future methods run in User Context or System Context?
  2. How can I guarantee that a Future method will always run in System Context?
  3. What about the way these asynchornous methods or Future methods work that causes it to alternate it's behavior when it comes to which context it runs in (even in multiple sandboxes where no code changes were made)?
Any help or advice on this would be greatly appreciated!
Best Answer chosen by RedmossSubC4i
RedmossSubC4iRedmossSubC4i
I figured it out.  This seems to not be related to the Future method.  Rather, I was able to solve the issue by enabling the profile permission of "Edit Self-Service Users" under App Permissions.  If a user without that permission attempts to update a contact and move it into an account that has not already been enabled as a Customer Account, they will receive that error.  When a Customer Community contact is successfully added to an account that has not been enabled yet, the account is automatically enabled as a Customer Account.  Then from that point forward, users will not need the "Edit Self-Service Users" permission to associate additional Customer Community contacts to that account.  The "Edit Self-Service Users" permission seems to be needed so that non-admin users can associate customers to existing accounts without receiving a confusing error.  It's confusing because non-admin users will need additional permissions in order to have the "Manage External Account" button visible on the account record page.