Skip to content

Instantly share code, notes, and snippets.

@RockyMtnMarc
Last active November 10, 2021 11:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RockyMtnMarc/c33bb86366201b547313de41dac04506 to your computer and use it in GitHub Desktop.
Save RockyMtnMarc/c33bb86366201b547313de41dac04506 to your computer and use it in GitHub Desktop.
Apex Trigger to create a new SurveyInvitation for every new LiveChatTranscript
//This trigger creates a new SurveyInvitaion record for each LiveChatTranscript for use in a post-chat survey
//This trigger assumes that a Contact record has been associated with each LiveChatTranscript, for example using a pre-chat form
trigger LiveChatTranscript_CreateSurveyInvitation on LiveChatTranscript (before insert) {
for (LiveChatTranscript lct : Trigger.New){
if(lct.ContactId != null){
//Change %Consumer% in the following line to a unique keyword for the Community Name where your surveys are published
List<Community> thisCommunity = [SELECT NetworkId FROM Community WHERE Name LIKE '%Consumer%' LIMIT 1];
//Change %Hutch% in the following line to a unique keyword for the Survey you want to show your chat users
List<Survey> thisSurvey = [SELECT Id,Name FROM Survey WHERE Name LIKE '%Hutch%' LIMIT 1];
//Creates a new instance of SurveyInvition and populate the required fields
SurveyInvitation newSurveyInvitation = new SurveyInvitation();
newSurveyInvitation.SurveyId = thisSurvey[0].Id;
newSurveyInvitation.Name = thisSurvey[0].Name;
newSurveyInvitation.CommunityId = thisCommunity[0].NetworkId;
newSurveyInvitation.OptionsAllowGuestUserResponse = true;
newSurveyInvitation.OptionsAllowParticipantAccessTheirResponse = false;
newSurveyInvitation.OptionsCollectAnonymousResponse = false;
newSurveyInvitation.ParticipantId = lct.ContactId;
newSurveyInvitation.ChatKey__c = lct.ChatKey;
try{
insert newSurveyInvitation;
} catch(dmlexception e){
system.debug('SurveyInvitation creation error: ' + e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment