1

I am having a Custom Object, which has a field Country__c. (Picklist).

When I am clicking the Edit page, it is going to a Custom Visualforce page. But, there in that page, Country__c is showing blank, as if it didn’t remember the country that was chosen.

I've written Extension Controller for this particular object.

Any solution for this?

Below is my Constructor for Extension Class

Apex Code

public class MyGroupConfigControllerExt {

    public final My_Config__c mpgcObj {get; private set;} 
    ApexPages.StandardController stdController;
    public list<SelectOption> specDiscountPicklist {get; private set;}
    public String selectedDiscount {get; set;}

    public MyGroupConfigControllerExt(ApexPages.StandardController Controller) {
        stdController = Controller;

        //we cannot use addFields within test methods, so we have to wrap it in a Test.isRunningTest()
        if (!Test.isRunningTest()) {
            stdController.addFields(new List<String> {
                'country__c'
            });
        }

        this.mpgcObj = (My_Config__c)stdController.getRecord();
        // Here I have to set mbgObj.Country__c, but how???

        mpgcObj.Country__c = ?;



    }  
6
  • Please post your code. The short answer is that you would need to set the Country on the field of the bound sObject in the constructor on page load.
    – greenstork
    Commented Jun 17, 2014 at 19:41
  • @greenstork : Constructor code posted in the main question section Commented Jun 17, 2014 at 19:51
  • @greenstork : any example, how to use binding here Commented Jun 17, 2014 at 20:17
  • 1
    You should add the apex:inputField or whatever other way you are presenting the field in the Visualforce to the question too. By the way, your addFields should result in the country being automatically queried for you by the standard controller.
    – Keith C
    Commented Jun 17, 2014 at 20:22
  • 1
    It looks like you are binding the field to selectedDiscount which will never be read or written. (Setting id="country" is immaterial.) If you want the standard controller to take care of the reading and writing you will have to bind a field that is on the SObject e.g. mpgcObj.Country__c to your input field.
    – Keith C
    Commented Jun 17, 2014 at 20:34

1 Answer 1

2

You are displaying the string selectedDiscount on your Visualforce page, so that is what you need to set in your controller. If you are also using the visualforce page for new records and you want to have a default value for country you have to account for that as well. The following is what I have done to handle this previously:

public class MyGroupConfigControllerExt {

public final My_Config__c mpgcObj {get; private set;} 
ApexPages.StandardController stdController;
public list<SelectOption> specDiscountPicklist {get; private set;}
public String selectedDiscount {get; set;}

public MyGroupConfigControllerExt(ApexPages.StandardController Controller) {
    stdController = Controller;

    //we cannot use addFields within test methods, so we have to wrap it in a Test.isRunningTest()
    if (!Test.isRunningTest()) {
        stdController.addFields(new List<String> {
            'country__c'
        });
    }

    this.mpgcObj = (My_Config__c)stdController.getRecord();

    if(mpgcObj.Country__c != null){
       selectedDiscount = mbgObj.Country__c;
    } else {
        selectedDiscount = 'Default Value';
    }
}  

You will also want to make sure to override the save method, and make sure that you are saving the selectedDiscount into Country__c. See a previous answer I posted to my own question here for details: Dynamically display fields based on Picklist for create page

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .