+ Start a Discussion
Tyler ZikaTyler Zika 

Use standard controller and custom controller together

I have a VF page with a custom controller. It has a sign up form to create a new contact. When I tried adding a picklist field for the MailingStateCode field, I was unsure how to get the prepopulated values. Do I make a setter and getter for it? If so how? If not, what shall I do?
Best Answer chosen by Tyler Zika
Rohit Sharma 66Rohit Sharma 66
Use following code for VF Page
<apex:page controller="CommunitiesSSignUpController">
    <apex:form >
        <apex:selectList value="{!StateList}" size="1" styleclass="form-control">
            <apex:selectOptions value="{!StateCodes}"/>
        </apex:selectList>
    </apex:form>
</apex:page>
Apex Class:
public with sharing class CommunitiesSSignUpController {
    
    public String StateList { get; set; }
    
    public List<SelectOption> getStateCodes(){
    
		List<SelectOption> options = new List<SelectOption>();
			
		Schema.DescribeFieldResult fieldResult = Contact.MailingStateCode.getDescribe();
		List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
			
		for( Schema.PicklistEntry f : ple){
		  options.add(new SelectOption(f.getLabel(), f.getValue()));
		}  
		system.debug('*********'+options);
		 return options;
	}

}
It will work for sure.

Let me know if it really does :)


:

All Answers

GauravGargGauravGarg

Hi Tyler,

It would be a better option to initialize field in constructor rather than creating a getter setter for this.

Thanks,

Gaurav

VineetKumarVineetKumar
Salesforce allows the use of only one controller for a page, either standard or a custom.
But you can implment your above logic by use of extension classes (a page can have multiple extensions)
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_extension.htm

Let me know if you need any more help.
 
Rohit Sharma 66Rohit Sharma 66
Hi Tyler,

If you are using the custom controller, then just create a instansce of the contact object and use that in the VF page. Below ids the sample code:
 
public with sharing class contactController {
    
    public Contact conRecord{get;set;}
    
    public contactController(){
        conRecord = new Contact();
    }
}
Now u can access any of your contact object fields in your VF page like {!conRecord.Fieldname}

Please let me know if this works for you.
 
Tyler ZikaTyler Zika
This is what I have to access the State Codes for the picklist
<apex:inputField value="{!conRecord.MailingStateCode}"/>

And my controller
public with sharing class CommunitiesSSignUpController {

    public Contact conRecord{get;set;}

    public CommunitiesSSignUpController() {
        conRecord = new Contact();
        RegUserSuccessed=false;
    }

}

It's giving me an error.
GauravGargGauravGarg

Hi Tyler,

Can you please post the error message.

Thanks,

Gaurav

Rohit Sharma 66Rohit Sharma 66
Hi Tyler,

There is no such Standard field MailingStateCode exist for the Contact Object. The standard field is MailingState. If you are using any custom filed then use it like MailingStateCode__c.

Please try and let me know if it helps.
GauravGargGauravGarg
Hi Tyler,

Below is the list of all standard field on Contact object. MailingStateCode is a valid field. Please post the error message so that we can further help you out on this.

Thanks,
Gaurav
Tyler ZikaTyler Zika

I don't get a specific error, it just said Error: Visualforce page couldn't load.


I spoke with a developer I work with. I guess the customer user in our system can't edit a Contact record, and he recommend doing this.

Controller

public with sharing class CommunitiesSSignUpController {
    public String StateCodes { get; set; }
   
       public List<SelectOption> getStateCodes() {
           List<SelectOption> results = new List<SelectOption>();
           Schema.DescribeFieldResult functions = Contact.MailingStateCode.getDescribe();
       
          for(Schema.PicklistEntry entry : functions.getPicklistValues()) {
            results.add(new SelectOption(entry.getValue(), entry.getValue()));
          }

         return results;
        }
}
 

Visualforce page

<apex:selectList value="{!StateCodes}" size="1" styleclass="form-control">
    <apex:selectOptions value="{!StateCodes}">
</apex:selectOptions></apex:selectList>
 

Now I don't get an error, and the picklist displays, but it has no values.

 

Rohit Sharma 66Rohit Sharma 66
Try following code, I tested in Developer Console and seems like working for me.
 
public List<SelectOption> getCountries()
{
  List<SelectOption> options = new List<SelectOption>();
        
   Schema.DescribeFieldResult fieldResult = Contact.MailingStateCode.getDescribe();
   List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        
   for( Schema.PicklistEntry f : ple)
   {
      options.add(new SelectOption(f.getLabel(), f.getValue()));
   }  
	system.debug('*********'+options);
        return options;
}

Let me know if it works for you.
Tyler ZikaTyler Zika
Rohit, interestingly enough, when I comment out my
public List<SelectOption> StateCodes { get; set; }

The states appear in my picklist for the form. But now my visualforce page is getting an error of

"Error: Read only property 'CommunitiesSSignUpController.StateCodes'"

So the state codes do not appear when there is a getter/setter
Rohit Sharma 66Rohit Sharma 66
Use following code for VF Page
<apex:page controller="CommunitiesSSignUpController">
    <apex:form >
        <apex:selectList value="{!StateList}" size="1" styleclass="form-control">
            <apex:selectOptions value="{!StateCodes}"/>
        </apex:selectList>
    </apex:form>
</apex:page>
Apex Class:
public with sharing class CommunitiesSSignUpController {
    
    public String StateList { get; set; }
    
    public List<SelectOption> getStateCodes(){
    
		List<SelectOption> options = new List<SelectOption>();
			
		Schema.DescribeFieldResult fieldResult = Contact.MailingStateCode.getDescribe();
		List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
			
		for( Schema.PicklistEntry f : ple){
		  options.add(new SelectOption(f.getLabel(), f.getValue()));
		}  
		system.debug('*********'+options);
		 return options;
	}

}
It will work for sure.

Let me know if it really does :)


:
This was selected as the best answer
Tyler ZikaTyler Zika
That it! Thank you.