update created by (...
 
Notifications
Clear all

[Solved] update created by (ownerid) and last modified by in apex

2 Posts
1 Users
0 Likes
344 Views
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago
I have created an integration user accountHow can I update the created by and last updated by fields with the details of this integration user while doing a bulk dml insert in apexRight now its showing the logged in users name when the apex class is run.
// records to be updated received from json
jsonBody = '[{"count__c":"445", "downloads__c":"340"}, {"count__c":"440", "downloads__c":"240"}]';

List<Data__c> dList = (List<Data__c>) System.JSON.deserialize(jsonBody, List<Data__c>.class);

countList has unique count__c values, say: 445,440 // to use in the IN clause.

// Querry parent for those plan ids in daily data json
List<Parent__c> parentList = [SELECT Id, Name FROM Parent__c 
    WHERE count__c IN :countList];

List<Data__c> dataToInsert = new List<Data__c>();

// Loop through dList - inner loop
for(Data__c dRecords : dList) {
     for(Parent__c parentRecords : parentList) {         
          if(dRecords.count__c  == parentRecords.count__c) {
                dRecords.downloads__c  = parentRecords.downloads__c ;
                dataToInsert.add(dRecords );
           }
      } 
} 
insert dataToInsert;

Salesforce API user for integrations. Create a specific api user account for integrations. This way records can be updated and created with this user account than any other logged in user's account?

Issue Tags
1 Reply
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago

 To make the integration user the context user for the apex initiated records inserts, etc., you need to run the Apex class as your integration user, which means:

  • Authenticating a remote system calling the API as the integration user.
  • Scheduling a Scheduled Apex class while logged in as the integration user.
  • Doing a one-off test of an Apex class while logged in as the integration user.

Client applications that access Salesforce through the API must first log in as a Salesforce user for authentication. Create a special user in your organization, solely for integration purposes. That way, if an actual user leaves your organization, you'll always have a user with the correct permissions available.

Learn more here: https://help.salesforce.com/s/articleView?id=000331470&type=1

Reply
Share: