You need to sign in to do that
Don't have an account?
Show Parent Account's Contacts on Child Accounts Contact related list
Hello,
In our industry we have contacts who by for multiple accounts. We are currenlty cleaning up all these duplicate contacts since they are all assigned to multiple accounts and moving towards a model that contacts who buy for multple accounts are on the Parent Account. But they still want to see the buyers in the child accounts related list. The contacts shouldn't exist on the child, only be visible.
If did a search before posting, and found this but the post was old and I wasn't sure if anyone would see my reply since the original OP didn't report back whether that solution worked for them. Any help with this would be appreciated as I am new to developing for SalesForce.
Thanks
An extension controller simply has an additional constructor that takes the standard controller as a parameter. Further, the standard controller contains the record that is being edited/viewed etc.
E.g.
If your page is viewing/editing an existing account, it will be passed on the id parameter to the standard controller.
All Answers
The principle is correct, although I think there's a bug where the contact is added to the map, as it just puts an empty list rather than a list containing the single contact. You'll also need to put an iterator into the visualforce page - a pageblocktable or datatable would do it.
If you want to have a stab and post up any problems I'll be glad to help.
Well I eventually did figure it out, but since then the project has changed to require that any contact marked as a "Buyer" customer field accross any Account with the same Parent Account ID (I need to change to a custom field because I can't reuse Parent Account. I need this field filled in even on the Parent Account and SalesForce won't allow an Account to denote itself as the Parent). So if anyone else needs this or has questions let me know, but here is the solution.
VF page
VF Controller
I found this pieces of this code online, but I don't recall where. So I am sorry if you recognize parts of your code and I did not attribut credit to your or your blog post, but thanks for your help.
Two questions I do have:
Is if there is any way to move my NEW Buyers pageblocktable to the top of the related lists without having to change my VFPage to list each individual related list?
Am I supposed to make my Controller into an object or something? I tricked SFDC into allowing my override page, but I would prefer not tohave to trick SFDC. What step am I missing? Thanks
First question - no. The apex:detail stuff comes as one block, you can't insert your own elements into it.
Second question - not sure what you mean by trick. If you couldn't see your page as an override option, that would be because you only see pages that use the account standard controller. If that is the case, you could change yours to an extension controller and have the standard controller back the page.
Thanks for the quick reply.
First Question: -> So I could get around this by changing the apex detail lists to false and then just do <apex:relatedlist list"Contacts" />
Second question: -> That would be exactly why I couldn't see it. I did trick it by adding it as a standard override and then changing it. How to I change my controller to a extension controller?
An extension controller simply has an additional constructor that takes the standard controller as a parameter. Further, the standard controller contains the record that is being edited/viewed etc.
E.g.
If your page is viewing/editing an existing account, it will be passed on the id parameter to the standard controller.
Great after doing this and creating it as a controller extension I was able to answer and do question 1!!!
Thanks for your help.
Sorry I didn't read the second part about having to pass in the account. When I do include the private Account a;
in the first line of the extension I get an error
Ideas? Thanks
Error: mycontroller2 Compile Error: unexpected token: 'Account' at line 1 column 8
That looks like a simple syntax error - can you post that updated section of code?
I appologize for my ignorance. Just trying to learn and I appreciate your help.
No worries - learning is encouraged here!
This is declaring an instance variable, so needs to be inside the class, as does the constructor. You'll also need to change your soql to use the id of the account variable "a", as the override won't pass a parameter of parentId.
Getting closer. So I changed my Account List like you said from
// List<Account> ac=[select id,Name,parentid from Account where parentid=:ApexPages.currentPage().getParameters().get('parentid')];
to
List<Account> ac=[select id,Name,parentid from Account where parentid=:b.parentid];
***WHOOPS*** edited for forgot :b.parentid
Getting this error now
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.ParentId
Class.mycontroller2.getPcons: line 9, column 1
There's a couple of things here. Firstly you need to bind the a.parentId using the colon notation:
Secondly, the contained account gets all fields that are used by visualforce page rather than all fields.
if you add the following somewhere in your page:
that will make it available to your controller.
Wow, now it is doing something I completely don't understand.
Even though I already have the Parent Account field visible on Accounts, I added your
<apex:outputField value="{!account.parentId}" rendered="false"/>
and I think that got rid of my SOQL error from above.
When I have
List<Account> ac=[select id,Name,parentid from Account where parentid <> '' and parentid=:a.parentid];
None of my Buyers show up, at all, on any accounts.
When I remove the parentid<>'' from the where clause I get all the buyers on Accounts without parentid's and no buyers on accounts with parentid's.
That coudn't be more backwards. HA.
I must admit I've slightly lost track of what this code is trying to achieve.
As it stands it will retrieve all accounts that have the same parent as the account contained by the controller. From what you say, it sounds like the account in the controller has a null parentId.
Change the rendered value to true for the outputfield added a couple of posts ago - that will show you if the account has a parent defined.
Yes the Parent Account shows up twice now, once becaues we are rendering it, and once in the Account Detail because that is where we always show it. However, I still don't see Buyers on Accounts with a Parent Account. Is three someway to debug and follow the variables. I don't think the Accounts ParentID is being passed through. I also changed the a variable to b since I use a here
Didn't help though.
Figured it out. After looking at the code again. I changed the
List<contact> pcons=[select id,Name,Ext_Id__c, accountId,Account.Name from contact where Buyer__c=True and Account.id in:ids];
from Account.parentid
At that point I also readded the parenltid <>'' and it is working great.
Thanks for your help, now I just have to finish tweaking the layout. Like including the edit/delete buttons, and adding Chatter Follow buttons to the VF page.
Thanks again.
Glad to hear you got there in the end!
Hi Bob,
Sorry to get this thread open again, but I am trying to test my controller extension and I am not sure what to test? It works fine in my sandbox.
Here is the Controller, and I am assuming to get to 76% I have to test the List<Contact> 's
And here is the VFpage
Have you started a test class that you can post?
This is the testclass I use for our Triggers, so I just added to the end of it. The testMethod newAccountpage_Test is the first one I created and got to work without errors or problems, but it doesn't list my controller extension in the Code Coverage after I run the tests. I am assuming that if the test of my controller went successful it would list in the Apex Test Runner mycontroller2 (ApexClass) # of lines tested, 100% covered. Correct?
The last one, testMyController is where I started down different path from a post I found but it doesn't work because I don't have a Save Method in my Controller, but I was starting to think I could Edit the page's Account description then somehow call the built in Save button. But this is defintely confusing.
Thanks
I think all that you are missing is a call to myController.getPCons() after you've instantiated the controller passing the standard controller in as a parameter.
*EDIT sorry we posted at the same time so I didn't see your reply.
I did make that change before you posted and it does run. So maybe I am missing something about what should be be tested?
And here is the log file for my test
It does get the 9 rows of buyers that I am looking for so it is successful and IMO it is touching or running most of my controller, so why isn't it saying it tested it?
Is this in eclipse? I've found that since spring 12 was deployed it only shows my test classes as having coverage. If that is the case, try running it in the UI.
That was it!!! The IDE (Eclpise) just never listed it as having coverage, but the log showed it was running it. So Saved to Server and Ran test on the Sandbox and got 100% Coverage. Thanks so much.