You need to sign in to do that
Don't have an account?
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:
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]
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]
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
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
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
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,
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.
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
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..
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