function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Thomas CoffeyThomas Coffey 

Compiled formula size calculated incorrectly... what to do?

I have a formula. 

When I click "Check Syntax" Salesforce tells me "No syntax errors in merge fields or functions. (Compiled size: 2,325 characters)"

But when I click "Save" Salesforce tells me "Compiled formula is too big to execute (6,496 characters). Maximum size is 5,000 characters (Related field: Formula)"

Here's a screenshot showing the two different character counts on the same formula:

One formula with two different compiled sizes

I took a look at the constituent elements of the formula.

Vehicle_1_Coverage_Score = 295 characters
Vehicle_1_Value = 41 characters

Vehicles 2-6 use the exact same formulas / have the exact same character counts as Vehicle 1.

Formula calls Coverage score 1x and Value 2x for each of 6x vehicles.

So I'd expect my formula character count to be in the neighborhood of (295 + 2 * 41) * 6 =  2262

When you add in a few extra characters for other characters in the formula (e.g. "(", ")", "+", "/", etc.) you get the 2325 character count that the "check syntax" button produces.

I have no idea where the other 4100 or so characters are coming from in the other character count. 

I think 2325 is the true character count and I'd like Salesforce to recognize this as the correct count and allow me to save.  Any ideas how to accomplish this?

Additional info: screenshots showing Vehicle_1_Coverage_Score = 295 characters and Vehicle_1_Value = 41 characters 

Coverage score = 295 characters

Vehicle Value = 41 characters

Abdul KhatriAbdul Khatri
Please copy and paste your code here in the following site for letter count
https://www.lettercount.com/

Sometimes spaces also counts against char. Please remove any extra spaces and try again.
Thomas CoffeyThomas Coffey
Thanks for this suggestion but I don't think it's going to work in this instance. 

If I copy and paste my formula into that tool it counts the length at 473 characters.  

473 isn't the right number because it doesn't include the size of formulas referenced by my formula.

Any other suggestions?

 
Alain CabonAlain Cabon
Hi,

You should post more details about the internal formulas : Vehicle_x_Coverage_Score and Vehicle_x_Value if you want that other people reproduce your problem.

For a so simple formula (as shown), something is wrong in the internal formulas.

I found nothing about the difference between the checked compiled size and the saved compiled size (should be the same logically or the checked compiled size is always useless and wrong). That seems a bug.

By estimation (trying all the functions), there are these compiled costs of each formula function:

https://salesforce.stackexchange.com/questions/148493/what-are-the-compiled-costs-of-each-formula-function
 
Thomas CoffeyThomas Coffey

Alain, thanks for sharing the link to stack exchange with the formula lengths.

Vehicle_x_Value formula simply pulls in a static value number field from a different object (our Vehicles object).  
Vehicle_x_Value = Vehicle_1__r.Vehicle_Value__c  (where Vehicle_1__r.Vehicle_Value__c is a regular, not a formula, number field on the vehicle object.

User-added image


Unfortunately I cannot share the formula for Vehicle_x_Coverage_Score (it's a trade secret) but I can tell you a little about that formula:
- it is of the format Vehicle_x_Coverage_Score = EXP (x)
- the formula for x references Vehicle_X_Value 2x, and two other static number fields (not number/formula fields) 1x each
- Vehicle_X_Value__c has a character count of 41
- one of the other static fields has a character count of 33
- the other static field has a character count of 141 (I think this is a little longer because it is pulled in from a related object)

User-added image
 

Like you, I think there is a bug - check compile size should equal save compile size - and have opened a case with Salesforce but hoping for some help in the interim.  Let me know if you have any suggestions.

Thanks again! 

Alain CabonAlain Cabon
Ok, you have already gone deeper into the question and you are focused on the huge difference between the checked and saved sizes (the ratio of one to two is abnormal and above all blocking).

Unfortunately if the SF support doesn't find a solution for your formula, you will need a stored calculated value by a trigger or a flow (with some invocable apex code). Each time one of the objects used by the formula is updated, the trigger will recalculate a new value and store it.

In Apex, you will not have this problem of size limit but the result values must be stored if you want to use them in batches or reports.

If the result value of the formula is just shown on a screen, you can just create a little VFP with its own apex controller and put this VFP directly in the standard layout (point and click).
 
Thomas CoffeyThomas Coffey

I'm aware of the formula to value process trick.  

The problem with that approach is that we have a connected web app and our developer said that designing wait times (to allow the process to run) into the web app causes problems so we just removed all of those processes from our instance. 

Also when a lot of objects / records are involved in these formulas, designing triggers that function properly and processes that don't error can be tricky.

So hopefully Salesforce can offer some insights regarding the bug.  

Thanks again.

 

Alain CabonAlain Cabon
For sensitive information, the updates by triggers of stored values are tricky for sure and need regular checks and complete recalculations for all the data (that is never 100% accurate, simply because a trigger is not always called for all the possible updates).

Many exceptions: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_ignoring_operations.htm

The best solution is still a compiled formula as far as possible or a VFP with a controller (always accurate but cannot be used into reports).
For only batches (formula never used in reports), the apex code can also recalculate the values on the fly but that can be heavy indeed. 

Finally if you don't need these values in reports, the stored values are not necessary (always calculated on the fly with a compiled apex code).

Good luck for some insights regarding the bug ( +  https://salesforce.stackexchange.com/   )