Preventing Survey Fatigue with scenarios other than a Case's Contact


Simple Survey provides out of the box functionality that prevents over-surveying a particular Contact based on the last time they were surveyed from a Case. We've providing some sample code to help with other scenarios.


Simple Survey uses a trigger called CaseTrigger to track when a survey was last sent to a Case Contact.  The "Survey_Sent__c" and "Survey_Last_Requested_on__c" fields it manipulates can be used by Workflow rules to prevent a particular Contact from being surveyed too frequently.

This same logic can be applied to other standard and custom objects as well. This sample code assumes:
  • The object has a field that looks up to a Contact
  • A date/time field such as "Survey_Sent__c" is on the triggering object
  • A workflow field update sets the "Survey_Sent_Date_Time__c" field to NOW() when the survey is sent.
  • A date/time field such as "Survey_From_Object_Survey_Last_Requested_On__c" is added to Contact
The following trigger can be used as a template to apply to another object.  To use it with another object, have your Salesforce developer substitute any references to Custom_Object__c with references to your own object.  This can be either a standard or custom object.

This is an example trigger that can be used with a Salesforce object to prevent survey fatigue. 
trigger SurveySent on Survey_From_Object__c (before update) {
    Set<Contact> contactSetForUpdate = new Set<Contact>();
    for (Survey_From_Object__c surveyFromObject : {
        // Get the old version of the custom object from trigger.oldMap context variable.
        Survey_From_Object__c oldsurveyFromObject = trigger.oldMap.get(surveyFromObject.Id);
        // Compare the old version of the object to the new version.  If the Survey Sent Date Time field has changed, enter the condition.
        if (surveyFromObject.Survey_Sent_Date_Time__c != oldsurveyFromObject.Survey_Sent_Date_Time__c) {
            // If the custom object has no contact, exit this iteration of the loop.  We won't be able to update a null contact.
            if (surveyFromObject.Contact__c == null) continue;
            // Create a new contact instance, set its Id and Custom_Object_Survey_Last_Requested_On__c.  Setting Id the makes querying the contact table unnecessary.
            Contact c = new contact (
                                     Id = surveyFromObject.Contact__c,
                                     Survey_From_Object_Last_Requested_On__c = surveyFromObject.Survey_Sent_Date_Time__c
            // Add the contact to our set.
    // Make sure there is something to before performing any DML.  There is no reason to update an empty list of records!
    if (!contactSetForUpdate.isEmpty()) {
        // Create a list and add to contacts to it.  DML cannot be performed on sets. 
        List<Contact> contactsForUpdate = New List<Contact>();
        // Update the contact records

Unit Test:
Unit test for the SurveySent trigger on Survey_From_Object__c
private class TestCustomObjectSurveySent {
    private static TestMethod void TestSurveySentTrigger() {
        contact c = new contact(FirstName = 'Survey', LastName = 'Recipient');
        insert c;
        Survey_From_Object__c surveyFromObject = new Survey_From_Object__c(Contact__c =;
        insert surveyFromObject;
        // The surveyFromObjectSurveySent trigger runs on update only.
        surveyFromObject.Survey_Sent_Date_Time__c =;
        update surveyFromObject;
        c = [SELECT Survey_From_Object_Survey_Last_Requested_On__c FROM Contact WHERE Id = :c.Id];
        // The SurveySent trigger copies the survey sent date time on Survey_From_Object__c to the survey last request on field on Contact.  We therefore expect the same values.
                            c.Survey_From_Object_Survey_Last_Requested_On__c, 'Survey_From_Object_Survey_Last_Requested_On__c on Contact has an unexpected value');

Please note:  This KB article is provided as-is, and custom survey fatigue triggers are not supported.  The intended audience for this article is advanced Salesforce developers.  For further assistance, please contact your Salesforce developer, Salesforce implementation partner, or your Internet Creations Account Executive to inquire about professional services.


Open a Case

Powered by