Created
October 31, 2019 04:47
-
-
Save dhaniksahni/d7245e6e19fe0d65ea6ceb745bfd9cf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestResource(urlMapping='/v1/account/*') | |
global class AccountPatchAPI { | |
@HttpPatch | |
global static Account patch() { | |
RestRequest req = RestContext.request; | |
// Deserialize the JSON string into name-value pairs | |
// Iterate through each parameter field and value | |
List<APIMapper__c> mappers=[select id,FieldName__c,MappedFieldAPI__c from APIMapper__c where name='AccountPatch']; | |
Map<string,string> mapFields=new Map<string,string>(); | |
if(mappers!=null && mappers.size()>0) | |
{ | |
for(APIMapper__c mapObject:mappers) | |
{ | |
mapFields.put(mapObject.FieldName__c,mapObject.MappedFieldAPI__c); | |
} | |
} | |
Map<String, Object> params=(Map<String, Object>)JSON.deserializeUntyped(req.requestbody.tostring()); | |
system.debug('params:' + params); | |
string accts =string.valueOf(params.get('accountNumber')); // get Account number to update record | |
//Create new map with fields mapped in object | |
Map<String, Object> newParams=new Map<String, Object>(); | |
for(String fieldName : params.keySet()) { | |
String val = mapFields.get(fieldName); | |
if(string.isNotBlank(val)) | |
{ | |
newParams.put(val,params.get(fieldName)); | |
} | |
} | |
//Update Record using mapped value | |
system.debug('newParams:' + newParams); | |
List<Account> accounts=Database.query('select Id from account where AccountNumber=:accts'); | |
if(accounts!=null && accounts.size()>0) | |
{ | |
Account acct=accounts[0]; | |
for(String fieldName : newParams.keySet()) { | |
// Set the field and value on the Case sObject | |
acct.put(fieldName, newParams.get(fieldName)); | |
} | |
update acct; | |
return acct; | |
} | |
//update account; | |
return null;//account.Id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment