+ Start a Discussion
sfdcFanBoysfdcFanBoy 

same method, different output

I have "Items" object with different statuses. I need to get the following output with single soql query.

Ordered Items: 10 
Cancelled Items: 3 
Reviewed Items: 2

VF Page and apex code below:
 
Ordered Items: <apex:commandButton value="{!Count}"/> 
Cancelled Items: <apex:commandButton value="{!Count}" /> 
Reviewed Items: <apex:commandButton value="{!Count}"/> 


Public Integer Count(){ 
        List<Items__c> items = [SELECT Count(ID) FROM Items__c WHERE Status == 'Ordered' OR Status == 'Cancelled' OR Status == 'Reviewed']; 
        return items.size(); 
}



With the above code ofcourse, I get the following output

Ordered Items: 15 
Cancelled Items: 15 
Reviewed Items: 15

Im calling the same method here, but I want to return differently based on the Status. I tried to use apex:param for the commandButton, but doesn't work.

Any ideas how to implement. [I want to use only 1 SOQL, with different SOQL and different methods, I can get what I want]
Best Answer chosen by sfdcFanBoy
sandeep sankhlasandeep sankhla
Hi Manish,

Then simple declare 3 variables and get the count from one query only see the below code

    public     Integer orderedCount {get;set;}
    public     Integer CancelledCount {get;set;}
    public     Integer ReviewedCount {get;set;}

Public Integer Count(){

    orderedCount = CancelledCount = ReviewedCount = 0;
        
        
        for(Items__c items : [SELECT Id, Status FROM Items__c WHERE Status == 'Ordered' OR Status == 'Cancelled' OR Status == 'Reviewed'])
        {
            if (items.Status == Ordered)
                orderedCount = orderedCount + 1;
            else if(items.Status == Cancelled)
                CancelledCount = CancelledCount + 1;
            else if(items.Status == Reviewed)
                ReviewedCount = ReviewedCount + 1;
        }

        return items.size();

}

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 

All Answers

PratikPratik (Salesforce Developers) 
Hi Manish,

You can pass the status as a runtime parameters.  e.g. 

Public Integer Count(string status_parameter){

List<Items__c> items = [SELECT Count(ID) FROM Items__c WHERE  Status == 'status_parameter' ]; return items.size();

}

Hope this will help you.

Thanks,
Pratik
 
sandeep sankhlasandeep sankhla
Hi Maish,

Please check the below code pass the param from page and get the value in controller and use in query to return the size

Ordered Items: <apex:commandButton value="{!Count}">
                    <apex:param name="status" value="Ordered"/>
                </apex:commandButton>

Cancelled Items: <apex:commandButton value="{!Count}" >
                    <apex:param name="status" value="Cancelled"/>
                </apex:commandButton>

Reviewed Items: <apex:commandButton value="{!Count}">
                    <apex:param name="status" value="Reviewed"/>
                </apex:commandButton>

 

You can pass parameters to a controller though the page's parameter list, but not directly to the function. Here's a standard method for passing a parameter:
 
  
Public Integer Count(){
        string myStatus = apexpages.currentpage().getparameters().get('status');
        
        List<Items__c> items = [SELECT Count(ID) FROM Items__c WHERE Status == myStatus];

        return items.size();

}

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 

ra811.3921220580267847E12ra811.3921220580267847E12
HI,

set<string> valuesStatus= new Set<String>(){'Ordered','Cancelled','Reviewed'};


List<Items__c> items = [SELECT Count(ID) FROM Items__c WHERE Status IN valuesStatus];

Thank you,
sfdcFanBoysfdcFanBoy
@sandeep sankhla

Thanks for the reply.  But the param "status" is not being set to the value.  It is showing as null.  Because the param is set only on clicking the command button.  So, the param is null and hence is count I'm getting is wrong.  

The param needs to be set even on the current page.  Is my understanding correct?  Please clarify.
sandeep sankhlasandeep sankhla
Hi Manish,

Then other alternative is you can use one get set variabel then from param parameter you can use assigneTo attribute to assign the value in that variable and then that variable you can use in your method..

This will surely solve your issue..

Please check and let me know if it solve your issue..
also check this link
https://success.salesforce.com/ideaView?id=08730000000YcV8AAK

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
sfdcFanBoysfdcFanBoy
Hi Sandeep, no! the param values are set only after clicking the commabd button.  Not on load.
sandeep sankhlasandeep sankhla
Hi Manish, what is your requirnmnet , if you need the data on load only without clicking on buttons then what is the use of these buttons ? I am not sure if I miss anything here, can you please elaborate on your req more ?
 
sfdcFanBoysfdcFanBoy
The command buttons are to redirect the user to different views on clicking.  Ex: clicking on 10, the user will be redirected to the Ordered items list.
sandeep sankhlasandeep sankhla
Hi manish, 
Then it is more simply..on load of your page in constructor itself you can get the data in 3 variables regarding these 3 types and then show them in VF page and then onclick of those you redirect them..
sfdcFanBoysfdcFanBoy
thats my original question, how to do it in single variable or rather single SOQL. instead of multiple variables/queries.
sandeep sankhlasandeep sankhla
Hi Manish,

Then simple declare 3 variables and get the count from one query only see the below code

    public     Integer orderedCount {get;set;}
    public     Integer CancelledCount {get;set;}
    public     Integer ReviewedCount {get;set;}

Public Integer Count(){

    orderedCount = CancelledCount = ReviewedCount = 0;
        
        
        for(Items__c items : [SELECT Id, Status FROM Items__c WHERE Status == 'Ordered' OR Status == 'Cancelled' OR Status == 'Reviewed'])
        {
            if (items.Status == Ordered)
                orderedCount = orderedCount + 1;
            else if(items.Status == Cancelled)
                CancelledCount = CancelledCount + 1;
            else if(items.Status == Reviewed)
                ReviewedCount = ReviewedCount + 1;
        }

        return items.size();

}

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
This was selected as the best answer