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
Walter@AdicioWalter@Adicio 

Is there a easy way to set a boolean based on the size of a list?

 I was trying to see if I can do it with something like this.

 

 

public boolean productAndServicesPod { get; set;} {if(quotes.size()==0)productAndServicesPod=false;}

 


 

Best Answer chosen by Admin (Salesforce Developers) 
bbrantly1bbrantly1

You could do it two ways:

 

 

public class TestForBlog { public boolean productAndServicesPod { get; set;} public TestForBlog() { productAndServicesPod = (quotes.size()==0)? false : true; } }

 

Since the constructor  is run first when the controller is called it will set the value for you.

 

The syntax of the inline if/then statment I used is as follows

 

 

value = (condition) ? true : false;

 

 

also could be written

 

 

if (condition) { value = true; } else { value = false; }

 

 

 

The second way would be:

 

 

private boolean pproductAndServicesPod; public boolean productAndServicesPod { get { return (quotes.size()==0)? false : true; } set { this.pproductAndServicesPod = value; } }

 

 Hope this helps.  Let me know if you have more problems and we can take a look at some more of your code to see what's going on.

 

 

 

 

 

All Answers

bbrantly1bbrantly1

You could do it two ways:

 

 

public class TestForBlog { public boolean productAndServicesPod { get; set;} public TestForBlog() { productAndServicesPod = (quotes.size()==0)? false : true; } }

 

Since the constructor  is run first when the controller is called it will set the value for you.

 

The syntax of the inline if/then statment I used is as follows

 

 

value = (condition) ? true : false;

 

 

also could be written

 

 

if (condition) { value = true; } else { value = false; }

 

 

 

The second way would be:

 

 

private boolean pproductAndServicesPod; public boolean productAndServicesPod { get { return (quotes.size()==0)? false : true; } set { this.pproductAndServicesPod = value; } }

 

 Hope this helps.  Let me know if you have more problems and we can take a look at some more of your code to see what's going on.

 

 

 

 

 

This was selected as the best answer
Walter@AdicioWalter@Adicio

Hi Bbrantly1,

 

Is this regular JAVA syntax versus apex specific? This is working for me by the way, thanks again.

 

 

private boolean ccustomer;public boolean customer{get{return (customerList.size()==0)? false : true;} set{this.ccustomer = value;}}

 

Thank you,

Walter 

 

bbrantly1bbrantly1

I'm glad it's working and what you were looking for.

 

value = (condition) ? true : false; Can be used in almost all programming languages

 

The other method is also very common in other languages.

 

So to answer your question, neither.  They aren't Java or apex specific.

 

Hope this helps.

Walter@AdicioWalter@Adicio

Yes singling this out does help me, thanks again.

 

 

value = (condition) ? true : false;

 

 The only thing i changed was a add a {get;set;} to the private boolean so I can call to that in a rendered attribute in the visualforce page.

 

 

private boolean showCustomer {get;set;}public boolean customer{get{return (customerList.size()==0)? false : true;} set{this.showCustomer = value;}}

 

 

<apex:outputPanel layout="block" rendered="{!showCustomer}">

 

 

 

bbrantly1bbrantly1

Assuming you are never writing to the showCustomer boolean, you don't need a seperate var.  You can simply use:

 

public boolean customer { get { return (customerList.size()==0)? false : true; } }

There is no need for a seperate boolean (showCustomer in this case)

 

You could also do it like this:

 

 

public boolean getCustomer() { return (customerList.size()==0)? false : true; }

 

Of course if you are writing back to the showCustomer boolean you will need to use a seperate boolean value like you have done.

Walter@AdicioWalter@Adicio

This is great, you are right, I only need this.

 

 

public boolean customer{get{return (customerList.size()==0)? false : true;}}