---
schema_version: "1"
id: da71d077c34841270ff2bdf0828c0194c15f488db5bfc79f8a43b3789e59507c
title: |-
  Example Implementation to Associate SurveySubjects with SurveyInvitation and
      SurveyResponses
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_interface_sfdc_surveys_associate_surveysubject_example.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_sfdc_surveys_associate_surveysubject_example.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: a4d56b88f7e094f7829305c20df1f3a33531c09ad1e8960d9e0972cfa4590945
description: If no survey responses are populated, create a custom code to associate SurveySubjects with SurveyInvitation and SurveyResponses.
---
# 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.

public with sharing class CreateEntriesInSurveyInvitationRespRL {
    // Utility to create SurveyInvitation and SurveySubject record
    public static void addEntry(String associatedRecordId, String surveyId, String participantId) {
        String invitationId = createSurveyInvitation(surveyId, participantId);
 
        createSurveySubject(invitationId, associatedRecordId);
    }
 
    // Create an unauthenticated invitation by setting the surveyId and participantId
    private static String createSurveyInvitation(String surveyId, String participantId) {
        SurveyInvitation surveyInv = new SurveyInvitation();
        surveyInv.Name = 'SurveyInvitationForCase'; // add your survey invitation name here
        surveyInv.ParticipantId = participantId;
        surveyInv.CommunityId = '0DBRM0000004n4y'; //add your community id here
        surveyInv.OptionsAllowGuestUserResponse = true;
        surveyInv.SurveyId = surveyId;
 
        // Insert the SurveyInvitation Record
        insert as user surveyInv;
 
        return surveyInv.Id;
    }
 
    // Associate the above invitation to the required record (eg: Case, Opportunity...)
    private static void createSurveySubject(String invitationId, String associatedRecordId) {
        SurveySubject subj = new SurveySubject();
        subj.Name = 'Sur\_Subject\_for\_invitation';
        subj.ParentId = invitationId; // similary you can use survey response id to associate survey subject to a response record.
        subj.SubjectId = associatedRecordId;
 
        // Insert the SurveySubject Record
        insert as user subj;
    }
}
 
 
 
//Use this trigger to create a survey subject record associated to
//the Survey Response record
 
trigger SurveyResponseForCaseTrigger on SurveyResponse (after insert) {
        
    System.debug('Inside Survey response trigger ');
     for(SurveyResponse sr: Trigger.New)
    {
      SurveySubject subj = new SurveySubject();
        subj.Name = 'Sur\_Subject\_for\_response';
        subj.ParentId = sr.id; //Associating survey response id
        
       //Get the associatedRecordId recordId (like Case, Opportunity etc) using the SurveyInvitation Id and 
       //assigning it to SubjectId, assuming we inserted SurveySubject record for the associated invitation 
       //using the previous code
       
       List<SurveySubject> SurSubj=\[select subjectid from SurveySubject where parentid = :sr.invitationId WITH USER\_MODE\];
        for(SurveySubject sub:SurSubj){
            String ids=String.valueOf(sub.subjectid).substring(0,3);
            if('500'.equals(ids)){
                
                subj.SubjectId =sub.subjectid;
        // Insert the SurveySubject Record
            insert as user subj;
                break;
        
            }
    
}