+ Start a Discussion
Abhiman MungalAbhiman Mungal 

how to get the value from object through VF Pages

<apex:page standardController="Position__c" extensions="ParamBlogController">
 <apex:outputPanel id="all">
  <apex:form >
    <apex:pageBlock title="Account Detail">
      <apex:pageBlockSection title="Account">
            
             <apex:commandButton action="{!setupContacts}" value="try"/>
            <apex:inputField value="{!Position__c.Position_Type__c}"/>
     
      </apex:pageBlockSection>
      <apex:pageBlockSection title="Contacts" columns="1">
        <apex:pageBlockTable value="{!conts}" var="cont" rows="10">
          <apex:column value="{!cont.Id}"/>
          <apex:column value="{!cont.Name}"/>
           <apex:column value="{!cont.city_state__c}"/>
          <apex:column value="{!cont.Position_Type__c}"/>
          
      
        </apex:pageBlockTable>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
 </apex:outputPanel>
</apex:page>


public class ParamBlogController 
{
 private ApexPages.StandardController stdCtrl {get; set;}
  
 public List<position__c> conts {get; set;}
  
 public ParamBlogController(ApexPages.StandardController std)
 {
  stdCtrl=std;
 
 }
  
 public void setupContacts()
 {
  conts=[select Name,city_state__c,Department__c,Position_Id__c,Position_Type__c from position__c where Position_Type__c=:stdCtrl.getId()];
 }
}

when i try to get the all records from position related records to position_types__c 
Best Answer chosen by Abhiman Mungal
Sohan Raj GuptaSohan Raj Gupta
Hi Abhiman,

In this case use below code: 
 
public class ParamBlogController 
{
 private ApexPages.StandardController stdCtrl {get; set;}
  
 public List<position__c> conts {get; set;}
 public position__c pos {get;set;}
 
 public ParamBlogController(ApexPages.StandardController std)
 {
  stdCtrl=std;
  this.pos = (position__c)std.getRecord(); 
 }
  
 public void setupContacts()
 {
  conts=[select Name,city_state__c,Department__c,Position_Id__c,Position_Type__c from position__c where Position_Type__c=:pos.Position_Type__c and Location_Name__c=pos.Location_Name__c ];
 }
}

Thanks,
Sohan Raj Gupta

All Answers

Vinod ChoudharyVinod Choudhary
Hi Abhiman,

Try following.

Your Class:
public with sharing class ParamBlogController 
{
public List<position__c> conts {get; set;}
 
 public ParamBlogController(){
     setupContacts();
 }
 
 public void setupContacts()
 {
   conts=[select Name,city_state__c,Department__c,Position_Id__c,Position_Type__c from position__c where Position_Type__c=:stdCtrl.getId()];
 }
}

VF Page:
 
<apex:page controller="ParamBlogController">
 <apex:outputPanel id="all">
  <apex:form >
    <apex:pageBlock title="Account Detail">
      <apex:pageBlockSection title="Account">
            
           
     
      </apex:pageBlockSection>
      <apex:pageBlockSection title="Contacts" columns="1">
        <apex:pageBlockTable value="{!conts}" var="cont" rows="10">
          <apex:column value="{!cont.Id}"/>
          <apex:column value="{!cont.Name}"/>
           <apex:column value="{!cont.city_state__c}"/>
          <apex:column value="{!cont.Position_Type__c}"/>
          
      
        </apex:pageBlockTable>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
 </apex:outputPanel>
</apex:page>
Other changes you can make according to your requirements.

Thanks
Vinod 
 
Abhiman MungalAbhiman Mungal
its not working properly. what exact u mean to say?
i try it but its not work
 
Vinod ChoudharyVinod Choudhary

ThanksOnce you create a list at your Class by querying the object you just need your list at VF page like following 
<apex:pageBlockTable value="{!conts}" var="cont>

          <apex:column value="{!cont.Id}"/>

          <apex:column value="{!cont.Name}"/>

           <apex:column value="{!cont.city_state__c}"/>

          <apex:column value="{!cont.Position_Type__c}"/>
      

        </apex:pageBlockTable>
First you need to create a correct list at your Class.  This code is working for me.

you can debug your class data using system.debug(), and check is your list return the correct data or not in debug log.

anyway... what Error you are getting when you use my code ?

 
Sohan Raj GuptaSohan Raj Gupta
Hi Abhiman,

use below class code: 
public class ParamBlogController 
{
 private ApexPages.StandardController stdCtrl {get; set;}
  
 public List<position__c> conts {get; set;}
 public position__c pos {get;set;}
 
 public ParamBlogController(ApexPages.StandardController std)
 {
  stdCtrl=std;
  this.pos = (position__c)std.getRecord();

 
 }
  
