1

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!

1
  • Iterate over cls_data and create a map Map<Id, cls_data> (MapA), then you can do a SOQL with the map KeySet and fetch all the policies and fetch it like a map Map<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. – Nagendra Singh Nov 20 '20 at 10:05
1

Iterate over cls_data and create a map Map<Id, cls_data> (MapA), then you can do a SOQL with the map KeySet and fetch all the policies and fetch it like a map Map<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.

For example something like this would work:

String str = '{"value":{"message":"Success","data":[{"id":"TestId","agentID":"TestAgenctId","agencyName":"TestAgencyName"},{"id":"TestId","agentID":"TestAgenctId","agencyName":"TestAgencyName"}],"code":200},"code":200}';

JSONParser1 objJsonParser = (JSONParser1) JSON.deserializeStrict(str, JSONParser1.class);

JSONParser1.cls_value clsValue = objJsonParser.value;
Map<String, JSONParser1.cls_data> clsDataMap = new Map<String, JSONParser1.cls_data>();

// Iterate from Object
for(JSONParser1.cls_data objClsData: clsValue.data){
    clsDataMap.put(objClsData.id, objClsData);
}

// Query on Policy
Map<ID, Policy__c> accountMap = new Map<ID, Policy__c>([SELECT Id, Name FROM Policy__c WHERE Id IN :clsDataMap.keySet()]);

// Iterate again and update or insert
for (String eachIdFromMap : clsDataMap.keySet()){
    if(!accountMap.isEmpty() && accountMap.containsKey(eachIdFromMap)){
        // Update
    } else {
        // Insert
    }
}
9
  • Thanks for the update @Narendra. To update policy__c object records, could you please let me know how to call field values from eachIdFromMap? Is it eachIdFromMap.agentid and eachIdFromMap.id? Please suggest – SFDCUser Nov 20 '20 at 10:48
  • As its a key from map, you need to fire clsDataMap.get(eachIdFromMap) which will give you SONParser1.cls_data, from there you can fetch the agentId and 'id' – Nagendra Singh Nov 20 '20 at 11:00
  • @NarendraSingh, I am getting this following error "Incompatible types since an instance of List<Policy__c> is never an instance of Map<Id,Policy__c>;" Could you please suggest any update here? – SFDCUser Nov 20 '20 at 11:24
  • Can you show some code what have you tried ? – Nagendra Singh Nov 20 '20 at 11:26
  • I havent made any changes to the given changes @Narendra except for variable change. I could see that the error is from Map<Id, Policy__c> accountMap line – SFDCUser Nov 20 '20 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.