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
test codetest 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???

New_DeveloperNew_Developer

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

test codetest code

i tried with that also but its not working throwing error illegal assignment.

New_DeveloperNew_Developer

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');

}

 


carlocarlo

And dont use a WHERE you need to use HAVING.

test codetest code

Using this its showing error Field must be grouped or aggregated at name field.

 

 

 

New_DeveloperNew_Developer

TRY THIS

 

AggregateResult[] groupedResults  =[SELECT name , CreatedDate , AVG(Sum_Of_Premium__c) SOP , Channel_Name__r.Target__c from Policy__c GROUP BY name];

 

test codetest code

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'));

}

}

}

 }

carlocarlo

Is this a VF page extension?

test codetest code

Ya Account is the standard controller and MyPolicy is the extension on that VF page

carlocarlo

I think the error Unknown property is in the VF page itself rather than the extension.  Are you seeing this error editing the page?

test codetest code

when i am trying to save the page i am getting this error

carlocarlo

Exactly.  So the error is in the VF.  Your Class could be fine.

test codetest code

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

carlocarlo

So I am guessing the errors are in your VF page.

New_DeveloperNew_Developer

can you post your vf page too!!!

test codetest code

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>

carlocarlo

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.