+ Start a Discussion
Priyadarshini Manoharan 3Priyadarshini Manoharan 3 

How to pass a javascript variable from apex page to standard controller?

Here's the code, inputValue from apex page should be assigned to webcase.Product__c

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: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"/>

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

Standard Extension controller

public class CaseStdControllerExt {

private final Case webcase;

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

     public PageReference savecase() 

       PageReference p = new ApexPages.StandardController(webcase).view();
       return p;


NagaNaga (Salesforce Developers) 
Hi Priyadarshini,

Please see the sample code below

User-added imagePlease see the link below for more information


Best Regards
Naga Kiran
Priyadarshini Manoharan 3Priyadarshini Manoharan 3
Hi Naga,

This seems quite useful. Thanks. Let me try and get back.