Calling Salesforce Rest API from Zapier

Telegram logo Join our Telegram Channel

Zapier has a new feature that enables us to call custom Apex Rest APIs, this feature can be very handy for businesses that want to leverage the easiness of Zapier and the flexibility of Apex Rest APIs.

Let's understand this with example, you have a requirement to send data from clients on-premise systems to salesforce. To bring data into Zapier we will use webhook and then we will feed that data into Salesforce using custom Rest APIs.

Let us build a Rest API class that has a post handler to create a contact with the provided data. Here is the sample data that we will send to the Zapier webhook.

{
  "contactRecord": {
    "GenderIdentity": "Male",
    "FirstName": "Rahul",
    "LastName": "Gawale",
    "Email": "rahul@forcetrails.com"
  }
}

Here is the sample rest class. Class logic is simple, it will try to find the contact based on the provided email address and update it. If contact is not found it will create a new.

@RestResource(urlMapping='/new-contact/*')
global class NewContactRest {
    @HttpPost
    global static void doPost(Contact contactRecord) {
        System.debug('contactRecord: ' + contactRecord);
        
        //find existing contact with specified email id
        Id conId = [SELECT Id FROM Contact WHERE Email = :contactRecord.Email LIMIT 1]?.Id;
        contactRecord.Id = conId;
        upsert contactRecord;
        
        RestContext.response.statusCode = 200;
        RestContext.response.responseBody = Blob.valueOf(JSON.serialize(contactRecord));
    }
}

Now that your custom Apex Rest API is ready let us connect it with Zapier. Follow the below steps and refer to the images.

  1. Log in to Zapier and create a new Zap by selecting the webhook as the triggering point.
  2. Select Event "Catch Hook" from the right sidebar.
  3. Now it is time to bring Salesforce in, click on step 2 and select "Salesforce (Premium)" option.
  4. Once you select Salesforce action, you will see the event option on the right sidebar, select "API Request (Beta)" from the available options. Select this option if you are calling a custom Rest API. You can choose standard APIs as well.
  5. Connect your Salesforce Account with Zapier, if not already connected, and click continue.
  6. Now, select the HTTP method that you want to use, (here I am using POST) and then enter the Salesforce API URL. In this format https://<org-base-url>/services/apexrest/<url-mapping>/.
    1. You can find the org base URL in "My Domain" settings in Salesforce.
    2. URL mapping is the same as you mentioned in the Rest Class, in my case, it is new-contact/.
    3. This is what the final URL looks like: https://mysbx--devsandbox.sandbox.my.salesforce.com/services/apexrest/new-contact/.
    4. See the image below for reference.
  7. Add header information, i.e. content-type = application/json.
  8. Now it's time to map the data, go ahead and send some test data to the Zapier webhook. You can send it from the actual system or any other Rest client like Postman.
    1. From the Zap select Catch Hook In Zapier Wehbook step.
    2. Go to the test section and see if the test data is visible.
    3. Once visible select a desirable test request and click "Continue with selected record". See the image below.
  9. Now go to the Salesforce action of Zap and copy-paste the request JSON.
  10. Map the webhook data to JSON. Go ahead and map each field one by one from the insert data section. 
  11. I have mapped all the fields, this is what my final mapping looks like. Remember I have added zap-data inside the double quotes.
  12. There you go. Well done! Now you can test the step and publish your Zap.

Considerations

  • This is an oversimplified example you might need to read the request body as a string and process it from the Apex class.
  • Make sure that data received into Zap is clean because Zapier is not very good at validating the data.
  • Select the test record carefully and make sure you have mapped all the required fields.
  • Once published test your zap thoroughly.


Comment down if you have any questions? Feel free to discuss any issues. Thanks for visiting.


No comments :
Post a Comment

Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. Sorry for the inconvenience but we have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, email us at rahul@forcetrails.com