HOW TO USE HTTP CALLOUT POST METHOD IN SALESFORCE FLOW ✅| Summer'23 Update ♨️

 


HTTP CALLOUT POST METHOD

In salesforce flow we have an HTTP CALLOUT action from which we can make http calls. In that action we already have GET METHOD which is now generally available.

In Summer'23 Release Updates, Salesforce has given us the POST METHOD as well. So now you can make http Post Calls with salesforce flows.

In today's use case we will learn how we can use http post method and get response from the server.

NOTE - Post Method is in Beta mode for now.

For today's example we will be using This Website to get a demo example of Request and Response for post method. We will talk about it later in our blog.

For creating an HTTP Call, there are few steps which we should complete before starting our flow.

  1. External Credential - An external credential represents the details of how Salesforce authenticates to an external system via an authentication protocol. It also links to a user's permission set.
  2. Permission Set - You need a permission set which will be linked to the external credential and also will be assigned to the user who needs to use the HTTP CALLOUT.
  3. Named Credential - A named credential specifies the URL of a callout endpoint and its required authentication parameters in one definition. A named credential can be specified as an endpoint to simplify the setup of authenticated callouts. Named credentials connect to external credentials.

Step 1 - External Credential -

for that go to SETUP > QUICK FIND BOX > NAMED CREDENTIAL

Click on named credential and you will see below screen -


Click on External Credentials > New


Fill the details in the form that appears when you click on new -


Label - Enter a label for your external credential.

Name - Name should be unique and should not have any spaces or special character.

Authentication Protocol - We will choose "Custom".

After you click on save, you will be redirected to the details page. On the details page, Scroll down a little and you will find "Principals" section.

Now, If you know salesforce has release their latest updates for Summer'23 and in those updates and enhancements they have changed the way we used to map permission sets to external credentials.

Before this release we have "Permission set Mapping" section in External Credential details page and from there we used to map our permission set to the credential.

Now, they have added a new section in the external credential details page which is "Principals".
The process of mapping a permission set has changed in the Summer'23 updates.

We will not go into the details for that here, If you want to learn about the difference or if you are a bit confused then I would recommend you to go through this video below -




Let's go to the Principals section and create a new Principal.


Parameter Name - Enter a unique parameter name for your permission set.

Sequence Number - Enter a number in sequence. If this is your first principal then add 1 and increase it by one in another principal. Make sure the number is unique and greater than 0.

Authentication Parameters - If you wish you can add them but they are not mandatory.

Click on save and your principal will be created.

Step 2 - Permission Set - 

For a user who need to use the External Credential and Named Credential, we have a create a permission set, map it with the External Credential and assign it to our user.

Go to SETUP > PERMISSION SET > NEW


Label - Enter a label name to identify the permission set.

API Name - Enter an API Name which should be unique, does not contain any spaces and associated with Label.

Description - Always provide a short description so that anyone could understand what it is all about.

Click on save and your permission set is created. You will be redirected to the details page. On that go to "App" Section and you will find "External Credential Principal Access" this option.


Click on this link and then click on the "Edit" button.

Then select your external credential principal, click on the add button and save.


You have successfully mapped your permission set to external credential. Now, Assign this permission set to your user.

Go to MANAGE ASSIGNMENTS > SELECT YOUR USER AND SAVE.

Step 3 - Named Credential - 

For creating a Named Credential Go to SETUP > NAMED CREDENTIALS > NEW


Label - Enter a Unique Label for Named Credential.

Name - Enter a Name without any spaces or special characters.

URL https://restful-api.dev (This is the url of the website that we are using for this example)

External Credential - Select the credential that we have created in Step 1.

Callout Options - Uncheck "Generate Authorization Header" because we are not creating any header.

Click on save and you are good to go.

Well done! you have done all the pre steps for HTTP CALLOUT POST METHOD.

Now, Let's create our flow -

Here is the flow that we are going to create -


Step 1 - Take an Action Element and click on the "Create HTTP Callout" button in the bottom left corner.


After that it takes you to "Create External Service" popup. Enter a name and select the named credential that we have created earlier.

Once you select the named credential, you will see the url we have added under that.

Click on next and it will ask you to "Configure Invocable Action". 
Enter a unique label and select the desired method.
In our case Select "Post" method.


Enter a url path, if you have any -


Go to the website that we mentioned earlier, scroll a little and click on the POST method. This is what you will find -


Here, Under request they have given the url. If you see we have a path in it as well.

So in this, this is the complete URL - https://api.restful-api.dev/objects

Base Url - https://restful-api.dev

Path - /objects

Copy the path and paste it in the Url Path section.
After that scroll to the bottom and it asks you to add Sample Request and Response for the post method.

Copy the request from the website and paster it here. Click on the Review button and it will create a Data Structure for you.

Here is the request -

{
   "name": "Apple MacBook Pro 16",
   "data": {
      "year": 2019,
      "price": 1849.99,
      "CPU model": "Intel Core i9",
      "Hard disk size": "1 TB"
   }
}


Do the same for Sample Response as well.

Here is the Response -

{
   "id": "7",
   "name": "Apple MacBook Pro 16",
   "data": {
      "year": 2019,
      "price": 1849.99,
      "CPU model": "Intel Core i9",
      "Hard disk size": "1 TB"
   },
   "createdAt": "2022-11-21T20:06:23.986Z"
}


Now, Add Label and Api for the Action Element. For action body we need to create an Apex-Defined variable.


Have a look at the variable attributes -


Resource Type - Variable

API Name - Enter a unique API Name.

Description - Add a short description about the variable.

Data Type - Apex-Defined

Apex Class - Choose the apex class which has your external service name and also has "IN_Body" at the end.

In our example, the apex class name is "ExternalService__HttpCalloutPostService_Httpx20Calloutx20Postx20Action_IN_body"

Click on done and your work for action element is all set.

Now, We need to add values to the Apex Defined variable that we have just created. For that we will add an Assignment Element right before the Action Element.


In assignment element, Assign values in the ActionBodyVar variable from the request data.

Here is the request data. You can copy it from the website as well.

{
   "name": "Apple MacBook Pro 16",
   "data": {
      "year": 2019,
      "price": 1849.99,
      "CPU model": "Intel Core i9",
      "Hard disk size": "1 TB"
   }
}

Have a look at the assignment element -


Here I have added data in all the variables which are in our sample request.
Click on Done and your flow is ready to go.

Now, Debug the flow and in the debug details you will get the desired response.

If you wish to learn this example with a tutorial then please follow this video below -





If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Let's learn and grow together.
Love and Peace! 

Post a Comment

4 Comments

  1. Thank you! I was just searching for this.

    ReplyDelete
  2. Hi not able to get the class part can you tell me more about how to define class and what need to be written in that.

    ReplyDelete
    Replies
    1. Hi, the class will be automatically created once you add your external service. You just have to choose the apex class which has your external service name and also has "IN_Body" at the end.

      Delete