To override the ‘Automated Process’ user by the desired user

  • Category : Configuration
  • Article Number : 15
  • Publish Date: 22 Dec, 2023

Objective

  • To replace/override the ‘Automated Process’ user by the desired user as per business requirement.

Solution

In general, when updates flow across the Salesforce org using the Platform Events, it updates the data as an ‘Automated Process’ user.

We can achieve it by using any of the below two ways.

  1. Using Metadata API through VSCode.

  2. Using tooling API (REST API).

Platform Event Name: ‘PlatformEventSubscriberConfig’

Using Metadata API through VSCode:

  1. Create a new folder inside ‘force-app\main\default’ ‘PlatformEventSubscriberConfigs’.

  2. Create a file under the above-stated folder and name it as

    ‘<Your Platform event subscriber trigger Name>

    Config.platformEventSubscriberConfig-meta.xml’.

For example, if you have an apex trigger called 'MyPlatformEventTrigger', the subscriber configuration file will be as below

Copy
'MyPlatformEventTriggerConfig.platformEventSubscriberConfig-meta.xml'.

Paste the following content into the above XML file:

Copy
<?xml version="1.0" encoding="UTF-8"?>

<PlatformEventSubscriberConfig xmlns="http://soap.sforce.com/2006/04/metadata">

<masterLabel>Sinergify_Platform_EventConfig</masterLabel>

<platformEventConsumer>Grz_Sf__Sinergify_Platform_Event</platformEventConsumer>

<user>[email protected]</user>

<batchSize>2000</batchSize>

<isProtected>false</isProtected>

</PlatformEventSubscriberConfig>

    Where

  • <platformEventConsumer>: Name of the apex trigger.

  • <batchSize>: Size of the batch.

  • <masterLabel>: Label for this config file.

  • <user>: Username of the user that should override the Automated Process.

Note: Trigger will execute as mentioned user.

  1. Deploy the above folder through the 'Package.xml' file in the manifest folder, as shown below.

  2. Verify if the ‘PlatformEventSubscriberConfig’ has been created by running the following query in Query Editor post deployment:

Copy
‘Select DeveloperName, MasterLabel,PlatformEventConsumerId, UserId, BatchSize from PlatformEventSubscriberConfig’

Important things to keep in mind:

  • Make sure the Tooling API is selected.

  • After the record has been created successfully, let it configure for approximately 10 minutes in the background.

  • Verify that the actual user is present during updates.

Note: Suspend and resume the trigger handler if an actual user is not present.

Follow the steps below if in case you need to Suspend and resume the trigger handler:

  1. Navigate to the Setup/ Platform Events / Select the desired Platform event.

  2. In the "Subscriptions" related list, click "Manage" next to the trigger, you can suspend and resume the trigger from this option.

Note: Resuming the trigger may take several minutes before it resumes processing events.

Using tooling API through REST API

We are using Workbench in our example, but you can use any REST API platform:

  1. Log in to Workbench / REST Explorer

  2. Use the below POST endpoint

Copy

/services/data/v51.0/tooling/sobjects/PlatformEventSubscriberConfig
  1. The JSON body should appear as shown below

Copy
{

"BatchSize": "2000",

"DeveloperName":"YourTriggerNameConfig",

"MasterLabel":"YourTriggerNameConfig",

"PlatformEventConsumerId": "01qXXXXXXXXX", //Apex Trigger’s ID

"UserId": "005XXXXXXXXXX"//Desired User’s ID

}
  1. If in case you see the error code: DUPLICATE_VALUE as shown in the screenshot below, follow the resolution below

    Resolution

  • Check your PlatformEventSubscriberConfig in the developer console to ensure that everything is properly configured (or delete it if you get the Duplicate ID error in Workbench).

  • Use the mentioned below query to Resolve the error code.

Copy

‘Select DeveloperName, MasterLabel,PlatformEventConsumerId, UserId, BatchSize from PlatformEventSubscriberConfig’