1

I'm trying to separate my Visualforce table by Hummer's Id value:

public List<Hammer__c> getHummers() {
    return [SELECT Id, Name FROM Hammer__c];
}

public List<Factory__c> getFactories() {
    List<Hammer__c> hummers = getHummers();
    List<Factory__c> factories;

    for(Hammer__c h : hummers){
        factories = [SELECT Name FROM Factory__c WHERE Hammer__c =: h.Id];
    }
    return factories;
}

VF:

<apex:repeat value="{!hummers}" var="h" >
        <apex:pageBlock title="{!h.Name}">
            <apex:pageBlockTable value="{!factories}" var="f" border="1px" width="100%">
                <apex:column value="{!f.Name}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
</apex:repeat>

But of caurse I get only the last Factory List (after return factories) with the same values: enter image description here How can I fix it to get values for each Hummer?

2 Answers 2

2

Please find the below code snippet:

Apex Class:

public List<Hammer__c> getHummers() {
    return [SELECT Id, Name, (SELECT Name FROM Factories__r ) FROM Hammer__c];
}

VF page:

<apex:repeat value="{!hummers}" var="hummer" >
    <apex:pageBlock title="{!hummer.Name}">
        <apex:pageBlockTable value="{!hummer.Factories__r}" var="factory" border="1px" width="100%">
            <apex:column value="{!factory.Name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:repeat>

Let me know if this is something you are looking for.

Note: I assume you have the plural name as Factories__r.

Also, just wanted to add a comment on your code, Query or DML in the for Loop are not at all recommended and you will end up in hitting the Governer Limits of the salesforce in case you have one.

1
  • 1
    Thank you for help :) Commented Jul 30, 2019 at 12:54
1

Here you can make use of the relationship queries. You can query on Factory__c object along with the Hammer__c object which will tie the factory records to its respective hammer record.

That way, it would need only single query and only one method i.e. getHummers would suffice your needs and there won't be any need to use maps to associate factory records to hummer records.

You can read about Relationship Queries from Salesforce documentation or can go through the Apex Basics and Database trailhead module to know more on this topic.

So here is how you can use realtionship queries in your controller

public List<Hammer__c> getHummers() {
    return [SELECT Id, Name, (SELECT Name FROM Factories__r) FROM Hammer__c];
}

And your visualforce page name would be

<apex:repeat value="{!hummers}" var="h" >
        <apex:pageBlock title="{!h.Name}">
            <apex:pageBlockTable value="{!h.Factories__r}" var="f" border="1px" width="100%">
                <apex:column value="{!f.Name}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
</apex:repeat>

Note : Assuming Factories__r as the child relationship name, if not, then replace the child relationship name with Factories__r in both controller and visualforce page.

1
  • Thank you for help :) Commented Jul 30, 2019 at 12:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .