+ Start a Discussion
Bryan TelfordBryan Telford 

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": [
        "peerLoanAccess": "Disabled",
        "personalStatusOnline": true
        "userId": "zmoore",
        "firstName": "Zachary",
        "lastName": "Moore",
        "workingFolder": "1 - Leads-Prospects",
        "subordinateLoanAccess": "ReadOnly",
        "userIndicators": [
        "peerLoanAccess": "Disabled",
        "personalStatusOnline": true

Andrew GAndrew G

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
       create a new record;
       add to ListForInsert;
    End if
End Loop

Update ListforUpdate;
Insert ListForInsert;


Bryan TelfordBryan Telford
Thanks for the suggestions. I did some more research and found it was easier than I thought. Since the User_Id__c is an external Id field all I had to do was this. The key is the last line which informs the code that User_Id__c is an external id and to either insert or update based on that value.

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;
upsert personaAudit User_Id__c;

Andrew GAndrew G
Cool.  I didn't think to ask about external keys.
Glad you got it sorted.