2

I am trying to sort records and I encountered the below markup in a Visualforce Page:

<apex:commandLink action="{!ViewData}" value="Phone{!IF(sortExpression=='Phone',IF(sortDirection='ASC','?','?'),'')}">
    <apex:param value="Phone" name="column" assignTo="{!sortExpression}" > 
    </apex:param>
</apex:commandLink>

Here I know the use of <apex:param>, but I do not understand the IF function. Why is it being used in this way?

1
  • 1
    Visualforce isn't code, it's markup.
    – Adrian Larson
    Apr 19, 2018 at 18:12

2 Answers 2

12

This code was actually copy-pasted from a UTF-8 file to an ASCII file at some point, which turned the original symbols into "?", because they were not convertable from UTF-8. The original code probably should have looked like this:

Phone{!IF(sortExpression=='Phone',IF(sortDirection='ASC','▲','▼'),'')

This is a pretty common design; if the field is sorted by "sortExpression", then display either arrow, depending on if sortDirection is ascending or not.

The apex:param on the next line is used by the apex:commandLink's ViewData method to set the sortExpression field to Phone when the command link is clicked, thus causing the sorting to change.

Edit: In fact, it may have literally been ripped from one of my older answers that demonstrates how to do this properly.

5

This code is very confused. Let's pull out the IF expression:

{!IF(sortExpression=='Phone', 
     IF(sortDirection='ASC',
        '?',
        '?'),
    '')}

This IF results in one of two values. If sortExpression is 'Phone', the whole IF evaluates to '?'. The inner IF() function effectively does nothing, returning "?" regardless of whether it is true or false. It could be replaced with the constant string '?'

If sortExpression is not 'Phone', the whole thing evaluates to an empty string, ''.

Edit: unfortunately I cannot delete this answer as it's already been accepted; please refer to sfdcfox's answer, which divined what was behind this code.

5
  • the code was very confusing that is why i didn't get much from it Apr 19, 2018 at 12:24
  • 1
    This answer, unfortunately, is incorrect.
    – sfdcfox
    Apr 19, 2018 at 13:56
  • I'm glad you caught the actual intent behind the code, which was not apparent to me, but I'd say it's the code that is wrong here!
    – David Reed
    Apr 19, 2018 at 13:59
  • 1
    @DavidReed It's still incorrect, as apex:param doesn't do what you think it does. Try looking at the answer I linked in my answer for an explanation of how it works.
    – sfdcfox
    Apr 19, 2018 at 14:02
  • Oy. You're quite right, of course, I misinterpreted that.
    – David Reed
    Apr 19, 2018 at 14:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .