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
Jonas MyhreJonas Myhre 

If statement help

Hi guys, this will be quite hard to explain, but i will try my best:

I have created a custom object called "Drift status" (currently a draft in the sandbox). This is created so we have an easy way to show our current issues to both our customers and internal users. I have then created an Apex class which gets this information and i use that as a visual force area home page component. It works fine if i have 1 active record (defined by a checkbox called "Visible"), if i have more or less it gives an error message. Im not fairly experienced in programming, so i need some help here. Ill post my code below:


Apex class:
global with sharing class driftsStatus{

     public Drift_Status__c status{get;set;}

     public driftsStatus() {

         status= [select Comment__c,ETA_Fix__c,Service__c from Drift_Status__c where Visible__c = true];
        

      }

}

So i need some guides on how to handle it if Visible__C count is less or more than 1. I would like to show all the records if there are more than 1, and a message if its 0 visible. Thanks alot
Best Answer chosen by Jonas Myhre
Vatsal KothariVatsal Kothari
Change the var value to something else.

<apex:page controller="driftsStatus" showHeader="true">
   <div align="left" width="550px">

<apex:pageblock title="Drift Status">
<apex:pageblocktable value="{!Status}" var="s">

<apex:column value="{!s.ETA_Fix__c}"/>
<apex:column value="{!s.Comment__c}"/>
<apex:column value="{!s.Service__c}"/>


      <b>
        <br> <apex:outputText value="Service: {!s.Service__c}"/></br>
        <br> <apex:outputText value="Comment: {!s.Comment__c}"/> </br>
        <br> <apex:outputText value="ETA Fix is: {!s.ETA_Fix__c}"/> </br>
      </b>

</apex:pageblocktable>
</apex:pageblock>

</div>
</apex:page>

try with this code, it will work.

All Answers

Vatsal KothariVatsal Kothari
Hi,

Refer the below updated code:

global with sharing class driftsStatus{

     public List<Drift_Status__c> status{get;set;}

     public driftsStatus() {

         status = [select Comment__c,ETA_Fix__c,Service__c from Drift_Status__c where Visible__c = true];       

      }

}
"Drift_Status__c status" will contains only one record but if convert it into List<Drift_Status__c> than it will handle one or more records.

Now you can use this list in your Visualforce page and iterate it using pageBlockTable.

you can refer this link for PageBlockTable:
http://nanostuffs.com/Blog/?p=751

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

Jonas MyhreJonas Myhre
Thanks! i've done that now, got one step further, thats good! New error occured though: 

"Unknown property 'VisualforceArrayList.Service__c'
Error is in expression '{!status.Service__c}' in component <apex:pageBlockTable> in page newdriftstatus"


This is the visualforce component (iknow its horrible, please dont shoot me):



<apex:page controller="driftsStatus" showHeader="true">
   <div align="left" width="550px">

<apex:pageblock title="Drift Status">
<apex:pageblocktable value="{!status.Service__c}" var="status">
<apex:column value="{!status.Service__c}"/>
<apex:column value="{!status.Comment__c}"/>
<apex:column value="{!status.ETA_Fix__c}"/>

      <b>
        <br> <apex:outputText value="Service: {!status.Service__c}"/></br>
        <br> <apex:outputText value="Comment: {!status.Comment__c}"/> </br>
        <br> <apex:outputText value="ETA Fix is: {!status.ETA_Fix__c}"/> </br>
      </b>
      </apex:pageblocktable>
</apex:pageblock>

   </div>
</apex:page>




Vatsal KothariVatsal Kothari
Replace below line
<apex:pageblocktable value="{!status.Service__c}" var="status">

with
<apex:pageblocktable value="{!status}" var="status">



Jonas MyhreJonas Myhre
Hm, cant seem to save it. I get the error message below, if i try to open the page, the old changes are still there. What am i doing wrong?

Error Error: Unknown property 'VisualforceArrayList.Service__c'
Jonas MyhreJonas Myhre
The error message seems to be based on the first value of these three lines:

<apex:column value="{!status.Comment__c}"/>
<apex:column value="{!status.Service__c}"/>
<apex:column value="{!status.ETA_Fix__c}"/>

i put ETA_Fix__c on the top and the error message is: Error Error: Unknown property 'VisualforceArrayList.ETA_Fix__c'
Vatsal KothariVatsal Kothari
can you please paste your updated apex class and visulforce page?
Jonas MyhreJonas Myhre
Apex class: 

global with sharing class driftsStatus{

     public Drift_Status__c status{get;set;}

     public driftsStatus() {

         status= [select Comment__c,ETA_Fix__c,Service__c from Drift_Status__c where Visible__c = true];

      }

}

VF page:

<apex:page controller="driftsStatus" showHeader="true">
   <div align="left" width="550px">

<apex:pageblock title="Drift Status">
<apex:pageblocktable value="{!Status}" var="Status">

<apex:column value="{!status.ETA_Fix__c}"/>
<apex:column value="{!status.Comment__c}"/>
<apex:column value="{!status.Service__c}"/>


      <b>
        <br> <apex:outputText value="Service: {!status.Service__c}"/></br>
        <br> <apex:outputText value="Comment: {!status.Comment__c}"/> </br>
        <br> <apex:outputText value="ETA Fix is: {!status.ETA_Fix__c}"/> </br>
      </b>

</apex:pageblocktable>
</apex:pageblock>

</div>
</apex:page>
Vatsal KothariVatsal Kothari
Update your apex class with below code:

global with sharing class driftsStatus{

     public List<Drift_Status__c> status{get;set;}

     public driftsStatus() {

         status= [select Comment__c,ETA_Fix__c,Service__c from Drift_Status__c where Visible__c = true];

      }

}


Jonas MyhreJonas Myhre
If i remove the "List" word, it works fine with one entry, if i have the list there in the apex class, it fails no matter how many records i have visible. Does  to field type have anything to say? 

I get this error message: Error: Unknown property 'VisualforceArrayList.Service__c'
Vatsal KothariVatsal Kothari
Change the var value to something else.

<apex:page controller="driftsStatus" showHeader="true">
   <div align="left" width="550px">

<apex:pageblock title="Drift Status">
<apex:pageblocktable value="{!Status}" var="s">

<apex:column value="{!s.ETA_Fix__c}"/>
<apex:column value="{!s.Comment__c}"/>
<apex:column value="{!s.Service__c}"/>


      <b>
        <br> <apex:outputText value="Service: {!s.Service__c}"/></br>
        <br> <apex:outputText value="Comment: {!s.Comment__c}"/> </br>
        <br> <apex:outputText value="ETA Fix is: {!s.ETA_Fix__c}"/> </br>
      </b>

</apex:pageblocktable>
</apex:pageblock>

</div>
</apex:page>

try with this code, it will work.
This was selected as the best answer
Jonas MyhreJonas Myhre
Thanks alot Vatsal. Im not sure what has happened, but i tried in Internet Explorer instead of Chrome, which showed the same error over and over again, and it works there. Might be some Chrome issues / cache settings, which i will try to figure out tomorrow.

Thanks alot for your time and patience, ill buy you a beer if we ever meet! Cheers!