4

Salesforce telephone support tells me that I should not use Visualforce variables in this way because they're only intended for display in a Visualforce page and not for inclusion in a calculation. That would seem to indicate that all of the successful calculations included below are simply unintended consequences or side effects.

Am I simply "doing it wrong" here, or could this be a bug on the Salesforce side?

<apex:page >
    <apex:variable var="First" value="{!10}" />
    <apex:variable var="Second" value="{!5}" />

    <apex:variable var="Test1" value="{!First + Second}" />
    <apex:variable var="Test2" value="{!First - Second}" />
    <apex:variable var="Test3" value="{!First * Second}" />
    <apex:variable var="Test4" value="{!First / Second}" />

    {!Test1} {!Test2} {!Test3} {!First / Second} {!Test4}
</apex:page>

When I attempt to save the above code into a Visualforce page, I receive the following error:

Error: Unknown property 'Test4' referenced in TestPage

I can eliminate the error by removing the {!Test4} reference from the end of the page, or by replacing the "Second" value in the Test4 variable with the number 5. The error also appears if instead of this division problem I pass a variable as the second parameter to the MOD function and assign that expression to a variable.

6

I believe this is a bug, because the following code does work:

<apex:page standardController="Opportunity">
    <apex:variable value="{!0}" var="totalItemCount" />
    <apex:variable value="{!0}" var="totalPrice" />

    <apex:repeat value="{!Opportunity.OpportunityLineItems}" var="lineItem">
        <apex:variable var="totalItemCount" value="{!totalItemCount + lineItem.Quantity}" />
        <apex:variable var="totalPrice"     value="{!totalPrice + lineItem.TotalPrice}" />
    </apex:repeat>
    Average Product Price: {!IF(totalItemCount > 0, totalPrice / totalItemCount, 0)}
</apex:page>

Despite the fact that the documentation clearly states:

Note: <apex:variable> does not support reassignment inside of an iteration component, such as <apex:dataTable> or <apex:repeat>. The result of doing so, e.g., incrementing the <apex:variable> as a counter, is unsupported and undefined.

However, the fact that the division does work in this page, but not when assigned to a variable first, suggests that there's something broken about how the page is compiling variables into expressions. There's no value that you should be able to assign to a variable that should cause the variable to be completely unset. Nowhere in the documentation does it say that there's a way to remove a declared variable.

Also, if variables couldn't be used inside "calculations," they'd be completely useless, because, by definition, using the variable for any type of output at all is a calculation (it has to be evaluated like any other formula). Also, trying to sneak around the division problem also doesn't work:

<!-- x / y = x * (1 / y) -->
<apex:variable var="averagePrice" value="{!totalPrice * (1 / totalItemCount}" />

<!-- convert to text? -->
<apex:variable var="averagePrice" value="{!totalPrice/VALUE('' & TEXT(totalItemCount)) }" />

<!-- and also try to transform the text? -->
<apex:variable var="averagePrice" value="{!totalPrice/VALUE(LOWER(TEXT(totalItemCount))) }" />

<!-- add then remove text? -->
<apex:variable var="averagePrice" value="{!totalPrice/VALUE(RIGHT('X'&TEXT(totalItemCount),LEN(TEXT(totalItemCount))))}" />

<!-- x^1 = x? -->
<apex:variable var="averagePrice" value="{!(totalPrice^1)/(totalItemCount^1)}" />

<!-- ... you get the idea ... -->

It appears that a variable anywhere in any divisor causes Visualforce to forget about the variable being assigned to until it is next set.

If "calculations" are truly not supported on variables, they have a bug, because everything else is calculating correctly, and the documentation also needs to be updated. If "calculations" are supported, they have a bug, because division is utterly broken, and the documentation also needs to be updated. In other words, either way, support has a responsibility to reach out to R&D to have a bug logged, and have the documentation updated. If they flat out refuse, consider reaching out to @metadaddy, @asksalesforce, even @Benioff. Surely there's someone that will listen.

  • 2
    Salesforce support have indicated that they believe it could be a bug and have decided to escalate the ticket to the Tier-3 team who will work with the R&D team and determine what the correct behavior should be. – SF1Dev May 3 '16 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.