Salesforce

Update the Records in Salesforce Using the REST API

In this guide, we will discuss how to update the records in Salesforce with the REST API using the PATCH and PUT HTTP methods in Workbench. By creating a custom REST API using Apex, we update the records through Workbench URI. Also, we will update the records directly from the Workbench URI by specifying the “id” as parameter in the URI. First, we will look into the PATCH method.

PATCH Method

PATCH is an HTTP method which updates the existing records in Salesforce. We can update the records with URI through the custom Apex REST API or directly from URI.

Custom REST Resource in Apex

To write REST in Apex, we have to utilize some annotations which access the REST API in your Apex Class – @RestResource(urlMapping=’/version/ApexClassName/’).

@httpPatch Annotation

This method is called when the HTTP PATCH is sent which enables the Apex method to be exposed as a REST resource. Then, it updates the existing resource.

Syntax:

@httpPatch

Example:

Write the “RestApi_Patch_Record.apxc” Apex class that involves the REST “PATCH” method to update the description field in the Salesforce case object.

  1. Create the Patch_Method with one parameter which is “Description”.
  2. Create a case object by this parameter.
  3. Use the update DML to update the record in the Salesforce case object.
@RestResource(urlMapping='/v56.0/RestApi_Patch_Record/')

global class RestApi_Patch_Record{

 

// REST - Patch Method

@httpPatch

global static Case Patch_Method(String Description){

Map<String,String> id_param = RestContext.request.params;

String caseid=id_param.get('update_id');

Case case_obj= new Case(Description=description,Id=caseid);

// Update DML

update case_obj;

return case_obj;

}

}

URI & Result:

Go to Workbench and navigate to the REST Explorer. Specify the following URI and execute it by selecting the HTTP method as PATCH. Let’s update the 5002t00000PdzqtAAB case.

/services/apexrest/v56.0/RestApi_Patch_Record/?update_id=5002t00000PdzqtAAB

Specify the data in the request body:

{

"Description":"Seeking guidance on electrical wiring installation for GC5060"

}

Open the record in Salesforce (navigate to the case object from the App Launcher).

Update the Record Directly from URI

Let’s update the following record. Change the status from “Working” to “Closed” and the case origin from “Web” to “Email”.

Navigate to the REST explorer under the “Utilities” tab and specify the following URI by choosing PATCH as the HTTP method and specify this record under the request body.

/services/data/v56.0/sobjects/Case/5002t00000PdzqtAAB

Request Body:

{

"Status":"Closed",

"Origin":"Email"

}

Go to the “Salesforce Cases” tab and view the record. We can see that the status and case origin are updated to “Closed” and “Email”.

PUT Method

As we previously discussed, PUT is an HTTP method which is used to create/update the records. In this guide, we will utilize this method to update an existing Salesforce record using the custom REST API.

@httpPut: This method is called when the HTTP PUT is sent which enables the Apex method to be exposed as a REST resource. Then, it creates a new resource or update the existing resource.

Syntax:

@httpPut

Example:

Write the “RestApi_Put_Record.apxc” Apex class that involves the REST “PUT” method to update the case description from “Seeking guidance on electrical wiring installation for GC5060” to “GC5060”.

@RestResource(urlMapping='/v56.0/RestApi_Put_Record/')

global class RestApi_Put_Record{

 

// REST - Put Method

@httpPut

global static Case Put_Method(String Description){

Map<String,String> id_param = RestContext.request.params;

String caseid=id_param.get('update_id');

Case case_obj= new Case(Description=description,Id=caseid);

// Update DML

update case_obj;

return case_obj;

}

}

URI & Result:

Go to Workbench and navigate to the REST Explorer. Specify the following URI and execute it:

/services/apexrest/v56.0/RestApi_Put_Record/?update_id=5002t00000PdzqtAAB

Specify the data in the request body:

{

"Description":"GC5060"

}

Open the record in Salesforce (navigate to the case object from the App Launcher) and you will see that the description is updated to “GC5060”.

Conclusion

Now, you are able to update the existing records into Salesofrce using the HTTP PUT and PATCH methods. We learned how to update the records directly in the Workbench and use the custom REST API through the Apex class. The Salesforce object case is utilized in this guide as an example. The actual difference between PUT and PATCH is that PATCH is used to update the existing data, whereas PUT is used to insert/update the data. The @httpPut annotation is used for the PUT method and the @httpPatch annotation is used for the PATCH method.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain