Running Asynchronous Jobs in Delegated System Mode

To run a job in Delegated System Mode, you need to complete the following setups:

Configure Records in Foundations Configuration Items

Your administrator needs to set up the necessary records in Foundations Configuration Items. To do so:

  1. From Setup, navigate to Custom Metadata Types | Foundations Configuration Items.
  2. Click Manage records.

    Select the specific record that will be used to run your asynchronous job in Delegated System Mode.

    Note:

    The name of the record to be enabled will be specified in the product documentation. Some records will be enabled by default when the Delegated System Mode functionality is required.

  3. Select the Enabled field and click Save.
Note:

If you need to create Configuration Items for additional jobs, contact Certinia Customer Support. We recommend that you do not attempt to create configuration items without their guidance.

For more information about the Foundations Configuration Items, see Foundations Configuration Item Custom Metadata Type Fields.

Manage and Configure PlatformEventSubscriberConfig Setting

Properly configuring the platform is crucial to optimizing how platform events are processed in your Salesforce org. This setup enables you to control the batch size for event processing and specify the user context for your platform event Apex trigger, which is especially vital in high-volume scenarios.

Manage an Existing PlatformEventSubscriberConfig Setting or Delegated System User

If you already have a PlatformEventSubscriberConfig or a Delegated System User set up in your org, you must delete the existing configuration before creating a new one. This prevents conflicts and ensures a clean setup.

To delete the existing PlatformEventSubscriberConfig:

  1. Query for Existing Configurations
    1. In the Developer Console, open the Query Editor.
    2. Ensure the Use Tooling API field is selected.
    3. Execute the following query:
      Copy

                          SELECT id, developerName, platformEventConsumerId, batchSize, masterLabel, userId FROM PlatformEventSubscriberConfig
  2. Delete the Row

    1. In the query results, locate and click the row representing the PlatformEventSubscriberConfig you wish to delete.
    2. Click Delete Row.

Once the existing configuration is deleted, proceed with the steps outlined in the Configure a New PlatformEventSubscriberConfig to set up your new Delegated System User.

Configure a New PlatformEventSubscriberConfig

This process creates a new PlatformEventSubscriberConfig record and a packaged trigger.

  1. Open an Apex window in the Developer Console. Copy and paste the following script. Before running, ensure to customize the variables as described below:
    1. userName: Replace <USERNAME-OF-DELEGATED-USER> with the actual username of the delegated user. This is the user under whom the platform event trigger will execute.
    2. masterLabel: A descriptive label for this configuration.
      Copy

      APEX

      String userName = '<USERNAME-OF-DELEGATED-USER>'; // Replace with the username of the delegated user
      String ns = 'fferpcore';
      String platformEventConsumer = 'ffasync_AsyncFrameworkEventTrigger';
      Integer batchSize = 2000;
      String masterLabel = 'Certinia Delegated System Mode Config';
      Boolean isProtected = false;
      // Select the delegated user from the database
      User usr = [SELECT Id FROM User WHERE Username = :userName];
      // Select the packaged Apex Trigger
      ApexTrigger trg = [
      SELECT Id
      FROM ApexTrigger
      WHERE NamespacePrefix = :ns AND Name = :platformEventConsumer
      ];
      String json =
      '{' +
      '"developerName": "' +
      platformEventConsumer +
      'Config", ' +
      '"platformEventConsumerId": "' +
      trg.Id +
      '", ' +
      '"batchSize": ' +
      batchSize +
      ', ' +
      '"masterLabel": "' +
      masterLabel +
      '", ' +
      '"userId": "' +
      usr.Id +
      '"' +
      '}';
      String res = sendRequest(json);
      System.debug('RESPONSE=' + res);
      // Helper function to send the HTTP request
      String sendRequest(String body) {
      String endPoint =
      URL.getOrgDomainUrl().toExternalForm() +
      '/services/data/v51.0/tooling/sobjects/PlatformEventSubscriberConfig';
      Http h = new Http();
      HttpRequest req = new HttpRequest();
      req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
      req.setHeader('Content-Type', 'application/json');
      req.setHeader('Content-Length', String.valueOf(body.length()));
      req.setTimeout(60000);
      req.setEndpoint(endPoint);
      req.setBody(body);
      req.setMethod('POST');
      HttpResponse res = h.send(req);
      return res.getBody();
      }
  2. Run the prepared Apex script in the Apex window. This action creates the PlatformEventSubscriberConfig record.
  3. To ensure your changes take effect immediately, you need to activate the configuration:
    1. In Setup, navigate to Platform Events and click Async Framework Event.
    2. Under the Subscriptions section, find ffasync_AsyncFrameworkEventTrigger.
    3. Click Manage next to ffasync_AsyncFrameworkEventTrigger.
    4. Click Suspend.
    5. Click Resume.
  4. Suspending and then resuming the trigger immediately activates your new configuration. If you skip this step, the changes might take up to 24 hours to become active.

Enable Platform User as Delegated System User

After the PlatformEventSubscriberConfig settings are configured, you must enable the user intended to act as the delegated system user. For that, you can create a new user or use an existing user.

To do this:

  1. In the Setup, navigate to Custom Settings and click Delegated System Mode Settings.
  2. Click Manage.
  3. Click New. Create a custom setting record for the Delegated System Mode Settings scoped to the user specified in the PlatformEventSubscriberConfig. Ensure that it does not use the org-wide settings by default.
  4. Select the Is Platform User Field, which will specify that the user is a platform user.

For more information, see Delegated System Mode Settings.