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
CodenameSnugsCodenameSnugs 

Custom Related List Problem

I have a custom related list showing on Accounts that lists the account's discounts. This works fine, however sometimes an account won't have any discount to show as the discount are loaded against the parent account.

 

I was wondering if it is possible to create another related list to show the parent account's discounts, but show it on the child account so they can see both discounts and parent account discounts?

 

Any help would be appreciated!

Best Answer chosen by Admin (Salesforce Developers) 
AdrianCCAdrianCC

You are pretty close. :) Hang in there.

 

So, from what I read in the documentation not all the fields of the account are readily available. See here: http://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_standardcontroller.htm

"Note that only the fields that are referenced in the associated Visualforce markup are available for querying on this SObject. All other fields, including fields from any related objects, must be queried using a SOQL expression."

 

So you'll need to query all the necessary fields in the constructor.

public ParentDiscountsController(ApexPages.StandardController controller) {
        this.controller = controller;
        this.a = (Account)controller.getRecord();
        a = [SELECT Id, ParentId, Parent.Number /*list of fields here*/ FROM Account WHERE Id=:a.Id];
    }

 

 What do you mean "make the VF page span both columns on the account page layout"? Check the layout section options(the wrench in the right corner) to see if you cannot modify it from there(I might be wrong cause I don't really know your problem).

 

Have a nice day,

Adrian 

All Answers

AdrianCCAdrianCC

Hello,

 

How are you displaying the related list? Are you using or custom code in the controller to retrieve the records?


To show the Parent Account related Discounts you should use a custom controller and vf page.

 

Adrian

CodenameSnugsCodenameSnugs

Can a VF page be shown within a page layout?

AdrianCCAdrianCC

Sure... Enter a layout to edit it and you'll see a VisualForce pages section.

The only limitation is (I hope I'm not wrong) that the page needs to have the standardcontroller of the same object. Otherwise it will not show up on the layout edit list.

 

Adrian 

CodenameSnugsCodenameSnugs

That would make sense.....so I would basically need to use the standard controller for accounts on the VF page.  But can I add methods into the standard controllers?  I would need a method to query back all discounts and somehow pass the accounts parent account field into the query to pull back the correct discounts?

AdrianCCAdrianCC

You'll have an apex:page like this:

<apex:page controller="Account" extensions="MyController" >
...

 You'll need an apex class MyController where you'll get the list of related records for the parent Account.

See here how to do it:

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

 

Adrian 

CodenameSnugsCodenameSnugs

OK...sorry about the dumb questions but 'm pretty new to salesForce and a only just starting to look a apex.

 

In the extension how would I reference the parent account field within the account page for use in the SOQL query?

AdrianCCAdrianCC

There are no dumb questions :) (just answers... ).

 

So, you have access to the Account record through the acct = (Account)stdController.getRecord(); piece of code from teh controller (check out the example from the previous link that I gave you). When you extract that list for the related records just use "WHERE Account__c =:acct.ParentId"; replace Account__c with the name of your custom lookup relationship.  

CodenameSnugsCodenameSnugs

Ok so I found some code and modified it for the controller extension, does this look right?

 

public class ParentDiscountsController {
 
    private ApexPages.StandardController controller {get; set;}
    private Account a;
    public List<discounts> searchResults {get;set;}
 
    public ParentDiscountsController(ApexPages.StandardController controller) {
        this.controller = controller;
        this.a = (Account)controller.getRecord();
    }
 
	public PageReference search() {
		if (searchResults == null) {
			searchResults = new List<discounts>();
		} else {
			searchResults.clear();
		}

		String qry = 'Select d.Range__c, d.Discount__c from Discount__c d Where d.Account__c = \''+a.Parent+'\' Order By d.Range__c';
		searchResults = Database.query(qry);
		return null;
	}
}

 I should be able to reference {!searchResults} to get the results from the standard controller, but I think I need to trigger the search() function somehow first?  I should then be able to show it on the VF page with something like this?

 

<apex:pageBlockTable value="{!searchResults}" var="o" rendered="{!NOT(ISNULL(searchResults))}">
   <apex:column value="{!o.Range_c}"/>
   <apex:column value="{!o.Discount__c}"/>
</apex:pageBlockTable>

 

AdrianCCAdrianCC

You can put the code for the searchResults directly in the get:

public List<discounts> searchResults {
    get {
        return [Select d.Range__c, d.Discount__c from Discount__c d Where d.Account__c = :a.ParentId Order By d.Range__c];
    }
    set;
}

 

or write a sepparate getsearchResults()  method. See here about getters and setters: http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_methods.htm

 

Adrian 

 

CodenameSnugsCodenameSnugs

I think I've almost got this working, but I've just run into a problem.

 

The 'Parent' field on the account is a lookup field that seems to hold the parent account name.  I need to get the 'AccountNumber' field from the parent account to use in the SOQL query as the discount objects only hold the account number.

 

Any ideas on how I can achieve this?

AdrianCCAdrianCC

Parent.AccountNumber ? :)

CodenameSnugsCodenameSnugs

Geez, I feel like I'm REALLY close with this but it just not happening!

 

My controller extension now looks like this:

 

public class ParentDiscountsController {
 
    private ApexPages.StandardController controller {get; set;}
    private Account a;
       
    public ParentDiscountsController(ApexPages.StandardController controller) {
        this.controller = controller;
        this.a = (Account)controller.getRecord();
    }

    public List<discount__c> searchResults { get { return [Select d.Range__c, d.Discount__c from Discount__c d 
                Where d.Account__c = :a.ParentId Order By d.Range__c]; } set; }    
}

 

And my VF page looks like this:

 

<apex:page standardController="Account" extensions="ParentDiscountsController">
    <style type="text/css">
        body {background: #F3F3EC; padding-top: 15px}
    </style>
 
    <apex:form >
        <apex:pageBlock title="Head Office Discounts" id="block" mode="edit">
            <apex:pageMessages />
            <apex:pageBlockSection id="resultsBlock" columns="2">
                <apex:pageBlockTable value="{!searchResults}" var="d">
                    <apex:column value="{!d.Range__c}"/>
                    <apex:column value="{!d.Discount__c}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
 
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

But when I load an account page I get this error where the VF page should be:

 

Content cannot be displayed: SObject row was retrieved via SOQL without querying the requested field: Account.ParentId

 

Obviously I'm still doing something wrong here, any ideas on what it might be?

 

Also, is it possible to make the VF page span both columns on the account page layout?

AdrianCCAdrianCC

You are pretty close. :) Hang in there.

 

So, from what I read in the documentation not all the fields of the account are readily available. See here: http://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_standardcontroller.htm

"Note that only the fields that are referenced in the associated Visualforce markup are available for querying on this SObject. All other fields, including fields from any related objects, must be queried using a SOQL expression."

 

So you'll need to query all the necessary fields in the constructor.

public ParentDiscountsController(ApexPages.StandardController controller) {
        this.controller = controller;
        this.a = (Account)controller.getRecord();
        a = [SELECT Id, ParentId, Parent.Number /*list of fields here*/ FROM Account WHERE Id=:a.Id];
    }

 

 What do you mean "make the VF page span both columns on the account page layout"? Check the layout section options(the wrench in the right corner) to see if you cannot modify it from there(I might be wrong cause I don't really know your problem).

 

Have a nice day,

Adrian 

This was selected as the best answer
CodenameSnugsCodenameSnugs

You my friend are a legend!  Thanks so much for your help!

 

Now I just need to figure out how to create aPDF of it LOL

AdrianCCAdrianCC

:)... I try to be helpful when I think I know the answer.

 

For pdf you just need to add the renderas attribute to the page element:

<apex:page renderas="pdf" ....

 

The problem would be how do you want to generate this pdf? I would suggest creating a custom button that opens a different page that has the same code and the renderas specified.

 

Adrian 

CodenameSnugsCodenameSnugs

I did think about that but then I wondered how I would get the account number in the PDf page?  The controller extension is pulling the discount details based on the account on the current page, but if the button navigates to a new page then won't I be missing the account details in the query?

CodenameSnugsCodenameSnugs

Nevermind, I figures it out!  Thanks again!