Form response and automatic mapping to reference fields

The Form Response record

Every time a form is loaded and submitted, a FormResponse__c record tracks it. It is the audit trail for the submission: which form and version, what device and browser, the outcome, and which records were created or updated.

Lifecycle

The response moves through states in its Type field:

State When
DRAFT Created on first load, or saved for later
AUTOSAVE Periodic save while filling
COMPLETE Submitted successfully
FAILED Submission errored

On success the response stamps the Main Record id with the final saved record. On failure it records the error message and error type. Draft and incomplete states let a submitter return and continue. See Save as draft.

The Main Record id

The Main Record id field, whose API name is New_Record_ID__c, holds the id of the record the submission created or updated. It also records whether that was an Insert or an Update. This one field is the anchor for the automatic mapping described below.

Automatic mapping to reference fields

Response records auto link to real Salesforce records without any per object configuration. A before save trigger does this whenever the Main Record id is set or changed.

The logic is schema driven:

  1. The trigger inspects every custom lookup field on the response object and groups them by the object each looks up to. Out of the box that is Account__c (looks up to Account) and Contact__c (looks up to Contact).
  2. It derives the object type of the Main Record id from the id itself.
  3. For every lookup on the response that points at that object type, it fills the lookup with the Main Record id, but only if the lookup is currently empty.

So if a submission's Main Record is an Account, the response's Account lookup is filled automatically. If it is a Contact, the Contact lookup is filled. Because the logic reads the schema rather than a fixed list, this extends to your own fields: add a custom lookup from Form Response to any standard or custom object, and it will be populated automatically whenever a response's Main Record is of that object's type.

Existing values are never overwritten, so any lookup you set manually or through automation is left alone.

Reporting benefit

Because responses auto link to Account, Contact and any object you add a lookup for, you can build related lists and reports of form activity directly on those records. An Account shows its Form Responses; a custom object shows its Form Responses, with no mapping setup.

The legacy mapping metadata

The package ships a custom metadata type with Account and Contact mapping records. These are not read by the current trigger, which derives everything from the schema. Treat those records as documentation of the default behaviour, not as configuration you need to maintain. To add a new mapping, you add a lookup field, not a metadata record.