You need to sign in to do that
Don't have an account?
Help with inserting/updating records
I have to parse a JSON response and either insert or update records, depending on finding a matching userId in the JSON for existing records in Salesforce. I have setup a wrapper class using https://json2apex.herokuapp.com/ to help with deserializing the JSON.
My code where I deserialize the JSON:
List<encompassRESTUsers> users = (List<encompassRESTUsers>)JSON.deserialize(JSONResponse, List<encompassRESTUsers>.class);
I then create a list of the current records in the database, so that I have something to loop through and compare with the list I got from the JSON.
Persona_Audit__c[] currentUserList = [Select User_Id__c, id from Persona_Audit__c];
Here is where I am stuck. I know that I need to iterate through the deserialized JSON. Then I need to find out if the userId in the JSON already exists in a record. If it does, then I need to update that record. If not, then I need to create a new record. I'm seeing something like this but I don't know what to put in the for loops.
for (encompassRESTUsers u: users) {
}
for (Persona_Audit__c theList : currentUserList) {
}
Below is an example of the JSON.
[{
"userId": "jsmith",
"firstName": "Smith",
"lastName": "Joseph",
"workingFolder": "2 - Originated Applications",
"subordinateLoanAccess": "ReadOnly",
"userIndicators": [
"Locked",
"Disabled"
],
"peerLoanAccess": "Disabled",
"personalStatusOnline": true
},
{
"userId": "zmoore",
"firstName": "Zachary",
"lastName": "Moore",
"workingFolder": "1 - Leads-Prospects",
"subordinateLoanAccess": "ReadOnly",
"userIndicators": [
"Locked",
"Disabled"
],
"peerLoanAccess": "Disabled",
"personalStatusOnline": true
}
]
My code where I deserialize the JSON:
List<encompassRESTUsers> users = (List<encompassRESTUsers>)JSON.deserialize(JSONResponse, List<encompassRESTUsers>.class);
I then create a list of the current records in the database, so that I have something to loop through and compare with the list I got from the JSON.
Persona_Audit__c[] currentUserList = [Select User_Id__c, id from Persona_Audit__c];
Here is where I am stuck. I know that I need to iterate through the deserialized JSON. Then I need to find out if the userId in the JSON already exists in a record. If it does, then I need to update that record. If not, then I need to create a new record. I'm seeing something like this but I don't know what to put in the for loops.
for (encompassRESTUsers u: users) {
}
for (Persona_Audit__c theList : currentUserList) {
}
Below is an example of the JSON.
[{
"userId": "jsmith",
"firstName": "Smith",
"lastName": "Joseph",
"workingFolder": "2 - Originated Applications",
"subordinateLoanAccess": "ReadOnly",
"userIndicators": [
"Locked",
"Disabled"
],
"peerLoanAccess": "Disabled",
"personalStatusOnline": true
},
{
"userId": "zmoore",
"firstName": "Zachary",
"lastName": "Moore",
"workingFolder": "1 - Leads-Prospects",
"subordinateLoanAccess": "ReadOnly",
"userIndicators": [
"Locked",
"Disabled"
],
"peerLoanAccess": "Disabled",
"personalStatusOnline": true
}
]
If you are chasing thoughts on Psuedo code level: with code snippets in Italic
Deserialise the JSON into a encompassUserList (as done)
Declare eUserMap<uId, Object>
Loop the encompassUserList and populate the Map
map<id, encompassRESTUsers> eUserMap= new map<id, encompassRESTUsers>();
for ( encompassRESTUsers eU: users ){
eUserMap.put(eU.userId, eU);
}
Create a Set of the UserIds
Set <String> userIds = new Set<String>(); userIds = eUserMap.keySet();
Get list of users that will related to the JSON users being passed in
currentUserList = [SELECT User_Id__c, id FROM Persona_Audit__c WHERE User_Id__c in :userIds];
Loop the Map
for (String key : Map.Keyset())
If map.key in currentUserList
if(currentUserList.contains(key) )
update record details;
add to ListforUpdate
Else
create a new record;
add to ListForInsert;
End if
End Loop
Update ListforUpdate;
Insert ListForInsert;
HTH
Andrew
Persona_Audit__c[] personaAudit = new Persona_Audit__c[]{};
for (encompassRESTUsers u: users) {
Persona_Audit__c user = new Persona_Audit__c();
user.User_Id__c = u.id;
user.Email__c = u.email;
personaAudit.add(users);
}
upsert personaAudit User_Id__c;
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_upsert.htm
Glad you got it sorted.
Regards
Andrew