+ Start a Discussion
Naveen DhanarajNaveen Dhanaraj 

Create a class and Visualforce page to display two custom object records in single VF( ObjectA__c and ObjectB__c)

I have two objects (  ObjectA__c and ObjectB__c) and i have some record for each object .Those records should be displayed in single VF page .once if i click the record name(A) it should redirect to the record (A) details page
ManojSankaranManojSankaran
Hi Naveen,

Below is my understanding on your requirement.

You want to display the list of two objects (A and B ) in a same page and on click of that you want to display the record detail in a separate screen. Let me know if my understanding is correct. i will share you the code.


Thanks
Manoj S





 
BALAJI CHBALAJI CH
Hi Naveen,

Please find below Sample Class and Visualforce Page. Here I am displaying Account Records and Contact Records provind link for names. When clicked on the record name, it will redirect to respective detail page.

VF Page:
<apex:page Controller="MyController">
    <apex:form >
        <apex:pageBlock>            
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!Arecords}" var="temp" >
                    <apex:column headerValue="Account Name" >
                    	<apex:commandLink value="{!temp.name}"  action="{!method1}" >
                        	<apex:param name="id" value="{!temp.id}" assignTo="{!aid}" />
                       	</apex:commandLink>
                    </apex:column>
                    <apex:column value="{!temp.id}" />
                    <apex:column value="{!temp.phone}" />
                    <apex:column value="{!temp.billingcity}" />
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
        
        <apex:pageBlock>            
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!Brecords}" var="temp" >
                    <apex:column headerValue="Contact Name" >
                    	<apex:commandLink value="{!temp.name}"  action="{!method1}" >
                        	<apex:param name="id" value="{!temp.id}" assignTo="{!aid}" />
                       	</apex:commandLink>
                    </apex:column>
                    <apex:column value="{!temp.id}" />
                    <apex:column value="{!temp.phone}" />
                    <apex:column value="{!temp.email}" />
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Cotroller:
public with sharing class MyController {

    public string aid{set;get;}
    public List<Account> getArecords()
    {
        return [select id, name, billingcity, phone from Account limit 100];
    }
    
    public pagereference method1()
    {
        pagereference pr = new PageReference('/' + aid);
        return pr;  
    }  
    
    public List<Contact> getBrecords()
    {
        return [select id, name, phone, email from Contact limit 100];
    }
}

Let us know if that helps you.

Best Regards,
BALAJI