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
krishna casukhela 7krishna casukhela 7 

load picklist values from standard object

Hello friends
I am appending a contact ID to the browser url, based on the contact ID I will fetch lastname and leadsource into the vf page for that contact ID only.

so I am trying to bind leadsource picklist values into my controller as follows for that particular contact ID only.
public class clsContact {
    public Contact contact {get; set;}
    public String lastname{get; set;}
    public List<Selectoption> leadsourcevalues{get; set;}
    String contactId {get; set;} 
   
    public clsContact(){
        contactId = ApexPages.currentPage().getParameters().get('id');
        if(contactId != null)
        {
           getContact();
        }
        else{
            contact = new Contact();
        }
 }
public void getContact() {
            contact = [SELECT Id, lastname,LeadSourcee FROM Contact WHERE Id = :contactId];
 }

<apex:page controller="clsContact" >
<apex:form >
 <apex:inputField value="{!contact.FirstName}"/>
 //Here how to get the picklist value for that particular ID
  <apex:commandButton value="Save" action="{!save}"/>
</apex:form>
</apex:page>
I am struggling here so kindly help me out.
Thanks
krishna


 
LBKLBK
Isn't supposed to be as simple as using an <apex:inputField> for that?
<apex:inputField value="{!contact.LeadSource}"/>
Or, is there something I am missing here?
 
krishna casukhela 7krishna casukhela 7
Hi LBK
I hv used  <apex:inputField value="{!Contact.LeadSource}"/>
so first time when I pass the ID in URL it works.
second time the previous value in picklist is retained and it throws an error : attempt to deference a null object

thanks
krishna
LBKLBK
Can you post your code here? Both the controller and VF page.
krishna casukhela 7krishna casukhela 7
HI LBK
I have given code in my previous post. The contrtoller code is the same , a small change in the vf page as follows.
<apex:page controller="clsContact" >
    <apex:form >
               <apex:inputText value="{!Contact.LastName}"/>                       
            <apex:inputField value="{!Contact.LeadSource}"/>
    </apex:form>
</apex:page>

thanks
krishna
LBKLBK
When you said "second time" in your previous response, what did you mean?

Is it something to do with your Save functionality? Are you redirecting back to the same page?

if yes, what is the PageReference are you using?
krishna casukhela 7krishna casukhela 7
Hi LBK
Initially when the page loads I am appending the URL with an ID. so it works.
Now without refreshing the page I append the URL with another ID then I get the error as :attempt to deference a null object"

also if I close the vf page and then open again with a new ID then I get the same error.

In this functionality I am not using any SAVE functionality or redirecting, its just that I when I append the ID to url it should fetch field values
for that ID, again if append a new ID then it should fetch for that ID and so on.
I hope  am clear.

thanks
krishna

 
krishna casukhela 7krishna casukhela 7
Hi LBK
the url is : https://krishnac-dev-ed--c.ap1.visual.force.com/apex/clscontact?id=0039000001OdrcA
here I am using a custom domain .
so now when I append another ID it fails
please help me out.

thanks
krishna
LBKLBK
How are you appending the new ID?

Are you not using PageReference object to do so?