0

I have a picklist in my VF page and have set an event.....based on the value that is picked i need to set a percentage field with some value which i think is not happeing....i'm sure there is something i'm missing and need help to fix it please?

my vf code:

            <apex:inputfield value="{!Opportunity.StageName}" >
            <apex:actionSupport id="oprstage" event="onchange" action="{!setProbability}" rerender="productInfo" immediate="TRUE" />
        </apex:inputfield>
        <apex:inputfield value="{!Opportunity.Day_Rate__c}" id="oprrate"/>
        <apex:inputfield value="{!opr.Probability}" id="oprprob"/>

controller:

public with sharing class OpportunitySave {
public string probability {get;set;}
public Opportunity opr {get;set;}
public OpportunitySave(ApexPages.StandardController controller) {

}
public PageReference setProbability(){
   if (Opportunity.StageName == 'Sign-off Deal')
      opr.Probability = '30%';
    return null;
}

}

since this is my first VF creation pls excuse me for any silly things done in the code or that i have missed

2 Answers 2

1

In Place of ActionSupport you can use ActionFunction. ActionSupport with Required field does not work correctly. So onchange event call the actuion function.

When we use ActionSupport with Required=True it not only does actionSupport skip the validation rules but it also skips all setter methods.

Reference 1

Reference 2

Also about your error first initialize the variable opr and then use it in the code. currently it is null and you are trying to set value thats why it is giving you error.

Code Sample

    <apex:inputfield value="{!Opportunity.StageName}" onchange="selectProedure()">

    <apex:actionfunction name="actfunProced" action="{!onChangeProcedure}" action="{!setProbability}" rerender="productInfo" immediate="TRUE"/>
<script>
     function selectProedure() {
         actfunProced(); 
     }
</script>

Try this piece of code it will solve your problem.

5
  • What you mean by doesn't work correctly? Are there any known issues? Could you paste some links? Oct 21, 2015 at 10:40
  • i get the error when i pick a value from the picklist ' System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!setProbability}' in page opportunitysave: Class.OpportunitySave.setProbability: line 8, column 1 Class.OpportunitySave.setProbability: line 8, column 1' Oct 21, 2015 at 10:42
  • There is problem with opr. I don't find where you in initialize oprr. Oct 21, 2015 at 10:43
  • 1
    @BartJuriewicz i have added the reference. Please check thanks. Oct 21, 2015 at 10:54
  • What shud be done here? as i'm new to VF i'm quite confused on how this works Oct 21, 2015 at 10:57
0

If your requirement is to get the value of selected Opportunity into apex then you have to modify your code as below. Hope it helps.

..........

    private ApexPages.StandardController stdCtrl;
    public PageReference setProbability(){
       opr = (Opportunity)stdCtrl.getRecord();
       if (opr.StageName == 'Sign-off Deal'){
          opr.Probability = '30%';
       }
       return null;
    }
.............
1
  • i still get the same error: 'System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!setProbability}' in page opportunitysave: Class.OpportunitySave.setProbability: line 9, column 1' Oct 21, 2015 at 11:55

You must log in to answer this question.

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