I have a requirement to upsert Policy records using JSON response obtained from external system.
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.
Update__
Below is the code block which I am using to update the existing policy records when the id is matched with unique id
list<policy__c> pollist = new list<policy__c>();
for (String eachIdFromMap : clsDataMap.keySet()){
if(!accountMap.isEmpty() && accountMap.containsKey(eachIdFromMap)){
for(policy__c pol : pollist){
pol.unique_id__c = clsDataMap.get(eachIdFromMap).id;
update pol;
}
} else {
system.debug('False');
}
}
I am not seeing any error but the records arent getting updated.
Thanks!
cls_data
and create a mapMap<Id, cls_data> (MapA)
, then you can do a SOQL with the map KeySet and fetch all the policies and fetch it like a mapMap<Id, Policy__c> (MapB)
., Then iterate over Map A, and check if the MapA's key exists on MapB, if yes then fire an update orelse fire an insert.