How to Customize Salesforce Survey
Below are steps to enable Salesforce Survey and to use in LWC. Below Sample code will tell you how to Initiate a Salesforce Survey from Case Record.
Step-1: Enable Salesforce Survey in Salesforce.
Setup-> quick find (Survey)->Survey Settings Enable
Step-2: Enable Community and activate community and give guest user profile access of following Object: Survey(READ), Survey Invitation (READ), Survey Response(READ,CREATE, EDIT)
click on Community ->Builder ->Administration-> Pages-> Goto Force.com-> Click on Public Access Settings-> Edit the Profile and give Object Access
Step-3: Data Model of Survey
Step-4: goto Survey Tab and create one Survey and make it active.
Step-5: Create a Case(Lookup) on Survey Invitation Object.
Step-6: Create this Exception Class:
public class MyException extends Exception{}
<template>
<lightning-button title="Click me" label="Click me" onclick={Opensurvey}></lightning-button>
</template>
Step-8: Create JS: sampleSurvey.js
import { LightningElement, track, api, wire } from 'lwc';
import CreateSurveyInvWrpResult from '@salesforce/apex/SampleSurveyController.CreateSurveyInvWrpResult';
export default class SampleSurvey extends LightningElement {
@track weburl;
@track error;
@track msg;
@api recordId;
Opensurvey() {
CreateSurveyInvWrpResult({ CaseId: this.recordId })
.then(result => {
this.msg = result;
var obj = JSON.parse(JSON.stringify(this.msg));
if (obj.bError == true) {
alert(obj.strMsg);
}
if (obj.bError == false) {
window.open(obj.strMsg);
}
this.error = undefined;
})
.catch(error => {
this.msg = error;
this.error = undefined;
alert('In error' + this.msg);
});
}
}
Step-9: Create Apex class : SampleSurveyController.cls
public with sharing class SampleSurveyController {
public SampleSurveyController() {
}
@AuraEnabled
public static WrapperResult CreateSurveyInvWrpResult(String CaseId) {
string CaseNumber;
string communityId;
string SurveyId;
string ContactEmail;
string ContactId;
string surveyInvd;
string strSurveyURL;
communityId = [select Id from Network where Name = 'survey'].Id;
SurveyId = [Select Id from Survey where Name = 'samplesurvey'].Id;
WrapperResult WrpResult = new WrapperResult(null, null);
try {
list<SurveyInvitation> lstSurveyInv = [select id from SurveyInvitation where Case__c = :CaseId];
if(lstSurveyInv.size() > 0) {
throw new MyException('Survey for this case is already completed.');
}
SurveyInvitation SInv = New SurveyInvitation();
Sinv.CommunityId = communityId;
SInv.Name = caseId;
SInv.Case__c = CaseId;
//SInv.ParticipantId='0036g00000A04uWAAR';//userInfo.getUserId();
SInv.OptionsCollectAnonymousResponse = true;
SInv.OptionsAllowGuestUserResponse = true;
SInv.SurveyId = SurveyId;
insert SInv;
SurveySubject SS = new SurveySubject();
SS.SubjectId = CaseId;
SS.ParentId = SInv.Id;
SS.Name = CaseId;
insert SS;
SurveyInvitation sInvRecord = [Select id, UUID from SurveyInvitation where id = :SInv.id];
string UniquieInviteId = sInvRecord.UUID;
//https://surveyamul-developer-edition.na174.force.com/survey/survey/runtimeApp.app?invitationId=0Ki6g000000DqqM&surveyName=samplesurvey&UUID=da11d795-ff3d-486e-819a-ffc08056234a
strSurveyURL= system.label.Survey_Base_URL + '' + SInvRecord.id + '&surveyName=samplesurvey&UUID=' + UniquieInviteId;
system.debug('survey url' + json.serialize(strSurveyURL));
WrpResult= new WrapperResult(false, strSurveyURL);
return WrpResult;
} catch(exception e) {
System.debug('The following exception has occurred while inserting SurveySubject: ' + e.getMessage());
strSurveyURL= e.getMessage();
WrpResult= new WrapperResult(true, e.getMessage());
return WrpResult;
} finally {
return WrpResult;
}
}
public class WrapperResult {
@AuraEnabled public boolean bError { get; set; }
@AuraEnabled public string strMsg { get; set; }
public WrapperResult(boolean bErr, string strMessage) {
this.bError = bErr;
this.strMsg = strMessage;
}
}
}
Step-10: Enable the Above LWC page in Case Page Layout.
Step-11: On Click Me your Survey URL will open:
2 Comments
Thank you much for sharing this wonderful post.
ReplyDeleteAWS Training in Chennai | AWS Training Institute in Chennai | AWS Training Center in Chennai
Buy Ambien Online
ReplyDelete