SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Wednesday, 13 November 2019

Salesforce Integration Tutorial Part 6 - Exploring PATCH Method

Hello Trailblazers,

Welcome to the 6th Tutorial in the Salesforce Integration Tutorial Series. In the previous tutorial, we learned about PUT method in detail. You can have a look at that tutorial by clicking here. In this tutorial, we'll have a look at the PATCH method in detail. We'll be creating a custom REST API that will interact with the contact object which is already present in Salesforce.

The PATCH method is mainly used to update fields in existing records. Therefore, it'll be associated with an update operation. So, we can send all the fields with the values in the request body which we need to udpate. Make sure you send the record id in this case, as we need to identify the record we want to update.

In this tutorial, we'll modify our code in the ContactResource class which has a URL mapping of /ContactAPI/* and will be updating the method annotation to @HTTPPatch and the method name to updateContact. Let's have a look at the below code:-

As you can see above, first of all, we are getting the request and assigning it to an instance of RestRequest class which is contactRequest. Then we got the requestURI from the contactRequest and using this, we're getting the Id of contact which is passed in the URL by getting the substring after last slash (/) from the requestURI. The contact id is assigned to the contactId variable. We're also getting the request body from the contactRequest and assigning it to the requestBody variable by converting it to String using the toString() method.

After that, we need to create an instance of Contact object that we're going to pass in the update operation. If the contactId string is not empty, we'll proceed further otherwise, we'll directly return the contact instance having null value. Inside the if we're deserializing the JSON using the JSON.deserialize() in which we pass the requestBody as the first parameter and the predefined Contact class as the second parameter which will be used to map the fields in order to deserialize. Then we assigned the contact id to the contact object using the contactId variable and finally, we performed the update operation on the contact. The updated contact is returned in the response in this case.

I created a record in Salesforce and tested this API from workbench.


As you can see above, this contact has a lead source of Phone Enquiry which I am going to update using workbench.



I've selected the PATCH method from the radio buttons, specified the record id in the URL after the last slash (/) and in the request body, I've just specified the data I want to update i.e. leadSource. I executed the request and got the updated contact object in the response showing the lead source as web. You can see above that the record id in the response is the same that we passed in the URL. I checked the same record in my Salesforce Org which is shown below:-


As you can see above, the lead source is updated to Web. Remember in the code, if we don't pass a contact id, the un-initialized contact instance will be returned in the response which has a value of NULL. I tried calling out the API without a contact id and got the below response:-


As you can see above, the API has returned a blank response, therefore it's even not formatted and nothing is shown in the raw response as well because our API returned NULL in the response body.

What if I want to update multiple contacts at once ?

In this case we need to take the similar approach like we did in case of DELETE method. Let's have a look at the below code of ContactResource1 class which has a URL mapping of /ContactAPI1/*

As you can see above, Everything is same like we did previously in case of delete, I have created the same wrapper class named as ResponseWrapper. I've updated the method name to updateContacts which is returning the list of ResponseWrapper in the response. Inside the method, we initialized a list of ResponseWrapper named as responseWrapperList. Then we again, got the input request in the instance of RestRequest class and parsed the request body as a String. Then we initialize a list of contacts named as contactsToUpdate which is assigned the deserialized input JSON. Using the List<Contact>.class as the apex type which is passed as a second parameter in the JSON.deserialize() method, this apex type is mainly the type of object that the method will create after deserializing the JSON input. In our case it's List<Contact> as we'll be sending the contact list in the input request body.

Once, We have the list of contacts to update, we use the Database.update() method with the allOrNone parameter (2nd parameter) as false to allow updation of valid records in the list. We got the results of the list update in the form of an array of Database.SaveResult. We've iterated the updateResults and created a response wrapper for each update call with the correct data (success or error messages) accordingly and added it to our responseWrapperList before ending the loop. Finally, we returned the responseWrapperList in the response.

I tested this API using workbench and used a list of two records in the input request body. The first record has other data but it's without an id so it should fail in an update operation whereas the second record is the one with the name Russ Hanneman that we created previously, I updated it's name to Richard Hendricks. After calling the API, I got the below results:-


As you can see in the response, the first record cannot be updated as specified in the error message that id is not specified in an update call. Whereas, the second record is updated with the same record id returned that we passed in the input JSON. I also checked in salesforce, and the record was updated there also as shown below:-


Tired of reading or just scrolled down, don't worry, you can watch the video too.



That's all for this tutorial, in the next tutorial, we'll be talking about creating test classes for a webservice class similar to those we created till now. All the code used in this tutorial is available in the patch branch of my github repository that you can access by clicking here. If you liked this tutorial, make sure to share it and let me know your feedback in the comments below.

Happy Trailblazing..!!

No comments:

Post a Comment