How To Create an Apex Trigger for a Prefilled Form

For those instances where you'd like to have more control over which records get custom links generated for them, run into errors while attempting to auto-deploy prefill triggers from our application, or have some advanced logic to include in your trigger, Formstack comes equipped with an easy-to-use link generator function that allows you to generate links on the fly to any of your forms. 

You can leverage this function using an Apex trigger to generate a link for you every time a new record is created and/or updated.

The following detailed example explores how you can do this.

Note:

  • We recommend Developers or Salesforce Administrator with Apex Trigger experience use the following guide. If you do not have a Developer on hand, please let us know so we can potentially reach out to a Partner through our Professional Services option.
  • At this juncture, our Support Team does not support troubleshooting Apex Triggers due to their highly variable nature. We appreciate your understanding!

Say you have an event registration form available on your public website that captures information into a custom Event Registration object. Perhaps you'd like to set up your form such that the registrant receives an email upon submission with their registration details as well as a custom link that they can use to update their submission after the fact. Using Apex triggers, workflow rules, and Formstack's Generate URL function, you can achieve this with the following steps:

  1. Add a custom field to your Event Registration object to store the custom prefilled URL to your form. Call it Event Registration URL and make sure it is of type URL.
  2. Create your Event Registration form using Formstack.
  3. While in the form editor, make note of the record ID of your form. This is an 18-character string that you can find at the end of URL in your browser's address bar. Your URL may look like the following. The item in bold is the string you'll want to record.
  4. https://na10.visual.force.com/apex/FormEditor?id=a0Di000000enKxbEAE
  5. Create a custom Apex trigger (Setup -> Develop -> Apex Triggers) as follows:
trigger FastForms_GeneratePrefillLink on Event_Registration__c (after insert)

{

if  (trigger.isAfter  &&  trigger.isInsert) {

  List<Event_Registration__c>  newlyInsertedItems =  [SELECT  Id ,  Event_Registration_URL__c  FROM  Event_Registration__c  WHERE  Id  IN :trigger.new] ;

  List<string> ids = new List<string>();

for ( Event_Registration__c  e  :  newlyInsertedItems) { 

  ids.add(e.id);

  }

VisualAntidote.FastFormsUtilities.DoUpdateRecords ( 'Event_Registration__c' ,  'Event_Registration_URL__c' ,  'a0Di000000enKxbEAE' ,  ids,[optional prefix url] );

// Event_Registration__c - Replace with the primary object API name

// Event_Registration_URL__c - Replace with the primary object field API name that will store the prefill link

// a0Di000000enKxbEAE - Replace with the Form ID from your production form

// [optional prefix url] - If you are not using a custom url domain please replace [optional prefix url] with null


update newlyInsertedItems;

}

}

That's it! You now have a URL to your custom form for each new Event Registration record that is created, prefilled with that record's information. You can then use that field as a mail merge field in an email template for use in a workflow rule or auto-response rule. 

There are a few items to consider when creating a trigger for an auto-generated prefill URL for your form:

  • You can only create triggers in developer editions of Salesforce or in a sandbox. For creating a trigger such as the one above, you will need to first create your form and trigger in a sandbox environment and deploy that trigger to your production environment using a change set. Click here for more details on how to do this.
  • Any triggers you create in your sandbox environment must have full Apex test coverage. To achieve this, you will need to create a test class for every trigger you create. A simple sample test class is included below, though we recommend writing your own test classes that suit your organization's business needs. This test class will insert a dummy contact record, then assert that a call is made to Formstack's URL generator method.
@IsTest

private with sharing class TestFastForms_GeneratePrefillLink{

@IsTest

  private static void testContactTriggerViaDML()

{ Test.startTest();

Contact testContact = new Contact(FirstName  =  'Test', LastName  =  'Contact', Email  =  'testcontact@company.com' );

// All code in your test class is only simulated and no new records are actually created.

// This line must be changed to create a record of your primary object. All required fields on the primary object must be included. If you have a required lookup field on this object then it must be set to the id of an existing record in your org of the object type of your lookup field.

insert testContact;

Test.stopTest();

  }

}

Related Articles

Screen_Shot_2019-03-14_at_3.29.21_PM.png

Troubleshooting Auto Generated Prefill Issues

Was this article helpful?
0 out of 5 found this helpful

Comments

0 comments

Article is closed for comments.