Sunday 6 December 2020

How to populate values in "Survey Invitations and Response" RL in Salesforce Survey


Not seeing values populated in "Survey Invitations and Response" RL you added to your Case, Opportunity or any other supported Objetc?


Do not worry, go through this simple article below to under

Firstly I would like to make it clear that there are two types of RL -

  • 'Survey Invitation' RL
  • 'Survey Invitation and Response' RL

The first 'Survey Invitation' RL populates only Survey Invitation relation details on any Object when Survey Invitation object has a lookup created (either standard or custom ) on that object.


However 'Survey Invitation and Response' RL is a bit different and as mentioned in the Salesforce Survey document  "To populate the survey invitation fields and the survey response fields, the survey invitation records, and the survey response records must be associated with the SurveySubject object records."


So this displays Survey invitation related details if there is a related record created between Survey Invitation and Survey Subject records.

Similarly, it displays Survey Response related details if there is a related record created between Survey Response and Survey Subject records.



To understand this better see this example:


Let say you added your RL to Case Object following the steps mentioned in the Salesforce Survey document 

But You see no data populated in the "Survey Invitations and Response" RL 




Now use the below code to generate Survey Invitations and then created Survey Invitation and response related record with Survey Subject.


public
class CreateEntriesInSurveyInvitationRespRL {
// Utility to create SurveyInvitation and SurveySubject record
public static void addEntry(String associatedRecordId, String surveyId, String participantId) {
String invitationId = createSurveyInvitation(surveyId, participantId);

createSurveySubject(invitationId, associatedRecordId);
}

// Create an unauthenticated invitation by setting the surveyId and participantId
private static String createSurveyInvitation(String surveyId, String participantId) {
SurveyInvitation surveyInv = new SurveyInvitation();
surveyInv.Name = 'SurveyInvitationForCase'; // add your survey invitation name here
surveyInv.ParticipantId = participantId;
surveyInv.CommunityId = '0DBRM0000004n4y'; //add your community id here
surveyInv.OptionsAllowGuestUserResponse = true;
surveyInv.SurveyId = surveyId;

// Insert the SurveyInvitation Record
insert surveyInv;

return surveyInv.Id;
}

// Associate the above invitation to the required record (eg: Case, Opportunity...)
private static void createSurveySubject(String invitationId, String associatedRecordId) {
SurveySubject subj = new SurveySubject();
subj.Name = 'Sur_Subject_for_invitation';
subj.ParentId = invitationId; // similary you can use survey response id to associate survey subject to a response record.
subj.SubjectId = associatedRecordId;

// Insert the SurveySubject Record
insert subj;
}
}



//Use this trigger to create a survey subject record associated to
//the Survey Response record
trigger SurveyResponseForCaseTrigger on SurveyResponse (after insert) {
System.debug('Inside Survey response trigger ');
for(SurveyResponse sr: Trigger.New)
{
SurveySubject subj = new SurveySubject();
subj.Name = 'Sur_Subject_for_response';
subj.ParentId = sr.id; //Associating survey response id
//Get the associatedRecordId recordId (like Case, Opportunity etc) using the SurveyInvitation Id and
//assigning it to SubjectId, assuming we inserted SurveySubject record for the associated invitation
//using the previous code
List<SurveySubject> SurSubj=[select subjectid from SurveySubject where parentid = :sr.invitationId];
for(SurveySubject sub:SurSubj){
String ids=String.valueOf(sub.subjectid).substring(0,3);
if('500'.equals(ids)){
subj.SubjectId =sub.subjectid;
// Insert the SurveySubject Record
insert subj;
break;
}
}






You will now see all invitation and response data created.




Note- You can implement the same thing using a process as well.
You will need to create two processes, one on the SurveyInvitation record and the other on the SurveyResponse record, and then create corresponding Survey Subject records for them.






3 comments:

  1. How would I do this using a Process Builder? I'm not very good at coding.

    ReplyDelete
  2. Hi Bhawna
    You mentioned we can achieve this through PB as well, can you show us how we can do this through PB.
    And can this not happen that, this trigger/PB does not create another new record in 'Survey Invitations and Responses' RL rather it populates the 'Responses' Field in existing record?
    Thanks in advance.

    ReplyDelete

How to generate QR code for Survey Invitation in Salesforce Platform Survey

 Easy steps to generate QR code for Survey Invitation Link 1) Create your Survey using Salesforce Platform Survey. 2) Once done activate it....