I'm trying to reset an Integer value to null, but it is converted to 0.

<apex:actionFunction action="{!doSomething}" name="doSomething" rerender="nothing">
    <apex:param name="indexValue" assignTo="{!index}" value="{!NULL}" />

public class MyController{
    public Integer index { get; set; }

    public void doSomething() {

Is there way to set it to null?

  • Hmmm, could you just set the variable to null directly in the apex method, if that method is used only for that purpose? – Sergej Utko Mar 4 '16 at 13:24
  • Not in my case :( I am actually not even calling any action in my code, I just reset the values and re render the component. But it has the same side effect as described. (Just kinda simplified it) – Basti Mar 4 '16 at 13:44
  • You might try defining it this way: public static Integer index; to see if that makes a difference for you. Declaring it as a static variable may allow you to assign it as null. The problem is that most methods and classes won't allow you to pass a null value for a variable, but if you're not passing it into your method and it's defined as static, it would persist and you could change it to null without passing it back into your class. Avoid putting it in a method and your problem will disappear. Ditto for the get, set – crmprogdev Mar 4 '16 at 13:52
  • I tried, but when I try to save the page it returns this: Error: Unknown property 'MyController.index'. Cause it has no access to the variable without getter and setter. – Basti Mar 4 '16 at 14:12

It looks like the problem is that when the parameter comes over as an empty string, salesforce converts the value to 0 instead of NULL. You could consider setting the value of index in your method based on the param value string:

public void doSomething() {
    String ParamValue = Apexpages.currentPage().getParameters().get('indexValue');
    index = ParamValue == '' ? NULL : Integer.valueOf(ParamValue);
  • I will accept this as the answer, cause it is the best rated and closest to what I asked for. (Even if the real answer would just be 'No'). In my actual use case it is not an option, cause I'm not calling any apex code, but just resetting the value and rerendering the area. I decided to reset it to -1. Thanks! – Basti Mar 8 '16 at 10:16
  • @Basti No problem, I hope I was able to help. Great question by the way. – martin Mar 9 '16 at 1:40

If you are always trying to clear out index in doSomething, just assign it in Apex.

public void doSomething()
    index = null;

Otherwise you could add a Boolean clearIndex property and set that instead.

public Boolean clearIndex { get; set; }
public void doSomething()
    if (clearIndex) index = null;
    clearIndex = false;

Then in your markup, change your actionFunction to:

<apex:actionFunction action="{!doSomething}" name="doSomething" rerender="nothing">
    <apex:param name="toClear" assignTo="{!clearIndex}" value="{!true}" />

You can change the setter method of the variable to check for 0 and set NULL.

public Integer index { 
        index = (index == 0) ? NULL : value;

One problem here is, if you try to assign the value 0, then it nullifies it.

@crmprogdev suggestion of making the variable static doesn't seem to work for me.

@adrianlarson's answer is quite apt if you are trying to set null always.

  • 1
    The variable is always null if your setter is set{} – martin Mar 4 '16 at 15:22
  • Oh I didn't know that. Removed the edit from my answer. – Vigneshwaran G Mar 4 '16 at 15:27
  • Also, the if(index == 0) index = NULL; part is fine, but you should add an else condition for other values: else index = value; – martin Mar 4 '16 at 15:32
  • Looks good. If you knew that the value of the integer would never be 0 otherwise, this could work. – martin Mar 4 '16 at 15:39

I ended up with Boolean variable.

On VF page:

<apex:inputHidden value="{!isActualHoursBlank}" id="actualHoursInputHidden"/>

<apex:actionSupport event="onclick" action="{!saveProduct}" 
  onsubmit="onSumbmitProductSave('{!$Component.actualHoursInput}', {!$Component.actualHoursInputHidden}');">

  function onSumbmitProductSave(actualHoursInputId, actualHoursInputHiddenId) {
    var actualHoursInputEl = document.getElementById(actualHoursInputId);
    var actualHoursInputHiddenEl = document.getElementById(actualHoursInputHiddenId);
    if (actualHoursInputEl.value == '' || actualHoursInputEl.value == null) {
      actualHoursInputHiddenEl.value = true;
    } else {
      actualHoursInputHiddenEl.value = false;

And in Controller I set Object's field as null in case isActualHoursBlank is set to true.

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.