+ Start a Discussion
brozinickrbrozinickr 

Illegal assignment from LIST<AggregateResult>

Helllooo.

 

Wondering if someone could help me with issue I having with this trigger I am attempting to write.

 

The gist of the what I am trying to do is this:  I have a Contract ID field in salesforce on my Opportunity and on a custom object called Recon_Detail__c.  Each Recon_Detail is basically like a line item for one Contract.  My plan for this trigger is to aggregate the sum of all of the Recon_Details of one Contract ID and then write the value on the Total_Amount__c field on the Opportunity.

 

So for example, Recon_Detail 1 has a contract ID of 123123 and amount of $100, Recon Detail 2 has a contract ID of 123123 and amount of 125, Recon Detail 3 has a contract ID of 123124 and amount of $123.

 

When the Opportunity with the Contract ID of 123123 updates, then the Total Amount field should populate with the amount of $225.

 

I'm getting this error in my code: Illegal assignment from LIST<AggregateResult> to LIST<Recon_Detail__c> at line 19 column 1 (line marked in red below)

 

trigger UpdateOppReconDetail on Opportunity (before update) {

//Create a placeholder list of Contract Id's from the opportunity called oppids and
//placeholder map for contract id and total amount from recon detail called rdmap

List<string> oppids = new List<string>();
Map<string,string> rdmap = new Map<string,string>();

    //add the Opportunity contract ids that have blank Total_Amount__c field on Opportunity to the oppids list

    for(Opportunity o : Trigger.new) {
        if(o.Total_Amount__c == null){
            oppids.add(o.Contract_ID__c);
        }
    }

//Get the Contract Id and Amount of all Recon Details that are in the OppId List

List<Recon_Detail__c> rds = [SELECT Contract_ID__c, SUM(Amount__c)sum
                             FROM Recon_Detail__c
                             where Contract_ID__c IN :oppids
                             GROUP BY Contract_ID__c];

    //Create the map that will be used to update; placing Contract ID, then Amount
    
    for(Recon_Detail__c rd : rds) {
        rdmap.put(rd.Contract_ID__c, String.Valueof(rd.get('sum')));
    }

    //If the Recon_Detail__c is blank/null, then get the opportunity id (key) from the map and place it's Amount (value) in that field
    
    for(Opportunity o : Trigger.new) {
           o.Total_Amount__c = rdmap.get(o.Contract_ID__c);
    }

}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
DharmeshDharmesh

You are collecting the aggregate results from the query to list of type Recon_Detail__c. Please change it to AggregateResult type as shown below.

 

 

List<AggregateResult> rds = [SELECT Contract_ID__c, SUM(Amount__c)sum
                             FROM Recon_Detail__c
                             where Contract_ID__c IN :oppids
                             GROUP BY Contract_ID__c];

All Answers

DharmeshDharmesh

You are collecting the aggregate results from the query to list of type Recon_Detail__c. Please change it to AggregateResult type as shown below.

 

 

List<AggregateResult> rds = [SELECT Contract_ID__c, SUM(Amount__c)sum
                             FROM Recon_Detail__c
                             where Contract_ID__c IN :oppids
                             GROUP BY Contract_ID__c];

This was selected as the best answer
brozinickrbrozinickr

Thank you Dharmesh! :)