[Back]  [Print Page]
Setting up triggers in Salesforce for Vista users (For Developers)

Vista can automatically update Salesforce fields.  However, if you are a developer and wish to further customize Vista for Salesforce, please follow the directions below.

Salesforce-integrated Vista surveys can update contact fields when survey results are transferred to Salesforce from Vista. You also can configure Salesforce to update fields in other Salesforce objects by creating a Salesforce trigger. The trigger in Salesforce will run whenever a new Survey Result object is created by Vista in your Salesforce account.

Important: Salesforce triggers are a part of Salesforce and not a part of Vista functionality. If you require assistance configuring Salesforce, please contact a Salesforce support representative at http://www.salesforce.com/services-training/customer-support/.

The following is an example of a Apex trigger that will update the first name, last name, and email fields in the Contact object of the people who took the survey. This example contains comments to assist you in understanding the trigger. 

trigger updateFieldsFromSurveyResult on VANG_Survey_Result__c (after insert) {

       list<Id> ids =  new List<Id>();

       for(VANG_Survey_Result__c s : Trigger.new)
           ids.add(s.Contact__c);

       list<Contact> contacts = [SELECT Id,FirstName,LastName,Email FROM Contact WHERE Id in :ids];

       for (Integer i = 0; i < Trigger.new.size(); i++) {

             VANG_Survey_Result__c s = Trigger.new.get(i);

             if(s.SurveyID__c == '5555555555'){

                   //Select the contact associated with the new Survey Result
                   Contact c = contacts.get(i);

                   //Update any fields in the contact from values in the Survey Result
                   c.FirstName = s.Q1Value__c;
                   c.LastName = s.Q2Value__c;
                   c.Email = s.Q3Value__c;

                   update c;

             }

       }

 }

The example above is a basic trigger. Salesforce requires code coverage of over 75% for code that will run on your live organization (you must have tests that run through at least 75% of every line of code that you write). To get the code coverage, you need to add a test class for the trigger, for example:

private class Test_trigger {

    static testMethod void myTest() {

       //Insert a test contact
       Contact test_contact = new Contact(FirstName='Orig First Name',LastName='Orig Last Name',Email='orig@email.com');
       insert test_contact;

        //Insert a test survey
       VANG_Survey__c survey = new VANG_Survey__c();
       survey.Survey_ID__c = '5555555555';
       insert survey;

       //Insert a test survey response
       VANG_Survey_Result__c result = new VANG_Survey_Result__c(Survey__c=survey.Id);

       result.Contact__c = test_contact.Id;
       
result.SurveyID__c = '5555555555';
       result.Q1Value__c = 'New First Name';
       result.Q2Value__c = 'New Last Name';
       result.Q3Value__c = 'test@email.com';
       insert result;

       Contact updated_contact = [SELECT Id, FirstName, LastName, Email FROM Contact WHERE Id = :test_contact.Id];

       //Verify that the values of the contact were changed by the trigger when we inserted the survey result
       System.assertEquals(updated_contact.FirstName,'New First Name');
       System.assertEquals(updated_contact.LastName,'New Last Name');
       System.assertEquals(updated_contact.Email,'test@email');

    }
}

In some instances of Salesforce, you can now develop triggers directly in your Salesforce environment. Using this method, you just edit the Survey Result object from Setup->Create->Objects, then click New in the Triggers section. Once the trigger has been created, you can add the test code from Setup->App Setup->Develop->Apex Classes.

If you do not have the ability to directly build triggers and classes mentioned above, it is easiest to develop and launch triggers and test code using the Eclipse development environment http://wiki.developerforce.com/index.php/Force.com_IDE).  You also can develop in a dev account or sandbox and use Salesforce’s Migration Tool to move Apex code from the sandbox or dev account to your live org (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_deploying_ant.htm).

Helpful links for Apex/trigger development: