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
Che SFDCChe SFDC 

Probability not changing based on Stage selected.

Dear all:
I`m working on dynamic VF page which will show fields based on stages selected. Page works fine but the issue is with Probability field. While selecting Stage, Probability field does not show up value % associated at each level (like it generates for standard SF Opportunity page). Can one of you SF gurus please help in fixing this? Thanks!



 

<apex:page standardController="Opportunity" sidebar="false">


    <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"
                                                    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 == '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>

 
Alok Khandelwal - DamcoAlok Khandelwal - Damco
Hi Chetan,

You could follow the code in the sample below and implement it in your situation.
 
<apex:page standardController="Opportunity">
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js" />
<script src="/soap/ajax/15.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/15.0/apex.js" type="text/javascript"></script>
 
<script>
sforce.connection.sessionId = "{!$Api.Session_ID}";
 
function processChange(){
 
var obj = document.getElementById('{!$Component.frm.stageFieldId}');
var p = document.getElementById('{!$Component.frm.probabilityFieldId}');
 
var qr = sforce.connection.query("Select DefaultProbability From OpportunityStage where MasterLabel = '" + $(obj).val() + "'") ; 
records = qr.getArray("records");
 
for (var i=0; i< records.length; i++) {
var record = records[i];
$(p).val(record.DefaultProbability);
break;
}
return true;
}
</script>
 
<apex:form id="frm">
<apex:inputField value="{!Opportunity.StageName}" onchange="processChange()" id="stageFieldId" />
<apex:inputField value="{!Opportunity.Probability}" id="probabilityFieldId"/>
</apex:form>
</apex:page>

Regards,
Alok
Che SFDCChe SFDC
Hi Alok, thank you for your response. I tried updating code based on your suggestion of jquery but its not working. Below is the updated code. Can you please point out what I`m doing wrong?
 
<apex:page standardController="Opportunity" sidebar="false">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js" />
<script src="/soap/ajax/15.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/15.0/apex.js" type="text/javascript"></script>
 
<script>
sforce.connection.sessionId = "{!$Api.Session_ID}";
 
function processChange(){
 
var obj = document.getElementById('{!$Component.frm.stageFieldId}');
var p = document.getElementById('{!$Component.frm.probabilityFieldId}');
 
var qr = sforce.connection.query("Select DefaultProbability From OpportunityStage where MasterLabel = '" + $(obj).val() + "'") ; 
records = qr.getArray("records");
 
for (var i=0; i< records.length; i++) {
var record = records[i];
$(p).val(record.DefaultProbability);
break;
}
return true;
}
</script>


    <apex:sectionHeader title="Edit Opportunity" subtitle="{!opportunity.name}"/>
    <apex:form id="frm">
        <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}" onchange="processChange()" id="stageFieldId">
                                <apex:actionSupport event="onchange" rerender="thePageBlock"
                                                    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}" id="probabilityFieldId"/>

                </apex:pageBlockSection>
            </apex:actionRegion>
            <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>