 public void setupContacts()
 {
  conts=[select Name,city_state__c,Department__c,Position_Id__c,Position_Type__c from position__c where Position_Type__c=:pos.Position_Type__c];
 }
}

Please let me know if it helped or you need any more assistance.

Please mark this is as the solution if it solved your purpose.

Thank You,
Sohan Raj Gupta
Abhiman MungalAbhiman Mungal
thanks sohan..
its work fine
 
Abhiman MungalAbhiman Mungal
what happen if i have two lookups in one apex and VF pages
like this....

 <apex:column value="{!cont.Position_Type__c}"/>
 <apex:column value="{!cont.Location_Name__c}"/>


public class ParamBlogController 
{
 private ApexPages.StandardController stdCtrl {get; set;}
 
 private ApexPages.StandardController stdCtrl1 {get; set;}
  
  
 public List<position__c> conts {get; set;}
 public position__c pos {get;set;}
 
 public ParamBlogController(ApexPages.StandardController std,loc)
 {
  stdCtrl=std;
  stdCtrl1 =loc;
  this.pos = (position__c)std.getRecord();
  this.pos=(position__c)loc.getRecord();

 
 }
  
 public void setupContacts()
 {
  conts=[select Name,city_state__c,Department__c,Position_Id__c,Position_Type__c from position__c where Position_Type__c=:pos.Position_Type__c and Location_Name__c=pos.Location_Name__c ];
 }
}
Sohan Raj GuptaSohan Raj Gupta
Hi Abhiman,

In this case use below code: 
 
public class ParamBlogController 
{
 private ApexPages.StandardController stdCtrl {get; set;}
  
 public List<position__c> conts {get; set;}
 public position__c pos {get;set;}
 
 public ParamBlogController(ApexPages.StandardController std)
 {
  stdCtrl=std;
  this.pos = (position__c)std.getRecord(); 
 }
  
 public void setupContacts()
 {
  conts=[select Name,city_state__c,Department__c,Position_Id__c,Position_Type__c from position__c where Position_Type__c=:pos.Position_Type__c and Location_Name__c=pos.Location_Name__c ];
 }
}

Thanks,
Sohan Raj Gupta
This was selected as the best answer
Abhiman MungalAbhiman Mungal
Hi sohan,
I need one another help could you please tell me how to insert Position__c records through apex class with master detail values
Sohan Raj GuptaSohan Raj Gupta
Hi Abhiman,

Please explain your requirment in detail, so I can help you.

Thanks,
Sohan Raj Gupta
Abhiman MungalAbhiman Mungal
<apex:page standardController="Position__c" extensions="NextPage">

<apex:pageBlock >
<apex:form >
      
   
 <apex:pageBlockSection title="Position" >
    
       <apex:inputField value="{!Position__c.Name}"/>
        <apex:inputField value="{!Position__c.Position_Id__c}"/>
        <apex:inputField value="{!Position__c.Department__c}"/>
        <apex:inputField value="{!Position__c.city_state__c}"/>
        <apex:inputField value="{!Position__c.Position_Type__c}"/>
        <apex:inputField value="{!Position__c.Location_Name__c}"/>
              
              <apex:commandButton value="Save" action="{!save}"/>
    
 </apex:pageBlockSection>
    
      </apex:form>

</apex:pageBlock>
</apex:page>

in my position object i have this much fields and i want to insert records through the VF pages using Apex class.
in my position object i have two lookups fields one is positon_type__c and another is Location_Name__c when i want to insert records in position that time it will give me an error .
so what can i do....
 
Abhiman MungalAbhiman Mungal
public class NextPage{
     private ApexPages.StandardController stdCtrl {get; set;}
     public List<position__c> conts {get; set;}
     public Position__c pos{get;set;}
    
    public NextPage(ApexPages.StandardController std) {
          stdCtrl=std;
          this.pos = (position__c)std.getRecord();
         }    
      
      public string position_type{get;set;}
      
 public String save() 
 {   
       Position__c newObj = new Position__c(); 
       newObj .position_type__c= [SELECT id FROM position_types__c WHERE Name =:pos.Position_Type__c LIMIT 1].id;
       insert newObj ;
       return 'success';
 }
 }

And this is my Apex class code
Sohan Raj GuptaSohan Raj Gupta
Hi Abhiman,

If positon_type__c is a lookup filed then it will give record ID apex class, so no need to get again ID in save function.

In save function you again create a new instance of Postion object and insert that. You should try below code:
 
public String save() 
 {   
       insert pos;
       return 'success';
 }
Try above code and if will face again issue then let me know.

Thanks,
Sohan Raj Gupta