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
Saurabh BSaurabh B 

Extension to dynamically Probability Update

Hello:

I have a VF page which dynamically shows field based on stages selected. What I need help with is an extension which will automatically populate Probabilty associated with each stage. I tried using below extension (OpportunityControllerExt) but it gives me an error "System.NullPointerException: Attempt to de-reference a null object". Not sure what is wrong? Can someone pls help me with fixing this code or provide an alternate solution which will work with this VF page? 

Any help is much appriciated! :)

VF Page:
 
<apex:page standardController="Opportunity" sidebar="True" extensions="OpportunityControllerExt" >
    <apex:sectionHeader title="Edit Opportunity" subtitle="{!opportunity.name}"/>
    <apex:form >
        <apex:pageBlock title="Edit Opportunity" id="thePageBlock" mode="edit">
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>                
            </apex:pageBlockButtons>
            <apex:actionRegion >
                <apex:pageBlockSection title="Basic Information" columns="1">
                    <apex:inputField value="{!opportunity.name}"/>
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Stage"/>
                        <apex:outputPanel >
                            <apex:inputField value="{!opportunity.stageName}">
                                <apex:actionSupport event="onchange" rerender="thePageBlock" action="{!updateProbability}" immediate="true"
                                                    status="status"/>
                            </apex:inputField>
                            <apex:actionStatus startText="applying value..." id="status"/>
                        </apex:outputPanel>
                    </apex:pageBlockSectionItem>
                    <apex:inputField value="{!opportunity.amount}"/>
                    <apex:inputField value="{!opportunity.closedate}"/>
                    <apex:inputField value="{!opportunity.Probability}"/>

                </apex:pageBlockSection>
            </apex:actionRegion>
            
                        <apex:pageBlockSection title="Closed Lost Information" columns="1"
                                   rendered="{!opportunity.stageName == 'Prospecting'}">
                <apex:inputField value="{!opportunity.Description}"  required="true"/>
                <apex:inputField value="{!opportunity.Type}"/>
                <apex:inputField value="{!opportunity.Dev_Sau_123__DeliveryInstallationStatus__c}"/>
                <apex:inputField value="{!opportunity.Dev_Sau_123__Languages__c}"/>
                
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Closed Lost Information" columns="1"
                                   rendered="{!opportunity.stageName == 'Closed Lost'}">
                <apex:inputField value="{!opportunity.Primary_Competitor__c}"  required="true"/>
                <apex:inputField value="{!opportunity.Reason_Lost__c}"/>
                <apex:inputField value="{!opportunity.OrderNumber__c}"/>
                <apex:inputField value="{!opportunity.TrackingNumber__c}"/>
                
            </apex:pageBlockSection>
            
            

        </apex:pageBlock>

    </apex:form>
    
</apex:page>

Extension:
 
public class OpportunityControllerExt {
    public static Opportunity opp {get;set;}

    private map<string,decimal> probabilities = new map<string,decimal>();
    public OpportunityControllerExt(ApexPages.StandardController stdController){        
        OpportunityControllerExt.opp = (Opportunity)stdController.getRecord();
       
    }
    public PageReference updateProbability(){
        //probabilities map was made before
        OpportunityControllerExt.opp.Probability = probabilities.get(OpportunityControllerExt.opp.StageName);

                    return null;
    }
}

 
Himanshu ParasharHimanshu Parashar
Hi,

Your function should be like this.
 
public PageReference updateProbability(){

        //probabilities map was made before
//fill if it is empty
if(probabilities==null)
{

for (OpportunityStage oppStage : [Select MasterLabel, DefaultProbability From OpportunityStage]) { 
probabilities.put(oppStage.MasterLabel, oppStage.DefaultProbability); }
}
        opp.Probability =probabilities.get(opp.StageName);

                       return null;
}

  
   if (opp.stagename!=null && opp.stagename!='' && probabilities.containsKey(opp.stagename)) {
     Opp.Probability = probabilities.get(opp.stagename);
   }

   return null;
 }
VF page change:

when you add immediate=true, SFDC doen't bind property with controller and we don't get stagename on controller side. you need to set it as false so that you can get opp.stagename on controller side.
<apex:actionSupport event="onchange" rerender="probability" action="{!updateProbability}" immediate="false"
status="status"/>

<apex:inputField value="{!opportunity.Probability}" id="probability"/>

Let me know if this works for you.


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant