Salesforce In-App Surveys by Shashank and Ritik

<This blog has been written by Shashank Lagishetty and Ritik Aggarwal>

Use the Salesforce Feedback Management (SFM) Response APIs to build in-app surveys. Users can respond to these surveys contextually instead of getting redirected to another screen or application. With these APIs, you can build a user-friendly survey experience that matches your company’s theme and branding.

Importance of Customer Satisfaction Surveys

The success of any company depends on their customers’ satisfaction. Companies gather continuous feedback from their customers through surveys, and then try to meet their expectations by addressing the feedback. If companies notice a positive change for their efforts to improve customer satisfaction, they start investing more to improve the customer experience further. If there’s no positive change for their effort, they make alternative plans. So, gathering customer feedback becomes an important part of any company’s growth lifecycle.

Low Response Rate for Surveys

Studies show that the general response rate for online surveys is in the range of 5% – 30%. Achieving a good survey response rate is important to avoid sampling bias. Also, companies may not be able to draw any meaningful conclusions if the survey response rate is low. Some of the reasons for users not completing surveys are:

  • Application switching: Users have to go to a different application to respond to the survey.
  • Bad survey experience: The survey either doesn’t match the company’s branding or the content isn’t displayed properly in certain devices and browsers.

To increase the response rates of surveys, especially when there’s no major incentive for users in completing them, the responding process must be as user-friendly as possible.

Increase Survey Response Rates with SFM APIs

Salesforce Feedback Management provides REST APIs that you can use to build a custom runtime experience with minimal effort. 

In this blog post, we describe how to use Lightning Web Components (LWCs), a Salesforce recommended framework, through which users can respond to any Feedback Management survey. We will also provide a sample implementation, which can be uploaded to a Salesforce org and used after adding the org’s details into them. After this setup is completed, you can embed LWCs in either the Salesforce Lightning pages or Experience Cloud sites, or both.

  • Salesforce Lightning Pages: If you want internal users to respond in the Salesforce UI itself, embed the components in the homepage or record pages etc.
  • Experience Cloud sites: If you want to focus on Experience cloud users and provide a flexible runtime experience, embed the components in Experience Cloud sites.

You can also build custom iOS or Android components for mobile applications using the Salesforce mobile SDK.

Feature Details

FeatureDetailsLearning Resources
SFM Response APIsSFM Response APIs are available only with the Feedback Management Starter or Growth licenses.Feedback Management LicensesSurvey Invitations Using Invitation Configurations (POST, PATCH)Surveys Invitations Using Invitation ID (POST, PATCH)
Custom Lightning Web ComponentsDevelop the required LWC for each type of question and deploy them in a Salesforce org.Lightning Web Components Basics
Apex classesCreate an Apex class and integrate with LWCs.Apex Basics for Admins Call Apex Methods
Named CredentialsDue to security constraints, sessions created by Lightning components or Apex classes aren’t enabled for API access. So, admins must create a named credential with a callout endpoint using their credentials and then use it in the Apex class REST calls to call the SFM APIs.Invoking Callouts Using Apex

Build a Sample In-App Survey

Let’s use an example to understand the process of building an in-app survey. 

A company, Dream House, wants to get employee feedback on their new hire onboarding process. The Salesforce admin of Dream House enables their employees to answer the feedback survey from Salesforce one.app. 

The Salesforce admin of Dream House must perform the following steps to build an in-app survey:

  1. Create a basic survey in the Survey Builder with the following questions:
    1. Rating question: How do you rate the onboarding process?
    2. Short text question: Do you suggest this process to your colleagues?
    3. Long text question: Describe your experience in a few words.
  2. Display survey runtime using custom or Salesforce provided sample LWCs on the Salesforce app homepage or anywhere else to display questions. These components are used to show the survey and get the survey responses from the users.
  3. In the initialization of these LWCs, the Apex class method must be invoked which creates a survey invitation using the API POST call.
  4. The Apex class parses the API response and returns details of the survey and survey questions to the component after the POST call is successful.
  5. Using these survey details, you can populate questions on the application UI as per the requirements. 
  6. With the questions content populated, users can answer the survey questions on the UI and send their responses to the Apex class which in turn calls API PATCH call.
  7. The feedback is submitted after the final PATCH API response is successful.

Create a Survey Invitations for Users

Creating survey invitations is the first step in getting feedback from the user. 

  • The Apex class calls the API endpoint with the required payload to create the survey invitation. 
  • The POST call works as a describe API and returns details about the survey and its questions. 

HTTP Method: POST

Developer Guide: POST API

Endpoint: /services/data/v56.0/connect/surveys/<surveyId>/survey-response

Note: Retrieve surveyId from the Survey object for the corresponding survey needed to be shown.

Payload Structure

{

  “languageCode”:  “<If you have provided translations and want to take the survey in a specific language, then pass the locale code. Ex: “en”/”de”>”,

   “invitationSettings”:{

      “allowGuestUserResponse”: <true/false based on authentication required or not>,

      “allowParticipantsAccessTheirResponse”: <true/false based on if participants can see their responses>,

      “collectAnonymousResponse”: <true/false based on if the responses should be anonymous>,

      “invitationExpirationDate”:”<Date until when invitation can be valid. Ex: 2023-07-31T21:32:54>“,

      “communityId”:”<Id of community if allowGuestUserResponse is made true (Optional)>“,

      “invitationOwner”:”<Owner of the invitation (Optional)>

   }

}

