+ Start a Discussion
james1986james1986 

put/add aggregateresult to map

hi,

I am trying to insert data into a parent table based on an aggregate query (table is called receipt__c). I will need to add the results to a map, because I am going to add data to a child table later.

 

When I run the SOQL query that retreives the aggregate result, I also get a record called concat_donor__c. I want that value in my map because I will need it when I add the child records (concat_donor__c will determine which receipt__c records each child record should be associated with).

 

The line of code "receiptmap.put(ar.get('Transaction__r.concat_donor__c'), r);"

results in: "Error: Compile Error: Incompatible key type Object for MAP<String,receipt__c> at line 33 column 9".

 

How should I be doing this? If anyone could point me in the right direct I'd really appreciate it.

 

--------------------------

    List<receipt__c> receipts = new List<receipt__c>();
    Map<String, receipt__c> receiptmap = new Map<String, receipt__c>();
    List<aggregateResult> aloc = [SELECT sum(receiptable__c)tot, Transaction__r.concat_donor__c from Allocation__C WHERE receiptable__c > 0 AND Transaction__c not in (select transaction__c from Donation_Receipt_Link__c) group by Transaction__r.concat_donor__c];
    for (AggregateResult ar : aloc)
    {
        receipt__c r = new receipt__c();
        r.Amount__c = (Decimal) ar.get('tot');
        receipts.add(r);
        receiptmap.put(ar.get('Transaction__r.concat_donor__c'), r);

    }
    insert receipts;

Best Answer chosen by Admin (Salesforce Developers) 
gm_sfdc_powerdegm_sfdc_powerde

Can you try casting ar.get('Transaction__r.concat_donor__c') to a String?

All Answers

gm_sfdc_powerdegm_sfdc_powerde

Can you try casting ar.get('Transaction__r.concat_donor__c') to a String?

This was selected as the best answer
james1986james1986

that was a quick fix- thanks!