Recently I was having a requirement for creating REST patch API. Our vendors will call this REST API to update some of fields in our Salesforce org.
For this blog, i have taken example for account object record update. Vendor might be updating one field at time or multiple fields. As it external system was involved in this API interaction, instead of sending field API name they were sending request in normal JSON request. Like
{ "vendorName":"Salesforce Codex", "vendorWebsite":"salesforcecodex.com", "accountNumber":"1231" }
as we don’t have field companyWebsite in our Account object. I have created one mapping object which will map request attributes with our object’s field API.
So below object is created to map field
Object Name : API Mapper
FieldName | Type | Description |
APIName | Text | Name of API for which this mapping is being created like AccountPatch |
FieldName | Text | Name of Attribute in Patch Request |
MappedFieldAPI | Text | API Name of Field in our Org |
Vendor | Text | Vendor for which this request mapping is being done |
Let us create Patch API now. This will involve below steps
- Get Mapping data for API Mapper Object
- Get JSON request into Map object
- Map request attribute with Field API Name
- Create object for Update
1. Get Mapping data for API Mapper Object:
Create a Map set for all mapped field from our Org.
Listmappers=[select id,FieldName__c,MappedFieldAPI__c from APIMapper__c where name='AccountPatch']; Map mapFields=new Map (); if(mappers!=null && mappers.size()>0) { for(APIMapper__c mapObject:mappers) { mapFields.put(mapObject.FieldName__c,mapObject.MappedFieldAPI__c); } }
2. Get JSON request into Map object
Convert your JSON request in untyped set so that we can access its data in normal object concept. We can get account number value for which PATCH request is done.
Mapparams=(Map )JSON.deserializeUntyped(req.requestbody.tostring()); string accts =string.valueOf(params.get('accountNumber')); // get Account number to update record
3. Map request attribute with Field API Name
Now map request parameters with our mapped object fields. So that we can get field’s API name for object.
MapnewParams=new Map (); for(String fieldName : params.keySet()) { String val = mapFields.get(fieldName); if(string.isNotBlank(val)) { newParams.put(val,params.get(fieldName)); } }
4. Create object for Update
prepare and execute update request on account object.
Listaccounts=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; }
Complete Code:
Sample Mapping Records:
APIName | FieldName | MappedFieldAPI | Vendor |
AccountPatch | vendorName | Name | SFDC |
AccountPatch | middleName | Middle_Name__c | SFDC |
AccountPatch | vendorWebsite | Website | SFDC |
Testing above API using POSTMAN:
You can create Connected App and test it. Refer http://amitsalesforce.blogspot.com/2017/06/test-salesforce-api-by-postman-rest.html for detail.
This will only update Name and Website detail for account. If we JSON requst wth only vendorWebsite and accountNumber then it will only update website detail.
Reference: