Skip to main content
I am attempting to create a custom link on the Account for my users to generate a PDF that will pull details from all Contacts associated to that Account.

The code below is pulling in all Accounts and Contacts on a PDF, even with the URL specifying a specific Account ID.

I'm still quite new to Visualforce and could use some guidance on how to adjust this code to call only the Contacts associated with the Account ID referenced. Thank you.

 

<apex:page standardController="Account" recordSetVar="Acct" renderAs="PDF" applyBodyTag="false">

<apex:pageBlock>

<table>

<apex:repeat value="{!Acct}" var="acc">

<tr>

<td><apex:outputText value="{!acc.Name}"/></td>

<apex:repeat value="{!acc.Contacts}" var="cont">

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

</apex:repeat>

</tr>

</apex:repeat>

</table>

</apex:pageBlock>

</apex:page>

 
1 réponse
  1. 22 janv. 2020, 17:26
    Hi Christen,

    Please use the code below to fetchonly related contacts when you click on the custom link of account record.

    VF page code :

    <apex:page standardController="Account" extensions="CurrentRecordIdDemoController" renderAs="Pdf">

    <apex:form >

    <apex:pageBlock title="Related Contacts ">

    <apex:pageblockTable value="{!Contacts}" var="a" cellPadding="4" border="1">

    <apex:column >

    <apex:facet name="header">Contact ID</apex:facet>

    {!a.id}

    </apex:column>

    <apex:column >

    <apex:facet name="header"> First Name</apex:facet>

    {!a.firstname}

    </apex:column>

    <apex:column >

    <apex:facet name="header">Last Name</apex:facet>

    {!a.Lastname}

    </apex:column>

    <apex:column >

    <apex:facet name="header">Account Name</apex:facet>

    {!a.Account.name}

    </apex:column>

    </apex:pageblockTable>

    </apex:pageBlock>

    </apex:form>

    </apex:page>

    Apex class for "CurrentRecordIdDemoController"

    public class CurrentRecordIdDemoController{

    public String currentRecordId {get;set;}

    public Account acc{get;set;}

    public list<Contact> Contacts{get;set;}

    public CurrentRecordIdDemoController(ApexPages.StandardController controller) {

    currentRecordId = ApexPages.CurrentPage().getparameters().get('id');

    acc = [select id ,name, AccountNumber, Type, Industry from Account where id =: currentRecordId ];

    Contacts=[select id, firstname,lastname,Account.name from Contact where Accountid=: currentRecordId ];

    }

    }

    Create a custom link on detail page and add your VF page.

    Hope this helps! Please mark as solved if it does.

    Thanks

     
0/9000