You need to sign in to do that
Don't have an account?
sksfdc221
Upserting records from JSON
I have a requirement to upsert Policy records using JSON response obtained from external system.
below is my apex code:
I have a JSON Parser class and it is as follows:
Now, from the above code peice, ID is unique and if the id matches with existing Policy__c's Unique_ID__c field value, the record should be updated. Else, the record should be inserted.
Can anyone please let me know on how to do so.
Thanks!
below is my apex code:
HttpRequest req = new HttpRequest(); req.setMethod('GET'); req.setTimeout(120000); req.setEndpoint('<endpoint>'); req.setHeader('Authorization', 'Bearer ' + '<bearer token>'); Http http = new Http(); HTTPResponse res = http.send(req); String response = res.getBody(); JsonParser jsondes = (JsonParser) JSON.deserialize(response, JsonParser.class);
I have a JSON Parser class and it is as follows:
public class JSONParser{ public Integer code; public cls_value value; public class cls_value { public cls_data[] data; public String message; public Integer code; } public class cls_data { public String agencyName; public String agentID; public string id; } }
Now, from the above code peice, ID is unique and if the id matches with existing Policy__c's Unique_ID__c field value, the record should be updated. Else, the record should be inserted.
Can anyone please let me know on how to do so.
Thanks!
You can use DML upsert sattement or Database upsert method.
DML Statement -> upsert listOfRecords Unique_ID__c
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_upsert.htm
Database upsert method -> Database.upsert(listOfRecords ,Policy__c.Fields.Unique_ID__c,true)
https://developer.salesforce.com/forums/?id=906F0000000908XIAQ
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database.htm
https://salesforce.stackexchange.com/questions/326856/upsert-records-after-parsing-json-response
If it helps close this thread,So that we can keep the community clean.