1

I'm having the following snippet in the VF page:

<apex:inputField id="reason" value="{!case.reason}" required="true"/>

which is displaying some case reasons for me.

By calling the following snippet I'm able to save changes which were performed on this page:

<apex:commandButton id ='save' action="{!save}" value="Save" />

I want to get a value which I choose in the reason field to be able to perform some comparison like this:

public PageReference save()
{
  if (this.case.reason != 'Some Value From Page') {
    callSomeMethod();
  }
  return this.controller.save();
}

Is there a way to achieve this?

1
  • 1
    this has the hallmarks of confusion between the controller property case and the standard controller's Case sobject. Use edit and expose your controller's constructor
    – cropredy
    Feb 13 '19 at 18:32
4

You could just create a field on the apex class to hold that value. Like:

public String someValue{get;set;}

Then use that on the VF page referencing it like {!someValue}. Then you can use that field in the save() method by:

public PageReference save()
{
  if (this.case.reason != someValue) {
    callSomeMethod();
  }
  return this.controller.save();
}

Give that a try and let me know if you need me to elaborate.

If the some value String will be static then you can just do:

public PageReference save()
{
  if (this.case.reason != 'This Value') {
    callSomeMethod();
  }
  return this.controller.save();
}

Where the string is just hardcoded. I don't see anything wrong with this if the string will always be a certain value.

If the dropdown value is mapped to case.reason then you can just check the selection in the logic to determine if you run callSomeMethod()

3
  • I created such field in the controller and tried to add it to the VF page like {!someValue} but I'm getting the "Could not resolve the entity from value binding '{!someValue}' can only be used with SObjects, or objects that are Visualforce field component resolvable" error - this is what I got. Probably, I forget to mentioned that I'm populating the drop-down list from "{!case.reason}" and I need to execute my function if some value was choose in that drop-down.
    – heisenberg
    Feb 13 '19 at 18:10
  • Is the value static? If so then why not just do if (this.case.reason != 'Some Value)? Feb 13 '19 at 18:12
  • Sorry, it was my fault in the expression, everything woks fine.
    – heisenberg
    Feb 14 '19 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.