Excerpt | ||||
---|---|---|---|---|
| ||||
Some considerations for working with FormRecords. |
Use fieldsets to query fields on existing records
...
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 |