1

I'm trying to come up with a solution to build a PATCH service where the external service will only be patching data that has change in their external system, we want to do this to to limit the overhead in the service. Granted they are only sending one record, but this solution would cover all future patches so keeping that in mind they will only send changes in their external system.

I know I can build the end point to figure out what changed through mulitple IF statements but I would rather have an all encompassing solution.. so I don't want to do

if(newValue != null && oldValue != newValue){...}
if(newValue1 != null && oldValue1 != newValue1){...}

and So on..

I could also do a JSON parser while checking to see if that property is there, but I'd rather not do that because of the overhead and I'll have to rewrite the parser for every PATCH service.

I've messed around with a few POC's using instanceOf and I can figure out what type of data point they are sending me but figure out the rest of it will be a challenge.. I may not be able to get away from JSONParser though..

4
  • Can't the system just send the changed values? Then you wouldn't have to do any extra work?
    – sfdcfox
    Commented Feb 13, 2018 at 18:58
  • Yes, but when I deserlize the object the values that they don't send me will be null.. so I won't know if these values are actually null now or are just unchanged
    – EricSSH
    Commented Feb 13, 2018 at 19:00
  • 1
    I'm guessing you're using JSON.deserialize? That's part of your problem...
    – sfdcfox
    Commented Feb 13, 2018 at 19:05
  • Yup I am using that, can you point me in a new direction?
    – EricSSH
    Commented Feb 13, 2018 at 19:06

1 Answer 1

3

JSON.deserialize works great for the majority of situations, but assuming your payload contains only changed values, it might be worth the time to use JSON.deserializeUntyped:

Map<String, Object> jsonValues = (Map<String, Object>)JSON.deserializeUntyped(jsonString);
Map<String, String> fieldMapping = new Map<String,String> {
  'name' => 'Name',
  'Industry' => 'Industry',
  'customField' => 'Custom_Field__c',
  ...
};
sObject someRecord = new sObject(); // Replace with appropriate type
for(String key: jsonValues.keySet()) {
  someRecord.put(fieldMapping.get(key), jsonValues.get(key));
}
update someRecord;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .