1

I want to add a visualforce page to the page layout of a custom object. The custom object is a child of the account. The visual force page will show a list of records on 2 other custom objects.

Account

References__c (child of Account) field named Account__c = lookup to the account

Owned_Products__c (child of Account) field named Account__c = lookup to the account

Rating__c (child of Account) field named Account__c = lookup to the account

I was just trying to make a quick demo of how this might work and "quick" turned into way too long. I just took some basic code to get a list, but I cannot get the list to only show what is related to the account.

Controller:

public with sharing class wrapper_test_controller {

    public wrapper_test_controller(ApexPages.StandardController stdController) {
    }
public list<wrapperclass> wrapperelement_for_prod{get;set;}
public list<wrapperclass> wrapperelement_for_rating{get;set;}
list<Owned_Products__c> object1 = [SELECT Account__c,Product_Status__c,Initial_Installation_Date__c,Implemented_Date__c, Name FROM Owned_Products__c];
list<Rating__c> object2 = [select id,name from Rating__c ];

public list<wrapperclass> getprodwrapperret(){
wrapperelement_for_prod = new list<wrapperclass>();
for(Owned_Products__c obj1: object1){
wrapperelement_for_prod.add(new wrapperclass(obj1));
}
return wrapperelement_for_prod;
}
public list<wrapperclass> getratingwrapperret(){
wrapperelement_for_rating = new list<wrapperclass>();
for(Rating__c obj2: object2){
wrapperelement_for_rating.add(new wrapperclass(obj2));
}
return wrapperelement_for_rating;
}
public class wrapperclass{
public Owned_Products__c object_prod_1{get;set;}
public Rating__c object_rating_2{get;set;}

public wrapperclass(Owned_Products__c obj1){
this.object_prod_1 = (obj1);
}
public wrapperclass(Rating__c obj2){
this.object_rating_2 = (obj2);}
}
}

VF Page:

<apex:page Standardcontroller="References__c" extensions="wrapper_test_controller">
    <apex:form >
        <apex:pageblock >
         <apex:pageblocktable value="{!prodwrapperret}" var="acc">
          <apex:column value="{!acc.object_prod_1.name}" />
          <apex:column value="{!acc.object_prod_1.Product_Status__c}" />
          <apex:column value="{!acc.object_prod_1.Initial_Installation_Date__c}" />
          <apex:column value="{!acc.object_prod_1.Implemented_Date__c}" />
          </apex:pageblocktable>
          <br/>
          <br/>
          <br/>
          <apex:pageblocktable value="{!ratingwrapperret}" var="con">
          <apex:column value="{!con.object_rating_2.name}" />
          </apex:pageblocktable>
         </apex:pageblock>
    </apex:form>
</apex:page>

I know the code is awful, like I said it was just supposed to be a demo of how it might look. I just need to know how to filter the lists that shows on the page Account. I feel like this should be easy and I am making it way too hard. Any pointers in the right direction would be great.

  • pro tip, get Skuid, this sort of page can be built out very quickly with just points and clicks, and it can be extended with JS if you're missing the coding for visualforce – Ralph Callaway Jul 16 '14 at 4:54
1

I wanted to let everyone know that I did get this resolved. Below is the code. (whew!) I could not get the product information and the rating information in the same table, so - if you have any suggestions about that it would be appreciated. But this does what we needed it to do, so I felt like it was the final answer.

