0

enter image description hereI have created a class and page . And Take two custom fields in opportunity Object Rating__c and Category __c .I manually insert some products in Opplineitem and Set their rating and category .my question is that i want to sort the order category wise .By Inserting some logic in my apex class for sorting please help

My apex class

public class OpportunityProduct {
    Public String opportunityId;
    Public Set<id> setId;
    public list<Product2> prdList{get;set;}

    public opportunityProduct(ApexPages.StandardController controller){

        opportunityId=ApexPages.currentPage().getParameters().get('Id'); 
        system.debug('zzzzzzz-'+opportunityId);

        list<opportunity> oppList = new list<opportunity>();
        setId = new set<id>();

        oppList = [SELECT id ,(SELECT id,Product2Id FROM opportunitylineitems)
                   FROM opportunity 
                   WHERE id =:opportunityId];
        system.debug('zzzzz'+oppList);

        for(opportunity opp:oppList){
            if(opp.opportunitylineitems!=Null){
                for(opportunitylineitem oli: opp.opportunitylineitems){
                    if(oli.Product2Id!=null){
                        system.debug('==oli=>'+oli);
                    }
                    setId.add(oli.Product2Id);
                    System.debug('zzzzzzzzz'+setId);  
                }
            }
        }

        prdList = [SELECT Name,Category__c,Rating__c 
                   FROM Product2 
                   WHERE id IN: setId ORDER BY Category__c, Rating__c Asc];
        system.debug('zzzzzzzzz'+prdList);
    }
}

My Page

<apex:page showHeader="false" standardController="Opportunity" extensions="OpportunityProduct" >
    <apex:form >
        <apex:pageBlock title="Product details">
            <apex:pageBlockTable value="{!prdList}" var="pro">
                <apex:column value="{!pro.id}"/>
                <apex:column value="{!pro.name}"/>
                <apex:column value="{!pro.Rating__c}"/>
                <apex:column value="{!pro.Category__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

2 Answers 2

0

It seems you want to display the data in four different tables if you have four categories.

For that you will have to create a map with Category as the key and List<Product2> as its value.

See below example:

Controller Code:

/******
****Declare this productCategoryMap at the top so that it is accessible in visualforce page    
*******/
Map<String, List<Product2>> productCategoryMap = new Map<String, List<Product2>>(); 

for (Product2 p : [SELECT Name,Category__c,Rating__c FROM Product2 WHERE id IN: setId ORDER BY Category__c, Rating__c Asc]) {
    if (!productCategoryMap.containsKey(p.Category__c)) {
        productCategoryMap.put(p.Category__c, new List<Product2>{p});
    }
    else {
        productCategoryMap.get(p.Category__c).add(p);
    }
}

Visualforce code

<apex:repeat value="{!productCategoryMap}" var="category">
    <apex:pageBlockSection title="{!category}">
        <apex:pageBlockTable value="productCategoryMap[category]" var="product">
            <apex:column value="{!product.Name}"/>
            <apex:column value="{!product.Rating__c}"/>
            <apex:column value="{!product.Category__c}"/>
        </apex:pageBlockTable>
    </apex:pageBlockSection>
</apex:repeat>

Here for each category a list is created in productCategoryMap. So all unique categories will be populated as a key and the products related to that category are stored as value against each category.

In visualforce, we display the key of the map as pageBlockSection title and we get the list for that category using the category key.

4
  • Hello Hemant Can you Please tell me the flow of code Commented May 10, 2018 at 9:50
  • I didn't get u. Can you elaborate, what you are not able to understand? Didn't this work? Commented May 10, 2018 at 10:05
  • This Code work absolutely Fine thanks to you . Just Wanted to know how this code is working .I didn't get it .Please Explain Commented May 10, 2018 at 10:11
  • updated the answer with some more details Commented May 10, 2018 at 11:00
0

In the SOQL query use ORDER BY properly. You can also define ASC or DESC for each field which you want to sort. Here is an example, the same principle applies to other queries.

SELECT Name,Category__c,Rating__c 
                   FROM Product2 
                   WHERE id IN: setId ORDER BY Category__c ASC, Rating__c Asc
3
  • I want A section Category wise in my Page Commented May 10, 2018 at 6:56
  • What you have tried so far and where you are stuck Commented May 10, 2018 at 6:59
  • Please see the vf page pic that i inserted . I just need to do 4 sections of Pageblocktable category wise .Thats it Thanks Commented May 10, 2018 at 7:15

You must log in to answer this question.

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