Apex · Integration · REST · Salesforce

Google Assistant Integration with Salesforce – Part 2

Let suppose when the Intent creation is ready, you want the custom data as a reply in Google assistant, we overwrite the intent from Apex call. You can add a custom response from Salesforce to Google assistant. We fetch the record names from the Salesforce org and overwrite the response in the intent.

global class restCall {
    global static string fetchRecords(){
	//response from Google Assistant as a JSON
        String request = RestContext.request.requestBody.toString();
        mapurl orp = (mapurl)JSON.deserialize(request, mapurl.class);
        string str=orp.result.metadata.intentName;
        string givenInt=orp.result.parameters.number1;
	string outputstring='';
        String accountList='';
	if((str.contains('New')||(str.contains('Add')) ||(str.contains('Create'))) &&(str.contains('Account'))){
            outputstring='Congratulations Account is created successfully';
            string lastname=orp.result.parameters.Name;
            string phone=orp.result.parameters.phone;
            string email=orp.result.parameters.Email;
            account acc= new account();
            insert acc;
        else if((str.contains('get ')) && (str.contains('Account'))){
            Integer num =  Integer.valueof(givenInt);
            accList = [select name from Account order by createddate limit :num];
            for(Account a: accList)
                accountList=accountList+ ' ' + a.name;
        String s= 'Success';
        return s;
    global class mapurl{
        global result result;
    global class result{
        global parameters parameters;
        global metadata metadata;
        global string resolvedQuery;
    global class parameters{
        global String number1;
        global String Phone;
        global String Name;
        global String Email;
    global class metadata{
        global String intentName;

Access token to be used in salesforce org is as shown below:


So when a response is received from Google assistant we make a call to an apex method to edit the intent. The below class is to edit the intent with our data from salesforce.

In the below code you can see a string that is passed to request.setBody where you can try to get it from postman or some related apps. In the JSON we edit our intent data that should overwrite the data in the Google Intent.

global class postWebhook {

	global static void postCallout(string outputstring){
        string accessToken='Here we have to use the developer Access token';
        //The below Text is the response from postman. dont use the same it will throw error
string text = '{"id": "332b2bf0-a53e-4b3e-b03c-986cbe8ee00e","name": "yes","auto": true,"contexts": [],"responses": [{"resetContexts": false,"affectedContexts": [],"parameters": [],"messages": [{"type": 0,"speech": "'+str1+'"}],"defaultResponsePlatforms": {},"speech": []}],"priority": 500000,"cortanaCommand": {"navigateOrService": "NAVIGATE","target": ""},"webhookUsed": false,"webhookForSlotFilling": false,"lastUpdate": 1526896421,"fallbackIntent": false,"events": [],"userSays": [{"id": "1ff25907-2b72-4866-ae96-c1dca66e9747","data": [{"text": "sure"}],"isTemplate": false,"count": 0,"updated": 1526896421,"isAuto": false},{"id": "b95c226f-4b36-4ba3-849d-824fb44a0bc0","data": [{"text": "yes"}],"isTemplate": false,"count": 0,"updated": 1526550631,"isAuto": false}],"followUpIntents": [],"endInteraction": True,"templates": []}';
        Http http  = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint('Here you have to set the endpoint URL which is Dialogflow Api URL');
        request.setHeader('content-type', 'application/json');
        request.setHeader('Accept-Language', 'application/json');
        request.setHeader('Authorization', 'Bearer '+accessToken);
        HTTPResponse response = new HTTP().send(request);
    global static void postCalloutquery(string accountList){
        string accessToken='241b07a2d7eb4f82a31158bb6e310d35';
        string text = '{"id": "2b641bce-bdf7-4b6c-8637-0388a05f71af","name": "True","auto": true,"contexts": [],"responses": [{"resetContexts": false,"affectedContexts": [],"parameters": [],"messages": [{"type": 0,"speech": "'+ listOf +'"}],"defaultResponsePlatforms": {},"speech": []}],"priority": 500000,"cortanaCommand": {"navigateOrService": "NAVIGATE","target": ""},"webhookUsed": false,"webhookForSlotFilling": false,"lastUpdate": 1526900788,"fallbackIntent": false,"events": [],"userSays": [{"id": "06833ebd-5581-4b35-b23b-c655231605ad","data": [{"text": "True"}],"isTemplate": false,"count": 0,"updated": 1526900594,"isAuto": false}],"followUpIntents": [],"endInteraction": True,templates": []}';
        Http http  = new Http();
        HttpRequest request = new HttpRequest();
        request.setHeader('content-type', 'application/json');
        request.setHeader('Accept-Language', 'application/json');
        request.setHeader('Authorization', 'Bearer '+accessToken);
        HTTPResponse response = new HTTP().send(request);

So create an intent as shown below and you find the intent will be changed when the method is called.

You can test it from the Google assistant Simulator as shown below.


Now you are all set with your Google assistant intent that your action is ready to deploy into production. Few more steps to be followed. Click on overview in the Actions on Google page.


This is to setup your app and upload it to the production. When it is uploaded to production, everyone will be able to access the custom app. The first step is the quick setup:


This slideshow requires JavaScript.

The second step is to build you App:

This slideshow requires JavaScript.

The third step is get ready for Deployment:

This slideshow requires JavaScript.

The final step is to submit your application to the production as shown below.

If any doubts feel free to reach out to us.

13 thoughts on “Google Assistant Integration with Salesforce – Part 2

      1. Thank you so much @ypranav :).I tried that but it says malformed response,when I am posting from salesforce to dialogflow while overwriting the default response for the get account intent.


  1. Hi Pranav,
    Can you help me?I have replicated your code and my text string looks like below:
    string text = ‘{“id”:”cad08db6-e7cb-4572-ae3e-26749a196a8e”,”name”:”True”,”auto”:true,”contexts”:[],”responses”:[{“resetContexts”:false,”affectedContexts”:[],”parameters”:[],”messages”:[{“type”:0,”speech”:”HUOI”}],”defaultResponsePlatforms”:{},”speech”:[]}],”priority”:500000,”cortanaCommand”:{“navigateOrService”:”NAVIGATE”,”target”:””},”webhookUsed”:false,”webhookForSlotFilling”:false,”lastUpdate”:1537522239,”fallbackIntent”:false,”events”:[],”userSays”:[{“id”:”49a680d8-b28e-47e2-ab75-aeba0bac5a63″,”data”:[{“text”:”yes”}],”isTemplate”:false,”count”:0,”updated”:1537509584,”isAuto”:false},{“id”:”c3c6b5a7-9038-4a03-a681-f37cb216a580″,”data”:[{“text”:”True”}],”isTemplate”:false,”count”:0,”updated”:1537509584,”isAuto”:false}],”followUpIntents”:[],”liveAgentHandoff”:false,”endInteraction”:true,”templates”:[]}’;

    When Im trying to post it says malformed response and its not overriding my default response for the “True” intent.
    Thanks in advance.:)


  2. Hi, Pranav,

    Thank you so much for taking time and sharing this.
    This is much cleaner approach than I did with integrating with AWS Lambda in between and authenticating via OAuth 2.0, using a custom app in salesforce and REST API.

    Now, can you share a Part 3 with secured connections, so that we know when sales person has logged in via their phone/google assistant?
    So that we can securely fetch only records based on their hierarchy or at least greet them with their names.

    Account Linking from Dailog flow is the closest solution I have found, but not sure yet if it works with Salesforce directly.

    Any guidance in this directly will be very helpful. If you prefer we can have a call or webex as well.

    Thank you!
    Google Assistant Integration for salesforce promotions /release mgmt – https://www.youtube.com/watch?v=E1u301IhvnM&t=377s


  3. Hi guys…I need to send given JSON response from dialogflow to salesforce API(Oauth2.0). How can i do that?

    “title”: “”,

    “firstName”: “”,

    “lastname”: “”,

    “city”: “”,

    “prjcode”: “”, ( Share SFDC Project ID)

    “comment”: “”,

    “visitDate”: “2015-07-07T11:11:11”,

    “addCode”: “”,

    “type”: “”, ( Enquiry Type)

    “Src”: “”, ( Enquiry Source)

    “CountryCode”: “”, (example +91)

    “rating”: “Warm”,

    “projectType”: “Residential”,

    “budget”: “”,

    “EnquiryType”: “”,

    “phoneNo”: “”,

    “state”: “”,

    “Country”: “”,

    “websiteName”: “” (Pass the landing page URL)



  4. Hey ! can you help i am facing issue my response is not replacing with custom response even after success Code 200 . I think i am facing issue with ID’s in text string json.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s