Update on 9/2/20:
RecalculateFormulas – New Action Coming Soon
The original recalculate formulas action posted last week actually required a text collection of field names. I went ahead and created a new action that is a bit simpler to use – this new action now just takes an input collection and outputs a collection with refreshed formula fields. Make sure that all of the fields are present on the record you are passing in, otherwise it might not get calculated properly. Be on the lookout for that in the next couple of days!
Ever notice how formulas don’t get calculated for formula fields or you need to reference ‘refreshed’ values in Flow?
James Hou (sparkworks.io & Google) and JamesIsSoPro from the Salesforce Discord community came up with two very handy actions that let you re-calculate all of the formula fields in a record collection or easily re-query for refreshed values in a record collection. This is quite handy – especially when you want to display formula fields on USF’s Datatable component if you’ve built up your own record collection.
Refresh Formula Fields
When would I use this?
Use this when changes occur to a collection of records before it hits the database – i.e. in a loop.
Use this when you only want to recalculate formula fields to use throughout your flow as a record collection goes through changes before it is committed to the database. This is great for when you want to assign values in an assignment element but want Salesforce to have the latest value in a formula. Best of all it doesn’t use a SOQL statement to do it and you don’t need to create the records for the formulas to calculate!
Let’s say you’ve built or modified a collection of records to create at a later step in your flow through a Loop or through some action on UnofficialSF. Those collection of records haven’t actually hit the database yet, so any formula fields within that collection have not been recalculated yet. If you needed to reference a formula field that sits on a record, it would probably be inaccurate because it hasn’t been recalculated – it’s going to either be blank or wrong as it was using the formula value from when you initially grabbed it. This action will recalculate it for you so that you can then safely use the most updated value in an Assignment step or display it on a flow screen.
- I ‘Get’ 10 contacts – each of these contacts has a ‘Data Quality Score’ formula field that adds up to 100 based on various non-blank fields on the contact. (i.e. Email = 20, Address = 50, etc)
- I then loop through all 10 contacts and add in a value for one of the fields referenced in the formula. Per typical Flow usage I create a new collection of contacts to update the contacts later.
- If I reference this ‘new’ collection it will still have the old ‘Data Quality Score’ because those changes have yet to hit the database and the formulas have not been re-calculated yet. If I were to reference this field on a screen or in assignment for another record, it’d be wrong!
- I would then run this formula action on the new collection to force a recalculation of the Record Collection if I needed the most up-to-date formula value before doing any DML.
Note: Any issues / support for this action should be made on Jame’s Github Issues page (see Source link). Note that the text input is actually a text collection, not a comma-separated string. I will provide an updated action in the next couple of days.
Refresh/Requery Record Collection
When would I use this?
If you have a long-running Flow and you expect other users or processes (like an After-save Apex Trigger) to make changes to a collection of records, you want to be sure that the collection will have the latest information. But Flow will in these cases only have the old values based on when it did its original ‘GetRecords’ query.
Think of this action essentially as ‘Re-query Collection’ that quickly refreshes a collection.
Source link coming soon!
Unmanaged Package Install: Sandbox: https://test.salesforce.com/packaging/installPackage.apexp?p0=04t1I000003NYWN
Check out some other links that talk about the Calculate Formulas apex method that makes the first action possible: