2

Inline if not working with inputfield...using two properties from the controller...compiler error: Syntax error. Missing ')'

<apex:inputField id="inpModelValue" value="
    {!IF(wrap.strdealerbaseparameter=='Retail',objPWDUT.AI_R0_Retail__c,
      IF(wrap.strdealerbaseparameter=='Delivery',objPWDUT.AI_R0_Delivery__c,
      IF(wrap.strdealerbaseparameter=='Booking',objPWDUT.AI_R0_Booking__c,
      IF(wrap.strdealerbaseparameter=='TestDrive',objPWDUT.AI_R0_Test_Drive__c,
      IF(wrap.strdealerbaseparameter=='Lead',objPWDUT.AI_R0_Lead__c,
      IF(wrap.strdealerbaseparameter=='QualifiedLead',objPWDUT.AI_R0_Qualified_Lead__c,
      objPWDUT.AI_R0_Retail__c))))))}"/>

Much Appreciated for the help!

1 Answer 1

3

Firstly, I would use a CASE instead of IFin this case since you are always checking the same field value wrap.strdealerbaseparameter.

I think that function below is more readable than what you have written.

{!CASE(wrap.strdealerbaseparameter,
      'Retail', objPWDUT.AI_R0_Retail__c,
      'Delivery', objPWDUT.AI_R0_Delivery__c,
      'Booking', objPWDUT.AI_R0_Booking__c,
      'TestDrive', objPWDUT.AI_R0_Test_Drive__c,
      'Lead', objPWDUT.AI_R0_Lead__c,
      'QualifiedLead', objPWDUT.AI_R0_Qualified_Lead__c,
       objPWDUT.AI_R0_Retail__c)}"

However, you can't use "static" binding in any other type of formula, so you have to be willing to use "dynamic" binding instead. Here's what that looks like:

<apex:inputField value="{!objPWDUT[CASE(wrap.strdealerbaseparameter,
      'Retail','AI_R0_Retail__c',
      'Delivery','AI_R0_Delivery__c',
      'Booking', 'AI_R0_Booking__c',
      'TestDrive', 'AI_R0_Test_Drive__c',
      'Lead', 'AI_R0_Lead__c',
      'QualifiedLead', 'AI_R0_Qualified_Lead__c',
       'AI_R0_Retail__c')]}" />

The [] syntax in Visualforce allows you to specify a string (even as the result of a formula evaluation) to determine which field to render/bind to, thus being "dynamic." You'll get runtime exceptions if the field name is misspelled or not queried in the original query.


See below for prior version:

However, this won't solve your problem as you can't have this sort of "dynamic binding" in the value attribute of apex:inputField (AFAIK).

What you can do is render different apex:inputField based on the the value of the wrap.strdealerbaseparameter property. Something like below:

<apex:inputField render="{!wrap.strdealerbaseparameter=='Retail'}" value="{!objPWDUT.AI_R0_Retail__c}"/>
<apex:inputField render="{!wrap.strdealerbaseparameter=='Delivery'}" value="{!objPWDUT.AI_R0_Delivery__c}"/>
<apex:inputField render="{!wrap.strdealerbaseparameter=='Booking'}" value="{!objPWDUT.AI_R0_Booking__c}"/>
<!-- etc. -->

That's the best I can come up with at the moment. Maybe someone else will have a better solution.

4
  • +1 smukov, you were close. It's actually possible to choose the field at runtime, and I've provided an example. The rest of the answer was spot-on, though.
    – sfdcfox
    Oct 23, 2016 at 20:22
  • Thank you @sfdcfox, that's a great solution. I love this community! You can learn so much here and everybody is nice and respectful. Thank you again for teaching me something new today.
    – smukov
    Oct 23, 2016 at 20:35
  • 1
    And we love helping people! I made another small adjustment, I had the {! in the wrong place. But I did test this in my developer org before I added the edit, so it definitely works.
    – sfdcfox
    Oct 23, 2016 at 20:37
  • Thanks @sfdcfox for the solution...it worked and I learnt something new!
    – piyush27
    Oct 24, 2016 at 15:35

You must log in to answer this question.

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