public with sharing class ProductInfoClass {

 private final Reference_profile__c reference;
    public list<wrapperclass> prodwrapperret { get; set; }
    public list<wrapperclass> ratingwrapperret{get;set;}

    public ProductInfoClass(ApexPages.StandardController stdController) {
        reference = [SELECT Id, Account__c FROM Reference_profile__c
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

         list<MC_Products__c> object1 = [
            SELECT Id, Account__c,Product_Status__c,Initial_Installation_Date__c,
            Implemented_Date__c, Name 
            FROM MC_Products__c
            WHERE Account__c =:reference.Account__c]; 

        list<Rating__c> object2 = [
        SELECT id,mc_products__c, Account__c, mc_products_name__c,Rating__c, date__c 
        FROM Rating__c 
          WHERE Account__c =:reference.Account__c AND mc_products__c in:object1 Order by Date__c DESC];

       prodwrapperret = new list<wrapperclass>();
        for(MC_Products__c obj1: object1){
            prodwrapperret.add(new wrapperclass(obj1));
        }
        ratingwrapperret = new list<wrapperclass>();
        for(Rating__c obj2: object2){
            ratingwrapperret.add(new wrapperclass(obj2));
        }
    }

    public class wrapperclass{
        public MC_Products__c object_prod_1{get;set;}
        public Rating__c object_rating_2{get;set;}

        public wrapperclass(MC_Products__c obj1){
            this.object_prod_1 = (obj1);
        }
        public wrapperclass(Rating__c obj2) {
            this.object_rating_2 = (obj2);
        }


}    public Reference_profile__c getreference() {
        return reference;    
     } 

}

VF

<apex:page Standardcontroller="Reference_profile__c" extensions="ProductInfoClass" 
            sidebar="false" showheader="false">
    <apex:form >
        <apex:pageblock mode="inlineEdit">
         <apex:pageblocktable value="{!prodwrapperret}" var="acc">
          <apex:column headerValue="Product" value="{!acc.object_prod_1.name}" />
          <apex:column headerValue="Status" value="{!acc.object_prod_1.Product_Status__c}" />
          <apex:column ><apex:commandButton action="{!quicksave}" value="Save" id="theButton1"/></apex:column>
          </apex:pageblocktable>
          <br/>
          <br/>
          <br/>
          <apex:pageblocktable value="{!ratingwrapperret}" var="con">
          <apex:column headerValue="Product"  value="{!con.object_rating_2.MC_Products_Name__c}" />
          <apex:column headerValue="Rating"  value="{!con.object_rating_2.Rating__c}" />
          <apex:column headerValue="Date" value="{!con.object_rating_2.Date__c}" />
          </apex:pageblocktable>
         </apex:pageblock>
    </apex:form>
</apex:page>
0

You'll need to filter the results you returned based on the record your page is on. The standard controller has a getId() function that will get you the related record id.

Secondarily, move all of your variable assignments into the constructor and use the property { get; set; } magic attributes instead of having both a property and a getter/setter method.

You might end up with a controller like this that should work with your existing visualforce page

public with sharing class wrapper_test_controller {

    public list<wrapperclass> prodwrapperret { get; set; }
    public list<wrapperclass> ratingwrapperret{get;set;}

    public wrapper_test_controller(ApexPages.StandardController stdController) {
        list<Owned_Products__c> object1 = [
            SELECT Account__c,Product_Status__c,Initial_Installation_Date__c,
            Implemented_Date__c, Name 
            FROM Owned_Products__c
            WHERE Account__c = :stdController.getId() // <-- this is the really important part
        ];
        list<Rating__c> object2 = [select id,name from Rating__c ];
        prodwrapperret = new list<wrapperclass>();
        for(Owned_Products__c obj1: object1){
            prodwrapperret.add(new wrapperclass(obj1));
        }
        ratingwrapperret = new list<wrapperclass>();
        for(Rating__c obj2: object2){
            ratingwrapperret.add(new wrapperclass(obj2));
        }
    }

    public class wrapperclass{
        public Owned_Products__c object_prod_1{get;set;}
        public Rating__c object_rating_2{get;set;}

        public wrapperclass(Owned_Products__c obj1){
            this.object_prod_1 = (obj1);
        }
        public wrapperclass(Rating__c obj2) {
            this.object_rating_2 = (obj2);
        }
    }
}
  • I tried this yesterday (some variation of that, anyway) and since the vf page will be on the Reference Object, it gets that Id instead of the Account Id. Which produces no results. – Merry Stambaugh Jul 16 '14 at 17:29
  • @Merry have you tried tweaking the query? Can you do it in the dev console? You're going to need to put a little elbow grease into this – Ralph Callaway Jul 16 '14 at 17:32
  • Also, unless you're willing to invest a lot of time in learning visualforce and apex I'd highly suggest just dropping your approach and going to Skuid – Ralph Callaway Jul 16 '14 at 17:34
  • I spent about 4 hours on this yesterday, as I stated I thought it should be relatively simple. Anything I know about Visualforce and Apex I have learned by trial and error, Looking at what others have done, reading the help & training, and when I am completely stumped - asking for help here. I appreciate that you took the time to answer, I don't appreciate the implication that I have not spent time trying to figure it out for myself. When I figure this out, I will update my code. – Merry Stambaugh Jul 16 '14 at 17:44
  • @Merry didn't mean to imply that, but that would be a great paragraph to add to your question. My implication was that you might be over your head, and you should consider switching your approach away from visualforce, i.e. skuid, or dedicate some time to learning visualforce and apex. A good starting points is the Visualforce Workbook, and the Apex Workbook – Ralph Callaway Jul 16 '14 at 17:58
0

Once you have the reference record, grab the account id from it and use that in your WHERE clause of your soql query. For example if you set Id accountId = References__c.Account__c then you can add WHERE Account__c = :accountId. Account__c may be named differently depending on how the fields are named on each related object.

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.