Skip to main content

Cannot build test data for survey objects

Hello All,

I activate survey in my org and now I want to measure NPS depends on these surveys.

So I built a class to measure NPS for whole company, division, NPS based governorate and more.. 

And when I tried to built Test class to deploy it to production it restricted me to build test data on all survey objects,

And always give me the error field is not writeable for each field.  

So now I can't deploy it to prod without the test class.

 

I would be grateful for any solutions :)

 

Code:

// insert new Survey

List<Survey> SurveyTest = new List<Survey>();

SurveyTest.add(new Survey(Name = 'testSurvey1'));

SurveyTest.add(new Survey(Name = 'testSurvey2'));

// insert new SurveyInvitation

List<SurveyInvitation> SurveyInvitationTest = new List<SurveyInvitation>();

SurveyInvitationTest.add(new SurveyInvitation(Name = 'surveyInvitationTest1', SurveyId = SurveyTest[0].Id, ContactId = testContacts[0].Id, ParticipantId = testContacts[0].Id));

SurveyInvitationTest.add(new SurveyInvitation(Name = 'surveyInvitationTest2', SurveyId = SurveyTest[1].Id, ContactId = testContacts[1].Id, ParticipantId = testContacts[1].Id));

// insert new SurveyQuestion

List<SurveyQuestion> SurveyQuestionTest = new List<SurveyQuestion>();

SurveyQuestionTest.add(new SurveyQuestion(Name = 'SurveyQuestionTestNPS1', Type = 'NPS'));

SurveyQuestionTest.add(new SurveyQuestion(Name = 'SurveyQuestionTestNPS2', Type = 'NPS'));

// insert new SurveyQuestionResponse

List<SurveyQuestionResponse> SurveyQuestionResponseTest = new List<SurveyQuestionResponse>();

SurveyQuestionResponseTest.add(new SurveyQuestionResponse(QuestionId = SurveyQuestionTest[0].Id, NumberValue = 9));

SurveyQuestionResponseTest.add(new SurveyQuestionResponse(QuestionId = SurveyQuestionTest[1].Id, NumberValue = 10));

// insert new SurveySubject

List<SurveySubject> SurveySubjectTest = new List<SurveySubject>();

SurveySubjectTest.add(new SurveySubject(Name = 'SurveySubjectTest1', SurveyInvitationId = SurveyInvitationTest[0].Id, ParentId = SurveyInvitationTest[0].Id, SubjectId = testAccounts[0].Id, SurveyId = SurveyTest[0].Id, SubjectEntityType = 'Account'));

SurveySubjectTest.add(new SurveySubject(Name = 'SurveySubjectTest2', SurveyInvitationId = SurveyInvitationTest[1].Id, ParentId = SurveyInvitationTest[1].Id, SubjectId = testCase[0].Id, SurveyId = SurveyTest[1].Id, SubjectEntityType = 'Case'));

 

#Apex #Test Classes #Salesforce Surveys 

11 answers
  1. Jun 30, 2025, 10:52 AM

    Indeed, you can't insert records on any Survey object (I checked it on Survey, SurveyResponse, SurveyQuestionResponse only). Then you have 2 hard and winding roads:  

    1. Use repository mocking pattern in your tests 

    2. Use (SeeAllData=true) which in this situation is awful. Easy for dev, hard for DevOps (he/she must create any of those objects manually in prod, using the Survey feature).

0/9000