Example Implementation to Associate SurveySubjects with SurveyInvitation and SurveyResponses

If no survey responses are populated, create a custom code to associate SurveySubjects with SurveyInvitation and SurveyResponses.

This example shows how to associate SurveySubjects with SurveyInvitation and SurveyResponses.

1public class CreateEntriesInSurveyInvitationRespRL {
2    // Utility to create SurveyInvitation and SurveySubject record
3    public static void addEntry(String associatedRecordId, String surveyId, String participantId) {
4        String invitationId = createSurveyInvitation(surveyId, participantId);
5 
6        createSurveySubject(invitationId, associatedRecordId);
7    }
8 
9    // Create an unauthenticated invitation by setting the surveyId and participantId
10    private static String createSurveyInvitation(String surveyId, String participantId) {
11        SurveyInvitation surveyInv = new SurveyInvitation();
12        surveyInv.Name = 'SurveyInvitationForCase'; // add your survey invitation name here
13        surveyInv.ParticipantId = participantId;
14        surveyInv.CommunityId = '0DBRM0000004n4y'; //add your community id here
15        surveyInv.OptionsAllowGuestUserResponse = true;
16        surveyInv.SurveyId = surveyId;
17 
18        // Insert the SurveyInvitation Record
19        insert surveyInv;
20 
21        return surveyInv.Id;
22    }
23 
24    // Associate the above invitation to the required record (eg: Case, Opportunity...)
25    private static void createSurveySubject(String invitationId, String associatedRecordId) {
26        SurveySubject subj = new SurveySubject();
27        subj.Name = 'Sur_Subject_for_invitation';
28        subj.ParentId = invitationId; // similary you can use survey response id to associate survey subject to a response record.
29        subj.SubjectId = associatedRecordId;
30 
31        // Insert the SurveySubject Record
32        insert subj;
33    }
34}
35 
36 
37 
38//Use this trigger to create a survey subject record associated to
39//the Survey Response record
40 
41trigger SurveyResponseForCaseTrigger on SurveyResponse (after insert) {
42        
43    System.debug('Inside Survey response trigger ');
44     for(SurveyResponse sr: Trigger.New)
45    {
46      SurveySubject subj = new SurveySubject();
47        subj.Name = 'Sur_Subject_for_response';
48        subj.ParentId = sr.id; //Associating survey response id
49        
50       //Get the associatedRecordId recordId (like Case, Opportunity etc) using the SurveyInvitation Id and 
51       //assigning it to SubjectId, assuming we inserted SurveySubject record for the associated invitation 
52       //using the previous code
53       
54       List<SurveySubject> SurSubj=[select subjectid from SurveySubject where parentid = :sr.invitationId];
55        for(SurveySubject sub:SurSubj){
56            String ids=String.valueOf(sub.subjectid).substring(0,3);
57            if('500'.equals(ids)){
58                
59                subj.SubjectId =sub.subjectid;
60        // Insert the SurveySubject Record
61            insert subj;
62                break;
63        
64            }
65    
66}