Ensure Single Response per Survey Invitation

This will be a short post on how to prevent customers from answering more than once to a Salesforce Survey.

survey_response_received

Problem

Salesforce Surveys are usually shared via publicly accessible Communities. The process that I’ve seen so far usually involves an Agent closing a Case, and then some kind of an automated process will generate a public link to a Survey that will be sent out to a customer via email. The customer would then open this link and submit a response.

The problem occurs if the customer opens the same Survey link for the second time - he or she will be able to submit a second response. The customer would then probably get confused, thinking that something is broken and that Survey responses are not being sent out, which would lead to pretty bad customer experience. So how can we avoid this?

Solution

The fact that Surveys are shared via publicly accessible Communities can help us out in this case. Salesforce Communities that can be accessed by the general public, without any need for prior authentication, have a special user called “Guest User”. All guest visitors to a public site share this same guest user record (one per site) and have the same access level.

If we take this knowledge and drill down into Survey Invitation object, we will find a field called OptionsAllowGuestUserResponse. All we need to do to stop a customer from answering a Survey Invitation more than once is to set this OptionsAllowGuestUserResponse field to false once we receive the first response. The below SurveyInvitationTrigger will do just that:

trigger SurveyInvitationTrigger on SurveyInvitation (before update) 
{
  for(Id surveyId : Trigger.newMap.keySet()){
    SurveyInvitation newRecord = Trigger.newMap.get(surveyId);
    SurveyInvitation oldRecord = Trigger.oldMap.get(surveyId);
    
    if( newRecord.ResponseStatus == 'Completed' && 
        newRecord.ResponseStatus != oldRecord.ResponseStatus
    ) {
      // survey has been responded to, so make it unavailable for public
      newRecord.OptionsAllowGuestUserResponse = false;
    }
  }
}

Appendix: Providing a meaningful message

If you have a dedicated Salesforce community just for surveys, you can build on top of the provided solution to further improve your customer experience.

If a customer visits the Survey Invitation link for the second time, after already submitting a response, the Salesforce community will show them an error page. In particular, the page that will be shown is the standard Login Error page that can be accessed via Salesforce Community Builder. You can customize this page to your liking to provide a more meaningful message to your customers, like the one below:

survey_response_received

Written on November 25, 2019