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
Joe HayesJoe Hayes 

Multiple IF Statements inside Visualforce page, is there an easier way?

Hi,

I have a vf page that renders as a pdf and displays a dataTable of results from a related list on an object, it is working well apart from the following.

I need to add a column in my data table that displays a description of the item in the 'Code' Column.
I have done this using if statements and it is working nicely, however, I am worried that I will hit the maximum formula compile size as I will be adding a lot more to this in the future.

What is the best way to get around this to generate the correct descriptions without huge multiple if statements?

section of current vf page
<div class="content">
<apex:dataTable value="{!AsstList}" var="item" border="0" cellpadding="5" cellspacing="1" width="567px" style="margin: auto auto;">

<apex:column width="20%" headerValue="Code">
<apex:outputText value="{!item.CodeTEXT__c}">
</apex:outputText>
</apex:column>

<apex:column width="60%" headerValue="Description">
<apex:outputText value="{!IF(item.CodeTEXT__c = 'Code1','Code1 Description Here',
                        IF(item.CodeTEXT__c = 'Code2','Code2 Description Here',
                        IF(item.CodeTEXT__c = 'Code3','Code3 Description Here',
                        IF(item.CodeTEXT__c = 'Code4','Code4 Description Here',
                        IF(item.CodeTEXT__c = 'Code5','Code5 Description Here',
                        IF(item.CodeTEXT__c = 'Code6','Code6 Description Here','NULL'))))))}">
</apex:outputText>
</apex:column>

<apex:column width="20%" headerValue="Expiry Date">
<apex:outputText value="{0, date, dd/MM/yyyy}">
       <apex:param value="{!item.Expiry_Date__c}" />
       </apex:outputText>
</apex:column>
</apex:dataTable>
</div>

Thanks
Best Answer chosen by Joe Hayes
Alexandru IleanaAlexandru Ileana
Hello,

I have randomly stumbled upon your post.
I think I understand what you are saying... depending on what value is selected on each record on the codeTEXT__c dropdown field, you want to display a predefined description text next to it. A simple solution to this would be to simply add a text field (hidden from the layout of the object if you like) that would memorise these descriptions. On the backend you obviously have a list returned by a method called "getAsstList()". Why not do this:

List<Item__c> getAsstList()
{
    return [SELECT Id, CodeTEXT__c, Expiry_Date__c, Code_Description__c FROM Item__c];
}

Of course this will look ugly... you have to sync values between the dropdown and actual text somehow or simply make sure you manually add the right description...


The best way I can see this working is to have a junction object called let us say... ItemDetail__c
with fields like:
Name // the code
Description__c // the code description

Then in Item__c you have:
ItemDetail__c (lookup)

and in you page:
<apex:column width="60%" headerValue="Description">
<apex:outputText value="{!item.ItemDetail__r.Description__c}"/>

Hopefully this works for you!
Best Regards,
Alex.

All Answers

Alexandru IleanaAlexandru Ileana
Hello,

I have randomly stumbled upon your post.
I think I understand what you are saying... depending on what value is selected on each record on the codeTEXT__c dropdown field, you want to display a predefined description text next to it. A simple solution to this would be to simply add a text field (hidden from the layout of the object if you like) that would memorise these descriptions. On the backend you obviously have a list returned by a method called "getAsstList()". Why not do this:

List<Item__c> getAsstList()
{
    return [SELECT Id, CodeTEXT__c, Expiry_Date__c, Code_Description__c FROM Item__c];
}

Of course this will look ugly... you have to sync values between the dropdown and actual text somehow or simply make sure you manually add the right description...


The best way I can see this working is to have a junction object called let us say... ItemDetail__c
with fields like:
Name // the code
Description__c // the code description

Then in Item__c you have:
ItemDetail__c (lookup)

and in you page:
<apex:column width="60%" headerValue="Description">
<apex:outputText value="{!item.ItemDetail__r.Description__c}"/>

Hopefully this works for you!
Best Regards,
Alex.
This was selected as the best answer
Joe HayesJoe Hayes
Thanks Alexandru,

A junction object isnt a bad idea actually.

I might give that a go.

Thank you
Joe