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
Priyadarshini Manoharan 3Priyadarshini Manoharan 3 

How to fetch a picklist value selected in a VF page before the record is saved?

There is a picklist field on a VF page, after clicking on new button once I select a picklist value in the field, how do I fetch the value, before it is saved in the database. I need to use this fetched value to populate another field before the record is saved.
rohitsfdcrohitsfdc
Hello,
on button click, are you calling any method in the controller? if yes, you can write the logic there itself.

let me know if i didnt understand the requirement properly.

Thanks


 
Priyadarshini Manoharan 3Priyadarshini Manoharan 3
The standard new button is overriden using the VF page, in the VF page once I select a value from the picklist field, I want to capture that value before that record is saved, and I need to populate another field based on the value selected in the picklist, before the record is saved
rohitsfdcrohitsfdc
Are you using custom save method or the one with standard controller?
Priyadarshini Manoharan 3Priyadarshini Manoharan 3
I am using custom save method

Thanks
rohitsfdcrohitsfdc
Cant you use something like this 
 
if(picklist1=='value1')
piclist2 = 'value2';

 
Priyadarshini Manoharan 3Priyadarshini Manoharan 3
Sorry, I did'nt understand what you are trying to say. Can you please elaborate?

Thanks
rohitsfdcrohitsfdc
public pagereference save(){

if(objname.picklist1=='Value1')
objname.picklist2 = 'value2';

insert objname;
}
Priyadarshini Manoharan 3Priyadarshini Manoharan 3
the above code checks for a picklist value in the save menthod and sets another picklist value. 
My requirement is different, I have a field Issue Type with values Access, Data, Performance, while creating a new case if I select Access, I need to populate the description field with please provide login details etc. even before the record is saved by clicking submit case button, similarly for different values of Issue Type there are different values of description
rohitsfdcrohitsfdc
Got it. You need to use javascript for this kind of actions. 

check this blog. it may help you

http://www.cloudforce4u.com/2013/07/using-javascript-in-visualforce-page.html
Priyadarshini Manoharan 3Priyadarshini Manoharan 3
Thanks Rohit!
Let me try and get back, in case of any queries
Priyadarshini Manoharan 3Priyadarshini Manoharan 3

Hi Rohit,

I was able to get half way through. I was able to get the picklist value even before the record was saved through java script. I now need to set a value to Description field based on the value of the Product__c field fetched through java script. How can I achieve that? Below is the code

If Product is A, I want to set description as Please enter details for A

If Product is B, I want to set description as Enter Username, password etc. When issue occured etc...

If Product is C, I want to set description as Is performance slow? etc

Apex page

<apex:page standardController="Case" extensions="CaseStdControllerExt" title="Contact Us" showHeader="true" standardStylesheets="true" docType="html-5.0">


<apex:form id="frm">
    <apex:pageBlock title="Enter Case Details" rendered="true">
        <apex:pageBlockButtons >
        <apex:commandButton value="Submit Case" action="{!savecase}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Case Information" collapsible="false" columns="1">
        <apex:inputField value="{!Case.Product__c}"  id="inptID" onchange="MyjavaFunction('{!$Component.inptID}')" />
       
       

        <apex:inputField value="{!Case.Description}"  id="casedesc"/>
        </apex:PageBlockSection>
    </apex:pageBlock>
</apex:form>

 <!-- Java script starts Here -->
  <script>
   function MyjavaFunction(ReceiveInputID){
  
    var inputValue = document.getElementById(ReceiveInputID).value;
         if(inputValue == ''){
            alert('You did not eneter any value in input box');
         }
         else
         {
            alert(' You entered :: '+inputValue);
         }
           
           
   }
  </script>
</apex:page>

Standard Extension controller

public class CaseStdControllerExt {

private final Case webcase;

    public CaseStdControllerExt(ApexPages.StandardController controller) {
    
    webcase = (Case)controller.getRecord();

    }
    
     public PageReference savecase() 
     {
       

       upsert(webcase);
       PageReference p = new ApexPages.StandardController(webcase).view();
       p.setRedirect(true);
       return p;
       
     }

}