You need to sign in to do that
Don't have an account?
update created by (ownerid) and last modified by in apex
I have created an integration user account with name Integration User. How can I update the created by (ownerid) and last updated by fields with the details of this integration user while doing a bulk dml insert in apex? Right 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;
We can only do this while inserting a record. You should activate the create audit fields for that. Please find the below article for more details on it.
https://help.salesforce.com/s/articleView?id=000331038&type=1 (https://help.salesforce.com/s/articleView?id=000331038&type=1)
If this solution helps, Please mark it as best answer.
Thanks,
public class JSON2Apex {
public String count__c {get;set;}
public String downloads__c {get;set;}
public JSON2Apex(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'count__c') {
count__c = parser.getText();
} else if (text == 'downloads__c') {
downloads__c = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'JSON2Apex consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
public static JSON2Apex parse(String json) {
System.JSONParser parser = System.JSON.createParser(json);
return new JSON2Apex(parser);
}
public static void consumeObject(System.JSONParser parser) {
Integer depth = 0;
do {
System.JSONToken curr = parser.getCurrentToken();
if (curr == System.JSONToken.START_OBJECT ||
curr == System.JSONToken.START_ARRAY) {
depth++;
} else if (curr == System.JSONToken.END_OBJECT ||
curr == System.JSONToken.END_ARRAY) {
depth--;
}
} while (depth > 0 && parser.nextToken() != null);
}
}
TEST CLASS:
@IsTest
public class JSON2Apex_Test {
// This test method should give 100% coverage
static testMethod void testParse() {
String json = '{\"count__c\":\"445\", \"downloads__c\":\"340\"}, {\"count__c\":\"440\", \"downloads__c\":\"240\"}';
JSON2Apex r = JSON2Apex.parse(json);
System.assert(r != null);
json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
JSON2Apex objJSON2Apex = new JSON2Apex(System.JSON.createParser(json));
System.assert(objJSON2Apex != null);
System.assert(objJSON2Apex.count__c == null);
System.assert(objJSON2Apex.downloads__c == null);
}
}