4
Controller:
...
public boolean Closebutton {
    get {
        if(Opp.Status != 'Closed Lost' || Opp.Status != 'Closed Won')
        {
            return true;
        }
        return false;
    }
    Set;      
}
...

Page:
...
<apex:commandButton value="Close Opportunity" action="{!CloseOpp}" rendered="{!Closebutton}"/>

If I state any one condition or many conditions with AND statement, the button is obeying the condition and appearing/disappearing in the page. But when I state multiple conditions with ||, the button always appear on the page. Any idea?

4
  • Why are you comparing twice the same field and same value? Jun 6, 2014 at 4:40
  • Oh sorry. That was a typing mistake.
    – Seesh
    Jun 6, 2014 at 4:55
  • @Seesh AND and OR are relatively confusing to relatively new developers. You should practice.
    – sfdcfox
    Jun 6, 2014 at 6:44
  • @Seesh Try re-rendering the section which contains the button <aoex:commandButton id="closebtn" value="Close Opportunity" action="{!CloseOpp}" rendered="{!Closebutton}" rerender="closebtn"/> or rerender the outputpanel or pageblock or pageblocksection or form which contains that button. Jun 6, 2014 at 10:08

3 Answers 3

4

I think there is logical error in your IF condition.

It will always return TRUE

Say if Opp.Status is set to 'Closed Lost' then FIRST part of OR will be true and so it will return TRUE

if Opp.Status is set to 'Closed Won' then second part of OR will be true and so it will return TRUE

If Opp.Status is set to 'XYZ' then first part of OR will be true and so it will return TRUE

SO it short it will always return TRUE.

1
  • 1
    Stated otherwise, if Status is closed won, then the status will not be closed lost. The inverse is always true. "AND" is the appropriate operator here.
    – sfdcfox
    Jun 6, 2014 at 6:34
0

From your advice and suggestions, I'm clear that comparing the same field with two different values in an OR statement will always return TRUE. I've modified the code. and this code is working fine now.

Controller
...
public boolean Closebutton {
    get {
        if(Opp.Status != 'Closed Lost'){ 
            if(Opp.Status != 'Closed Won'){
                return true;
            }
        }
        return false;
    }
    Set;      
}
...

Page
...
<apex:commandButton value="Close Opportunity" action="{!CloseOpp}" rendered="{!Closebutton}"/>
...
-1

You have a space in your rendered attribute. Your boolean condition is looking for "Closed Win" when the standard status is "Closed Won", so that could also be the issue. Furthermore, the rendered attribute does not always render correctly with booleans.

Lastly why not simply compare either the Opp Status or the Opportunity IsClosed field in the rendered attribute, rather than creating a new variable for rendering? See an example below:

rendered="{!NOT(OR(Opp.status == 'Closed Won',Opp.status == 'Closed Lost'))}"

Or

rendered="{!NOT(Opp.IsClosed)}"

Note: You'll have to query for IsClosed if not doing so already.

More info on the Opportunity standard fields here: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_opportunity.htm

2
  • Hey @James Loghry. The space in the rendered attribute and the closed Win are my fault while typing in the Stackex. Yes, your suggestion is working good. But I'm not able to figure out what is the error I've made in the method. And comparing the status directly in the rendered attribute is fine for Production environment?
    – Seesh
    Jun 6, 2014 at 5:25
  • Like I mentioned, the rendered attribute is goofy occasionally. You may have to add a VF function around your boolean, either an IF(var,true,false) or the NOT() function you see in my example. Jun 6, 2014 at 5:46

You must log in to answer this question.

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