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
Abhishek KedariAbhishek Kedari 

Passing custom object fields to controller through visualforce page

Hi All,

   I am new to salesforce developemnt.
   I want to know, how I can access fields from custom objects to my apex controller through visualforce page.
   To be more detail about my question - 
   - I have a custom object 'shipment' and some fields created for that object (Fileds are like dropdown, multiselect, long text etc)
   - I have visual force page and I am not able to show fields like drop down list with values defined in my custom object (values are predefined in object fields with type as picklist) Anyone knows how I can do that ?
   -  and after that I want to send those values to my controller from my visualforce page where I can write my actual business logic and process that data.

Let me know if I am not clear. Thanks is advance.

Best Regards,
Abhishek
AshlekhAshlekh
Hi,

In MVC stucture you have difined the a object called Shipment so you have difiend the Model Layer.

Now you need to show the value on page so you need a controller(your apex class) and page (view).

Suppose you have a object  "Shipement". In object schema you will find the API name of this object like "Shippment__C". And suppose you have dified two fields X and Y  and the api name wil be X__c and Y__c respectly.


Your apex class will  be

Class ShipmentController
{
   List<shipment__c> shipList{set;get;} // we use set and get if you want access this variable on page
   public ShipmentController(){
        shipList  = [select id,name ,x__c,Y__c from Shippment__C limit 10];
   };
}



your page code

<apex:page controller="ShipmentController" >
 <apex:form>
    <apex:pageBlock>
       <apex:pageBlockTable var="t" value="shipList">
       <apex:column value="t.name" headerValue="Name"/>
       <apex:column value="t.x__c" headerValue="X"/>
       <apex:column value="t.y__c" headerValue="Y"/>
    </apex:pageBlockTable>
  </apex:pageBlock>
</apex:form>
</apex:page>
This link will hepl you.

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_custom.htm

if it helps you then please mark as solution
AshlekhAshlekh
<apex:pageBlockTable var="t" value="{!shipList}">
       <apex:column value="{!t.name}" headerValue="Name"/>
       <apex:column value="{!t.x__c}" headerValue="X"/>
       <apex:column value="{!t.y__c}" headerValue="Y"/>
</apex:pageblocktable>
Please update above table with this table I forget to bind the field {!YOUR PROPERTY}
Abhishek KedariAbhishek Kedari
Thanks Ashlekh for your reply.
I was trying the code u pasted above. I am not getting data from anywhere as of now.
So my constructor is empty and not like the one you mentioned above. I just initialized the variables x and y to 0 value.
and when I hit on save button which I created on my visualforce page, I m calling save method from my controller. There I am just trying to print the value of x and y and I am getting error as :

10:41:12.037 (37287047)|EXCEPTION_THROWN|[14]|System.NullPointerException: Attempt to de-reference a null object
10:41:12.037 (37466224)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Any idea about this ?

Thanks,
Abhishek

Abhishek KedariAbhishek Kedari
Not complete code but important part from code :

public class Sushi {

public Sushi__c sushiObj{get;set;}

public Sushi() {
        sushiObj.Name__c = '';
    }
   
    public PageReference save() {
        //String strX = ApexPages.currentPage().getParameters().get('X');
        System.Debug('This is : ' + sushiObj.Name__c);
        return null;
    }
}


Visualforce :
<apex:page standardController="Sushi__c(**this is object api name**)" extensions="Sushi  (** this is controller name**)">
   :
   :
<apex:pageBlockSection title="Basic Information" columns="2">
                <apex:inputText label="Customized E-mail Text" value="{!sushiObj.Name__c}"/>
            </apex:pageBlockSection>
Abhishek KedariAbhishek Kedari
Got the issue. Issue was I was not actually creating new object with new operator.

Thanks,
Abhishek