Home Salesforce Create Dynamic Patch REST API in Salesforce Apex

Create Dynamic Patch REST API in Salesforce Apex

by Dhanik Lal Sahni

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 

FieldNameTypeDescription
APINameTextName of API for which this mapping is being created like AccountPatch
FieldNameTextName of Attribute in Patch Request
MappedFieldAPITextAPI Name of Field in our Org
VendorTextVendor for which this request mapping is being done

Let us create Patch API now. This will involve below steps

  1. Get Mapping data for API Mapper Object
  2. Get JSON request into Map object
  3. Map request attribute with Field API Name
  4. Create object for Update

1. Get Mapping data for API Mapper Object:

Create a Map set for all mapped field from our Org.

List mappers=[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.

Map params=(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.

Map newParams=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.

List 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;
}

Complete Code:

Sample Mapping Records:

APINameFieldNameMappedFieldAPIVendor
AccountPatchvendorNameNameSFDC
AccountPatchmiddleNameMiddle_Name__cSFDC
AccountPatchvendorWebsiteWebsiteSFDC

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:

https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_webservices

You may also like

Leave a Comment