Last active
July 10, 2020 06:52
-
-
Save LokeshSagi/451f2ac1c2eba636fe264a998b310cea to your computer and use it in GitHub Desktop.
Apex controller to auto generate a survey invitation link - Call this controller method from any aura or LWC component to automatically generate invitation link. We can also populate this link a custom field value anywhere and send email to the invitee automatically with process automation. Watch this video: https://screencast-o-matic.com/watch/…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AutoGenerateSurveyLinkController { | |
@AuraEnabled | |
public static String generateSurveyInvitationLink(String relatedObjectRecordId) { | |
String returnString; | |
Boolean isAllowed; | |
isAllowed = checkAdminPermission(); | |
if(isAllowed) { | |
returnString = helperMethod(relatedObjectRecordId); | |
} else { | |
returnString = 'not allowed'; | |
} | |
// } else { | |
// returnString = helperMethod(relatedObjectRecordId, whichAction); | |
// } | |
return returnString; | |
} | |
public static String helperMethod(String relatedObjectRecordId) { | |
system.debug('relatedObjectRecordId - '+relatedObjectRecordId); | |
String returnString; | |
string communityId; | |
string surveyId; | |
List<SObject> relatedRecords = new List<SObject>(); | |
// relatedRecords = [Select Id, ..... from <SObject>]; | |
if(relatedRecords.size() > 0) { | |
SObject relatedRecord = relatedRecords[0]; | |
//fetch the community Id of the community for which survey is enabled. | |
communityId = [select Id from Network where Name='Survey'].Id; | |
// get the Id of the survey to send | |
List<Survey> surveys = [Select Id from Survey where Name=<'name of the survey'>]; | |
if(surveys.size() > 0) { | |
surveyId = surveys[0].Id; | |
} | |
SurveyInvitation surveyI = New SurveyInvitation(); | |
surveyI.Name = <'populate name of the invitation as required'>; | |
surveyI.ParticipantId = <'populate the the survey participantId as required.'>; | |
surveyI.Contact__c = <'populate any other custom fields as required'>; | |
//SInv.OptionsCollectAnonymousResponse = true; | |
// set this to true if anonimous users are allowed to respond to sutveys without loggin in. | |
surveyI.OptionsAllowGuestUserResponse = true; | |
surveyI.SurveyId = surveyId; | |
// insert Survey Invitation record | |
insert surveyI; | |
system.debug('surveyI - '+surveyI); | |
// Create SurveySubject record as a junction between standard/custom object and SurveyInvitation | |
SurveySubject surveyS = new SurveySubject(); | |
surveyS.SubjectId = relatedObjectRecordId; | |
// SS.SurveyId = SurveyId; | |
surveyS.ParentId = surveyI.Id; | |
surveyS.Name = <'populate the name as required'>; | |
insert surveyS; | |
// query for the inserted SurveyInvitation to get the invitation link and pass it back to UI(Aura/LWC) | |
SurveyInvitation updatedInvitation = | |
[Select Id, ParticipantId, CommunityId, Name, OptionsAllowGuestUserResponse, SurveyId, | |
InvitationLink from SurveyInvitation where Id =: surveyI.Id]; | |
// if we need to append any other parameters in the invitation link, we can do so and encode the URI as below. | |
returnString = updatedInvitation.InvitationLink + '&accountName=' + | |
<relatedObjectRecord.Account__r.Name> + '&contactFullName='+ | |
<relatedObjectRecord.Contact__r.Name>; | |
system.debug('returnString -- '+returnString); | |
} | |
// encode the url to avoid spaces in the accountname or contactname | |
return returnString.replace(' ', '%20'); | |
} | |
public static Boolean checkAdminPermission(){ | |
// Logic to check if logged-in user is allowed to generate the survey goes here | |
// return true/false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment