1

I am trying to create a custom tab just like Accounts Tab.

I was able to fetch the detail on my custom tab.but I donot have any idea how i need to get the related list like contact which is related to the record i am opening

My apex code is

<apex:page controller="ClassRetrieve" >
<apex:panelGrid columns="2">
<apex:form >
  <apex:commandButton value="New Account" action="{!NewAccount}"/>
  <p></p> 
<apex:pageBlock >
  <apex:pageBlockTable value="{!accs}" var="a">
    <apex:column headerValue="Name" >

                        <apex:commandLink value="{!a.name}" action="{!accountClicked}">
                          <apex:param name="nickName"
                                value="{!a.id}"
                            assignTo="{!recordId}"/>
                        </apex:commandLink>
      </apex:column>
   </apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:panelGrid>
</apex:page>

My controller

public class ClassRetrieve {

Public string recordId{get;set;}
public List<Account> contactsInformation { get; set; }
public list<Account> accs {get;set;}

public ClassRetrieve()
{
    accs = [Select Id,Name From Account];
}

public pagereference accountClicked() 
{
    pagereference redirect = new PageReference('/apex/recordPage?id='+recordId);
    redirect.setRedirect(true);
    return redirect;
}

Public pagereference NewAccount()
{
    pagereference newAccpage = new pagereference('/apex/newAccount');
    newaccpage.setredirect(true);
    return newaccpage;
}
}

Page that displays the record is like

VF page

<apex:page controller="recordPageCont">  
create an custom email Field on account
  add edit / cancel buton <br/>
  add realted list using repeter eg Contacts using soql query
  <apex:form >
  <apex:pageBlock >
  <apex:pageBlockButtons >
       <apex:commandButton value="Edit" action="{!EditAccount}"/>
       <apex:commandButton value="Cancel" action="{!CancelButton}"/>
  </apex:pageBlockButtons>
  <apex:pageBlockSection >
     <apex:outputField value="{!objAccount.Name}"/>
     <apex:outputField value="{!objAccount.AccountNumber} "/>
     <apex:outputField value="{!objAccount.Type} "/>
     <apex:outputField value="{!objAccount.industry} "/>
     <apex:outputField value="{!objAccount.Rating} "/>
     <b>Email</b> {!objAccount.Name}@salesforce.com
  </apex:pageBlockSection>
  </apex:pageBlock>
  </apex:form>
</apex:page>

apex is

public class recordPageCont {
public Account objAccount{get;set;}
Public string recId{get;set;}

    public recordPageCont ()
    {
         recId = apexpages.currentpage().getparameters().get('id');
        objAccount =[SELECT Name, AccountNumber ,Type , Industry , Rating FROM account
                        WHERE ID = :recId ];
    }

    Public pagereference Editaccount()
    {
        Pagereference edtaccount = new Pagereference('/apex/editaccount?id='+recid);
        edtaccount.setredirect(true);
        return edtaccount;
    }

    public pagereference cancelButton()
    {
        Pagereference cnclbutton = new Pagereference('/apex/accountdisplay');
        cnclbutton.setredirect(true);
        return cnclbutton;
    }


}

please help..Any idea is welcome :)

  • You don't need custom controllers for any of this. You should read up on Standard Controllers and the apex:relatedList component. None of the above requires apex. – Adrian Larson May 20 '15 at 15:52
  • Also make sure to add the inline edit functionality that will be an attracting features for your User group. <apex:inlineEditSupport showOnEdit="saveButton" event="ondblclick"/> – DJD May 20 '15 at 18:01
  • I want to create a related lisk with custom code @adrian Larson – Ankita Singh May 21 '15 at 8:11
  • Don't use Apex where you don't have to. Unless you have strong reasons not to, generally err on the side of using standard functionality. @AnkitaSingh – Adrian Larson May 21 '15 at 14:47
2

You can fetch related contacts within same query in "recordPageCont" class, the query can be modified to:-

    objAccount =[SELECT Name, AccountNumber ,Type , Industry , Rating, 
(select firstname, lastname from Contact) FROM account
                            WHERE ID = :recId ];

Also, I would suggest referring to other components provided by Salesforce

  1. apex:relatedlist
  2. apex:enhancedList

To understand more about relationship queries refer to:-

  • I am getting an error for above query " Didn't understand relationship 'Contact' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 8 column 21" – Ankita Singh May 21 '15 at 8:12
  • Can you check if your profile (being used for testing) has appropriate access to Contact object? – Anshul May 21 '15 at 15:20
  • objAccount =[SELECT Name, AccountNumber ,Type , Industry , Rating, (select firstname, lastname from Contacts) FROM account WHERE ID = :recId ]; Please try the above query. The child relationship is Contacts instead of Contact – Vigneshwaran G Jun 4 '15 at 10:13
2

Sans Apex:

<apex:page standardController="Account">
    <apex:relatedList list="Contacts" />
</apex:page>
  • @Sans Apex - pls let me know where to add this – Ankita Singh May 21 '15 at 8:12
  • This would be your whole VisualForce Page. Because it has an Account standard controller, you can add it as a button on Account or to the Page Layout. – Adrian Larson May 21 '15 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.