I'm using Salesforce Survey for one of the requirements. I would like to write a trigger on SurveyResponse object. I've written the trigger and apply the logic that I need.

Now, I'm trying to write a test method of that trigger and as part of the test method, I'm trying to insert SurveyResponse as test setup. When I write insert DML statement, I receive an error that SurveyResponse is not writable.

Now, How would I write a test class for a sObject which is not writable but salesforce allow to write trigger code on that object?

One option in my mind is to put SeeAllData = true but this is something which I'm not preferring. Is there any better solution for this problem?

  • The SOAP developer guide does not specify that this object is writeable so not sure if you can create data through apex. your best bet is seealldata=true. Parallely also create a ticket with salesforce
    – RedDevil
    May 17, 2019 at 13:19
  • How does SeeAllData = true even help that much, since you still can't cause the trigger to fire? Can you programmatically submit a survey response, rather than directly inserting a SurveyResponse record? May 17, 2019 at 13:50
  • 1
    Factor all of the logic out of the trigger into a handler class and you may be able to test it with in-memory records, without performing DML. That'll depend somewhat on exactly what your code does, however. And you won't be able to perform a "Run Specified Tests" deployment since the trigger itself won't be covered.
    – David Reed
    May 18, 2019 at 14:06
  • 1
    @DavidReed - can you even deploy a trigger if it has no test coverage?
    – cropredy
    May 18, 2019 at 18:15
  • Yeah @cropredy that's my thought exactly and why I didn't post an answer. If an object does not support DML, it should not support trigger either.
    – Adrian Larson
    May 22, 2019 at 1:17

2 Answers 2


This isn't possible today. I've reached out to the Surveys team to ask them either mark the object type as not available for Apex triggers, or make it possible to test Apex triggers on it.

  • Thanks ca_peterson. Please let me know whenever you receive anything from Salesforce. Aug 14, 2019 at 3:41
  • @AnuragBhardwaj I work for Salesforce (PM for Apex). The surveys team acknowledged this is a gap today, but wasn't able to share suggestions at this time. Aug 19, 2019 at 22:45
  • @ca_peterson do you know if those triggers are deployable to production? how does it align to the Every trigger must have some test coverage. requirement from help.salesforce.com/… Is there some exception for this trigger?
    – neff
    Feb 13, 2020 at 9:44
  • @Neff - they are not, since it is impossible to test them I'm afraid. Hence my request to the surveys team to either disallow triggers on them or come up with an appropriate way to test. Feb 17, 2020 at 20:42
  • 1
    @ca_peterson The issue (solution) status is IN REVIEW. (trailblazer.salesforce.com/…)
    – Cray Kao
    Jun 25, 2021 at 4:45

I think you can test this through a couple of "hacks". If your logic applies to a before insert trigger then we're in luck! Factor out the logic to a handler class and method.

For your test code, initialize the sObject, then put the insert in a try catch block.... in the catch block catch the exception. Granted, this is not asserting the logic, but you're getting code coverage on the trigger to deploy the trigger. To assert the actual logic run unit tests on the handler....

This is just a limitation of the platform, like pricebooks were a while ago.

OR, you can do @seeAllData on the test method as you suggested.

Here is the snippet of the code and the test class

trigger SurveyResultTrigger on SurveyResult(before insert){
    //factor your logic to a  handler class and method

private static void surveyResult_test(){
    SurveyResult res = new SurveyResult();
    // init the res..
        insert res;
        System.assert(false, 'expected exception');

    catch(Exception e){
  • Thanks @ohana for the effort. This is something we also tried to create a local instance of surveyResult but luck is not with me because there trigger should fire on update operation and there would some field value checks. Since fields are also not writable, we can't create local copy of field. So far, we have used SeeAllData = true. May 22, 2019 at 5:13
  • @AnuragBhardwaj Have you tried to use JSON.deserialize to mock updated field values? Could you please share in your question exact piece of code that needs coverage?
    – Patlatus
    Sep 19, 2019 at 8:48
  • I would try to @isTest class SurveyResponseTriggerTest { private static testMethod void x() { SObject x = new SurveyResponse(Id = '0My41000002Dfe7AAC'); try { update x; } catch (Exception e) {} } } however, this doesn't give coverage for the trigger, since this throws TypeException
    – Patlatus
    Sep 19, 2019 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.