0

I am working on a custom VF page for a case screen with extension controller class in use.

<apex:actionRegion >
  <apex:inputfield label="Action" value="{!case.Action__c}" id="Action" >
     <apex:actionSupport event="onchange" rerender="ProdFamilyModel" immediate="false" /> 
  </apex:inputfield> 
</apex:actionRegion>

I want the selected value from the input field assigned to a string variable in my extension class. I have searched a lot and was unable to get an answer.

Suppose in my dropdown there are two values, say "Apple","Orange". So if i select Apple, i want this name to be stores in a string variable.

3

I am extending @battery.cord's answer with an example as follows:

Visualforce

<apex:actionRegion >
  <apex:inputfield label="Action" value="{!caseObj.Action__c}" id="Action" >
     <apex:actionSupport event="onchange" rerender="ProdFamilyModel" action="{!passValueToController}" /> 
  </apex:inputfield> 
</apex:actionRegion>

Controller

public class ActionSupportController
{
    public String fieldValue {get; set;}
    public Case caseObj {get;set;}
    public void passValueToController()   
    {        
          fieldValue = caseObj.Action__c; //this will assign the selected value to Controller variable.
    }
}
| improve this answer | |
  • Actually i tried this method, but got an error as Illegal assignment from Schema.SObjectField to String when Action_c is assigned to a string variable. Any thoughts on this? – saugandh k Apr 11 '18 at 16:20
  • Do you have a variable named Case? If not, Salesforce will think you're referencing the type Case, and provide the field schema for Action__c instead of a field value. – battery.cord Apr 11 '18 at 16:50
  • updates my answer, with proper variable caseObj – Santanu Boral Apr 11 '18 at 16:55
  • @SantanuBoral After researching with your code i was successfully able to assign the selected value to the string variable. Thank you for leading me to the right direction. – saugandh k Apr 12 '18 at 10:11
  • You should select one best answer since your problem got resolved – Santanu Boral Apr 12 '18 at 12:31
3

You put the variable you want to store this value in as your value="{!SomeString}" attribute.

You'll need to pass the value over to the record somewhere, usually in a save method, or in whatever action your actionSupport is calling.

You could even add a method to set a variable and call it with your actionSupport.


Seems like what you need is a reference to your case object inside your extension class.

I've prepared a demo page & class to show some of the methods of saving references to various objects.

Examples (Noted with Comments)

  1. Save the controller reference. By keeping the variable you take as a parameter for your extension, you can call other methods, such as getRecord() to obtain information you don't have direct access too. This lets you call save() as well.
  2. Keep a reference to the record used on the page. In your constructor, you use getRecord() to keep a reference saved of the object. Any updates should be carried over to your reference, allowing you to access field values & preform actions to the sObject. You can also use this object instead of the object provided by the standard controller, but this isn't always advisable.
  3. Use a variable to keep track of input values. Saving the input to a variable allows you to do conditional validation or rerendering without accessing your records fields, until you need to save the record. You'll probably still need a reference to the standard controller to modify & save the record.

Demo Class

public class DemoClass {

    // Example #1 
    public ApexPages.StandardController controller { get; set; }

    // Example #2 
    public Account Record { get; set; }

    // Example #3 
    public String Value { get; set; }

    public DemoClass(ApexPages.StandardController controller) {
        this.controller = controller; // Save controller reference to access record 
        this.Record = (Account)this.controller.getRecord(); // Save reference to actual record to manipulate fields in controller 
    }

    public PageReference CustomSave() {
        // Keep only a value saved, and carry this value over to the record on save 
        // Still requires a controller reference
        if (Value != null) {
            controller.getRecord().put('Name', Value);
        }

        return controller.Save(); 
    }

}

Demo Page

<apex:page standardController="Account" extensions="DemoClass">

    <apex:Form>

        <!-- Example #1 --> 
        <apex:inputField value="{!Account.Name}" />

        <!-- Example #2 --> 
        <apex:inputText value="{!Record.Name}" />

        <!-- Example #3 --> 
        <apex:inputText value="{!Value}" />

        <apex:commandButton action="{!CustomSave}" value="Custom Save" />

    </apex:Form>
