Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    • Enhancing Performance with File Compression in Apex
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Tuesday, May 20
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Create Dynamic Patch REST API in Salesforce Apex

    Create Dynamic Patch REST API in Salesforce Apex

    Dhanik Lal SahniBy Dhanik Lal SahniOctober 31, 2019Updated:January 13, 2025No Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Create Dynamic Patch REST API in Salesforce Apex
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Salesforce provides support for creating custom Salesforce APIs to handle business requirements. Businesses needed to update a few pieces of information in Salesforce. To handle such requirements we can use patch REST API. Client code canI call this REST API to update some of the fields in our Salesforce org.

    For this blog, i have taken an example for account object record update. A vendor might be updating one field at a 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<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);
        }
    }

    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<String, Object> params=(Map<String, Object>)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<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));
                }
    }

    4. Create object for Update

    prepare and execute update request on account object.

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

    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.

    Patch REST API

    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

    Similar Posts

    1. Ultimate Guide to Integrate Stripe with Salesforce CRM
    2. How to Seamlessly Integrate Shopify with Salesforce
    3. Shopify integration with Salesforce using Webhook
    4. Object Identification using Google Cloud Vision in Salesforce
    5. Logo Recognition using Google Cloud Vision in Salesforce
    6. Google FitBit Integration with Salesforce
    7. Integrate Google API with Salesforce
    8. Audio to Text by Google Speech API in Salesforce Lightning
    9. Extract Text From Images using Google Cloud Vision
    10. Seamless YouTube Video API Integration in Salesforce

    apex rest api web api
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleShow Image in Lightning Web Component Data Table
    Next Article Dependent PickList In Lightning Web Component
    Dhanik Lal Sahni
    • Website

    Related Posts

    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    By Dhanik Lal Sahni5 Mins Read

    How to Implement Dynamic Queueable Chaining in Salesforce Apex

    April 21, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) einstein (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) einstein (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.