Output

{

   “errors”:[], // Errors observed in the API call

    “flowInterviewState”:”<This will be a encoded string and needs to be passed to the PATCH call>“,

   “invitationId”:”<Unique id and needs to be passed to PATCH call>“,

   “status”:”<Success/Failed. Status of this API call>“,

   “surveyDetail”:{

      “label”:”<Label of survey>“, 

      “name”:”<Internal name of survey>“,

      “surveyPage”:{

         “label”:”<Internal label of page>“, 

         “name”:”<Internal name of the page>“,

         “surveyQuestions”:[ // List of questions

            {

               “description”: “<Description of the question>”,

               “isResponseRequired”: <true/false based on if response is required>,

               “label”:”<Content of the question>“,

               “name”:”<Internal name of the question, needs to be passed to PATCH call>“,

               “questionType”:”<Type of the question>“,

               “responseDataType”:”<Object type of response>“,

               “validationFormula”: <Formula if any custom validation is added>

            }

         ]

      },

      “versionLabel”:”<Survey name>“, 

      “versionNumber”: <Survey version>

   },

   “warnings”:[] // Warnings observed in the API call

}

Populate Survey Questions on the Salesforce UI

Get the details of all the survey questions and show them on the Salesforce UI using the response from the POST call. You can have a submit button or any other trigger to call the Apex class again for submitting the responses. Ensure to pass the user provided answers to the Apex class so that they can be persisted.

We recommend you to have different LWCs for different question types to style question types according to the company’s standard branding and styling.

Persist Survey Responses

After the front end passes the user responses, the Apex class can call the PATCH endpoint to persist the survey responses. This API returns whether the call is successful and the Thank you message that’s shown to users at the end of the survey.

HTTP Method: PATCH

Developer Guide: PATCH API

Endpoint: /services/data/v56.0/connect/surveys/<surveyId>/invitation/<invitationId>/survey-response’

Note: invitationId is retrieved from the earlier POST API call.

Payload structure

{

 “invitationId”: “<Unique invitationId from post API response>“,

 “languageCode”: “<If you have provided translations and want to take the survey in a specific language, then pass the locale code. Ex: “en”/”de”>”,

 “surveyPageResponses” : { 

    “questionResponses” : [ // List of questions with their responses

            {

                “name”: “<Internal name of the question from post API response>“,

                “questionType” : “<type of the question from post API response>“,

                “responseValue” : “<User given answer>

            }

         ]

  },

  “flowInterviewState” : “<flowState from post API response>

}

Output

{

   “errors”:[],

   “status”:”<Success/Failed. Status of the API call>“,

   “surveyPage”:{

       “label”:null,

       “messageDescription”:””,

       “name”:null,

       “redirectUrl”: “<URL of the page to be redirected>”,

       “thankYouMessage”:”<Thank you message to be displayed>“,

       “urlButtons”:[ // List of URL text and URL for buttons to be displayed

]

   }

}

Final Output

After all the LWCs are built and integrated, the survey is shown on the Salesforce UI and the Experience Cloud site.

Survey Builder

The survey in the Survey Builder

Salesforce UI

The survey runtime on the Salesforce UI

Experience Cloud Site

The survey runtime on the Experience Cloud site

Sample Implementation

A sample implementation of the LWCs and the Apex class can be found here. After the package is installed and configured in a Salesforce org, you can embed the SurveyContainer LWCs in the layouts to use the custom survey runtime.

Note: In the configured package, a valid SurveyId must be specified and a valid namedCredentials must be referenced in the Apex class.

Lightning Web ComponentDescription
SurveyContainerInteracts with the Apex class and displays the survey contents.
SurveyQuestionInvokes the question-specific component.
surveyBooleanQuestionShow the boolean type of questions
surveyShortTextQuestionShow the short text type of questions.
surveySingleSelectQuestionShow the single selection type of questions
surveyMultiSelectQuestion/surveyNpsQuestion/surveyRatingQuestion/surveyFreeTextQuestionShows other supported question types.

The SurveyApi Apex class holds the values for surveyId/namedCredentials and makes SFM API POST/PATCH calls. 

Benefits of In-App Surveys

Some of the benefits of in-app surveys are:

  1. Application switching isn’t required.
  2. Users are more likely to respond to surveys and with higher and faster response times.
  3. Users have more flexibility in designing the user experience of survey runtime. It can be made to sync with the application’s branding and theme.
  4. There’s no additional training required for the users to respond.
  5. Currently, the SFM runtime isn’t supported in LWR experience sites because it’s primarily an Aura-based application. With these APIs and sample implementation, you can enable support for Surveys in LWR experience sites.

Scope and Limitations

To be able to call the SFM APIs from Apex, named credentials must be created. It’s not feasible for each user to create a named credential before responding to the survey. So, the admin’s credentials are used to create the named credentials that’s used in the Apex class and all the responses are submitted using these credentials. 

Due to this limitation as of Winter ‘23, all the survey responses are submitted by the admin user because of the named credentials used. In the upcoming releases, we are planning to improve this experience. Stay tuned about the improvements we make by going through the release notes each release.

Conclusion

The frontend of a survey runtime experience is one of the essential aspects for any small business, a medium-scale organization, or an enterprise. If it isn’t seamless, there’s a high probability of users not attempting or completing the survey. With in-app surveys, the survey runtime experience can be more user-friendly. In-app surveys can bring a huge difference in usability and enhance overall customer engagement.