</apex:page>

Based on the code posted, I think the easist thing to do would be to keep a reference to the standard controller in your code, and add a string variable with a getter which fetches your value.

Like so:

public class Demo {

    public ApexPages.StandardController controller { get; set; }

    public String Action {
        get {
            return (String)controller.getRecord().get('Action__c');
        }
    }

    public DemoClass(ApexPages.StandardController controller) {
        this.controller = controller; // Save controller reference to access record 
    }
}

Then, wherever you need the value, you can use {!Action}. That being said, you could also just directly access the field value if you're trying to put a value onto a page ({!Case.Action__c})

| improve this answer | |
  • The 'value' attribute is already present as you can see in the code snippet in the question which is used to load the values of the Action__c picklist. Where else can i give a 'value' attribute to get the selected value? – saugandh k Apr 11 '18 at 16:25
  • I'm saying you can use a value that isn't a case field for your picklist, and then carry the value over to the record when you save. Could also use the getter of a variable to pull the value from the case record & access it. Something like get { return Case.Action__c; } – battery.cord Apr 11 '18 at 16:53
  • From your other comments, it seems like you're writing an extension to a page which is using the standard case controller, and don't have a reference to the case variable itself, which complicates things slightly. Can you edit your question to include the rest of your controller, or at least however much is relevant? Seeing some of your other variables would be helpful. – battery.cord Apr 11 '18 at 16:55
  • Thank you for your detailed answers. At last i am able to get it done. – saugandh k Apr 12 '18 at 10:15
0

I was able to figure out how to get the selected value from inputfield dropdown with the help of @battery.cord and @Santanu Boral.

This answer might help others who are stuck in the same problem.

VF Page

<apex:page standardController="Case" extensions="Inputfld">
 <apex:form >
<apex:pageBlock title="Case Edit" mode="edit">

        <apex:pageBlockSection title="Action" columns="2" collapsible="false" >

            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Action" for="Action"/>
               <apex:actionRegion >
                    <apex:inputfield label="Action" value="{!Case.Action__c}" id="Action" >
                        <apex:actionSupport event="onchange" rerender="ProdFamilyModel" immediate="false" action="{!passValueToController}"/> 
                    </apex:inputfield>  
               </apex:actionRegion>  
            </apex:pageBlockSectionItem>

            <apex:pageBlockSectionItem >
                <apex:outputPanel id="ProdFamilyModel" style="margin-top:0px;">
                    <apex:pageBlockSection rendered="{!IF(case.Action__c == 'Password Reset', true, false)}">
                        <apex:inputField id="Password"  value="{!case.Password__c}"  rendered="{!IF(case.Action__c == 'Password Reset', true, false)}"/>   
                    </apex:pageBlockSection>
                    <apex:pageBlockSection rendered="{!IF(case.Action__c == 'Pin Reset', true, false)}">
                        <apex:inputField id="Pin" value="{!case.Pin__c}" rendered="{!IF(case.Action__c == 'Pin Reset', true, false)}" />
                    </apex:pageBlockSection>
                    <apex:pageBlockSection rendered="{!IF(case.Action__c == 'Replacement Card', true, false)}">
                        <apex:inputField value="{!case.Replacement_Card__c}" />
                    </apex:pageBlockSection>
                </apex:outputPanel>
            </apex:pageBlockSectionItem>


        </apex:pageBlockSection>

     </apex:pageBlock>
</apex:form>

Apex Class

public class Inputfld {
 Case thisCase;
public String selectedAction{get;set;}
public String fieldValue {get; set;}

 public Inputfld(ApexPages.StandardController stdController)
 {
    thisCase = (Case)stdController.getRecord();
    System.debug('thisCase.Action__c====='+thisCase.Action__c);
}
public void passValueToController()   
{        
    fieldValue = thisCase.Action__c; 
    System.debug('fieldValue ====='+fieldValue);// This will provide the selected value form the action dropdown.
}

}

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.