Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt
hiddentrue
nameintro

Some considerations for working with FormRecords.

Use fieldsets to query fields on existing records

...

For example, your form displays Service Reservations, and your CFL has a handler that uses a checkbox on Service Reservations to decide what to do with them, but you do not want to display this checkbox on the form. When you test your CFL on an existing Reservation with existing Service Reservations, it fails because the checkbox has not been populated and always contains null. One option available to you is to modify your CFL to do your own query to get the values in this field. However, this is bad for performance (Service Reservations now get queried twice) and unnecessary. The better option is to simply add a fieldset on Service Reservation containing the checkbox field, and presto, the field is now populated.

Update formula fields before saving

You can use CFL to update formula fields in real-time, without forcing the user to save the reservation first.

Let’s say we have a field named My_Formula__c with formula B25__Account__r.Name + ': ' + B25__Contact__r.LastName.

First, create a handler that listens to updates on any field referenced by the formula.

Code Block
// do this inside the customise method of your CFL:
form.getField(B25__Reservation__c.B25__Account__c).onUpdate(new MyFormulaUpdater());
form.getField(B25__Reservation__c.B25__Contact__c).onUpdate(new MyFormulaUpdater());

Then, inside your handler, get the SObject record and pass it through Salesforce’s Formula.recalculateFormulas method.

Code Block
// do this inside the handleEvent method of your handler class:
B25.FormRecord theFormRecord = form.getActiveRecord();
SObject theSObjectClone = theFormRecord .getSObjectClone();
Formula.recalculateFormulas(new List<SObject>{theSObjectClone});

Finally, get the new value for the formula field and let the form update it by using the put method of B25.FormRecord.

Code Block
// still inside the handleEvent method of your handler class:
String theNewValue = theSObjectClone.My_Formula__c;
theFormRecord.put(B25__Reservation__c.My_Formula__c, theNewValue);

Important to note is that although the put method can update formula field values displayed on the form, subsequent calls to get or getSObjectClone will not contain the updated value in the field, as illustrated here:

Code Block
B25.FormRecord theFormRecord = form.getActiveRecord();
SObject theSObjectClone = theFormRecord.getSObjectClone();

theFormRecord.get(B25__Reservation__c.My_Formula__c); // returns the OLD value
theSObjectClone.get(B25__Reservation__c.My_Formula__c); // returns the OLD value

Formula.recalculateFormulas(new List<SObject>{theSObjectClone});

theFormRecord.get(B25__Reservation__c.My_Formula__c); // returns the OLD value
theSObjectClone.get(B25__Reservation__c.My_Formula__c); // returns the NEW value

theSObjectClone = theFormRecord.getSObjectClone();
theSObjectClone.get(B25__Reservation__c.My_Formula__c); // returns the OLD value