You need to sign in to do that
Don't have an account?
test code
Apex Class Not Working Properly
Hi i am new to salesforce. my code is not working, the where condition in query is not filtering data and when i am using groupby its showing the error
Field must be grouped or aggregated..even when i use WHERE its throwing error.
here is the class
public List<Policy__c> getMyPolicy() {
List<Policy__c> temp =[SELECT name,AVG(Sum_Of_Premium__c),Channel_Name__r.Target__c from Policy__c GROUP BY CreatedDate];
return temp;
In this i need average of custom field sum of premium.
any idea on this why it is not working???
Try it using AggregateResults.
You can find a example here
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_agg_fns.htm
i tried with that also but its not working throwing error illegal assignment.
Did u try it as shown below.
AggregateResult[] groupedResults =[SELECT name , CreatedDate , AVG(Sum_Of_Premium__c) SOP , Channel_Name__r.Target__c from Policy__c GROUP BY CreatedDate];
for (AggregateResult result : groupedResults)
{
Policy__c.Sum_Of_Premium__c = result.get('SOP');
}
And dont use a WHERE you need to use HAVING.
Using this its showing error Field must be grouped or aggregated at name field.
TRY THIS
AggregateResult[] groupedResults =[SELECT name , CreatedDate , AVG(Sum_Of_Premium__c) SOP , Channel_Name__r.Target__c from Policy__c GROUP BY name];
I am trying this its throwing the error as Unknown property 'AccountStandardController.MyPolicy'
public class MyPolicy{
public MyPolicy(ApexPages.StandardController MyPolicy
)
{
AggregateResult[] groupedResults= [SELECT name,AVG(Sum_Of_Premium__c)aver FROM Policy__c GROUP BY name];
{
for (AggregateResult ar : groupedResults) { System.debug('name' + ar.get('name'));
System.debug('Average Sum_Of_Premium__c' + ar.get('aver'));
}
}
}
}
Is this a VF page extension?
Ya Account is the standard controller and MyPolicy is the extension on that VF page
I think the error Unknown property is in the VF page itself rather than the extension. Are you seeing this error editing the page?
when i am trying to save the page i am getting this error
Exactly. So the error is in the VF. Your Class could be fine.
whenever i doing changes in the code its displaying the error.
for example i need to get sum of a custom field which is there in child object but when i am using that function its showing error
illegal assignment or sometimes field must be grouped or aggregated.
I am not able to query on the policy object which is related to Account
So I am guessing the errors are in your VF page.
can you post your vf page too!!!
here is the page
<apex:page standardController="Account" extensions="MyPolicy" tabStyle="Account">
<apex:pageBlock title="Channel Performance Report" >
<apex:dataTable value="{!MyPolicy}" var="item" cellpadding="3" border="1">
<apex:column value="{!item.name}" />
<apex:column value="{!item.Channel_Name__r.Target__c}"/>
<apex:column value="{!item.Sum_of_Premium__c}" headerValue="Premium"/>
<apex:column value="{!item.Channel_Name__r.name}"/>
</apex:dataTable>
</apex:pageBlock>
</apex:page>
This is your problem:
<apex:dataTable value="{!MyPolicy}" var="item" cellpadding="3" border="1">
You cant bind data like this. You need to pass a list called theList with a get function in your class:
// You need to create a list like this:
private list<DataTypeName> theList;
// Then define the get function which returns the data
public list<DataTypeName> getTheList() {
return theList;
}
See developer doc here:
http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_extension.htm
You can either buld the data in the constructor as you have your class laid out or in the get function.