Ad Code

Responsive Advertisement

Salesforce Survey using Apex

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{}


Step-7: Create this LWC: sampleSurvey.html


<template>
    <lightning-button title="Click me" label="Click me" onclick={Opensurvey}></lightning-button>
</template>

Step-8: Create JS: sampleSurvey.js

import { LightningElementtrackapiwire } 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(nullnull);


        try {
            list<SurveyInvitationlstSurveyInv = [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 idUUID 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

            strSurveyURLsystem.label.Survey_Base_URL + '' + SInvRecord.id + '&surveyName=samplesurvey&UUID=' + UniquieInviteId;
            system.debug('survey url' + json.serialize(strSurveyURL));
            WrpResultnew WrapperResult(falsestrSurveyURL);
            return WrpResult;
        } catch(exception e) {
            System.debug('The following exception has occurred while inserting SurveySubject: ' + e.getMessage());
            strSurveyURLe.getMessage();
            WrpResultnew WrapperResult(truee.getMessage());
            return WrpResult;
        } finally {
            return WrpResult;
        }
    }



    public class WrapperResult {
        @AuraEnabled public boolean bError { getset; }
        @AuraEnabled public string strMsg { getset; }
        public WrapperResult(boolean bErrstring 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:




Reactions

Post a Comment

2 Comments