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
adi salesforceadi salesforce 

How to display no.of records as values and account rating picklist values as keys ?

Niraj Kr SinghNiraj Kr Singh
Hi Adi,

Suppose you are having following Object and its fields: (Note: Change according to your object and fields)
Object: Testobject__c
Fields: Rating__c (Interger type)
            Industry__c (Text type)

Not getting proper of your Key of map requirements, but you can check there two use cases:       

Use Case 1: Considering Rating only as a key
Map<Integer, Integer> ratingVsRecordCountMap = new Map<Integer, Integer>();
List<AggregateResult> lstAggResult = [Select Count(Id) ct, Rating__c rt From Testobject__c Group By Rating__c ];

if(!lstAggResult.isEmpty()) {
    for(AggregateResult objAgg : lstAggResult) {
        ratingVsRecordCountMap.put(objAgg.get('rt') ,objAgg.get('ct'));
    }
}
system.debug('your result 1: ' + ratingVsRecordCountMap);

Use Case 2: Considering Rating and industry both together as a key
Map<String, Integer> ratingVsRecordCountMap = new Map<String, Integer>();
List<Testobject__c> lstAggResult = [Select Industry__c, Rating__c rt From Testobject__c Where Industry__c != Null && Rating__c != Null];
if(!lstAggResult.isEmpty()) {
    for(Testobject__c objTest : lstAggResult) {
        String strKey = objTest.Rating__c + '-' + objTest.Industry__c; //used here '-' as string seperator
        if(!ratingVsRecordCountMap.containsKey(strKey)) {
            ratingVsRecordCountMap.put(strKey, 1); //found 1st record
        }
        else {
            Integer count = ratingVsRecordCountMap.get(strKey) + 1;
            ratingVsRecordCountMap.put(strKey, count);
        }
    }
}
system.debug('your result 2: ' + ratingVsRecordCountMap);

Try this, Hope it will solve your problem.

Thanks
Niraj
adi salesforceadi salesforce
Hi Neeraj,
Thankyou for responding and for your clear explanation. let me explain my requirement.In account object  we have rating, industry picklist fields. Now I have to display like table rating picklist values industy picklist values and their corresponding records.

            education      energy      banking      insurance          
Hot             5               6                 1                   0 
Warm        10              3                 0                    2
Cold           3               5                1                    1

like this I have to display for all picklist values of rating and industry. I hope you will get the requirement.
 
Niraj Kr SinghNiraj Kr Singh
Hi Adi,

You can try this: Plz update object name and fields as per your requiremnets.
Considering:
Priority > rating
Status  > industry
Map<String, map<String, Integer>> ratingVsRecordCountMapOfmap = new Map<String, map<String, Integer>>();
List<Task> lstAggResult = [Select Priority, Status From Task Where Priority != Null AND Status != Null];
if(!lstAggResult.isEmpty()) {
    for(Task objTest : lstAggResult) {
        if(ratingVsRecordCountMapOfmap.containsKey(objTest.Priority)) {
            Map<String, Integer> innermap = ratingVsRecordCountMapOfmap.get(objTest.Priority);
            //if(!innermap.isEmpty()) {
                if(innermap.containsKey(objTest.Status)) {
                    Integer count = innermap.get(objTest.Status) + 1;
                    innermap.put(objTest.Status, count);
                }
                else {
                    innermap.put(objTest.Status, 1);
                }
                ratingVsRecordCountMapOfmap.put(objTest.Priority, innermap);
            //}
        }
        else {
            Map<String, Integer> innermap = new Map<String, Integer>();
            innermap.put(objTest.Status, 1);
            ratingVsRecordCountMapOfmap.put(objTest.Priority, innermap);
        }
    }
}
system.debug('---> ' + ratingVsRecordCountMapOfmap);


Output: (In My system)
 {
    High={In Progress=6, Not Started=1},
    Normal={In Progress=3, Not Started=1}
 }
And use this map: ratingVsRecordCountMapOfmap in VF-page to display in your way...

Note: Not considering the those records which not having values for Priority and Status. You can try this meanwhile i will update you for all as you expected.

Plz let me know if it helps you.
Mark you ans if meets your expectation.

Thanks
Niraj
adi salesforceadi salesforce
Hi
could you write vf page code for this 
 
adi salesforceadi salesforce
Niraj could you help me to write vf page for this apex code
adi salesforceadi salesforce
Hi  Niraj,

Could you write the vfpage to display like this.
                  Notstarted     Inprogress
High                 2                 1
Normal             3                 4

 
Niraj Kr SinghNiraj Kr Singh
Hi Adi,
I have posted your ans in this link, Try it with some modification
https://developer.salesforce.com/forums?id=9060G0000005l4kQAA

Hope it will help you