I am getting errors when storing my aggregate result value in the currency field. I have tried to look into a similar error but not getting anything.

Below is the logic which I am trying to implement.

I have written one trigger on the After Update Event that whenever Parent Object is marked as Released then child obj A records should be copied over to child obj B. At the moment it is working as expected but what I want to achieve is when records are copied to Object B and if Description and Unit Price is the same for the records then it should only create one record and Qty should sum up from the all the records where Unit Price and Description is Same.

I have summarised the below requirements in excel. enter image description here

trigger billingevent_au on pse__Billing_Event__c (after update) {
    for(pse__Billing_Event__c obj : trigger.new){
        if(obj.pse__Is_Released__c == Trigger.oldMap.get(obj.id).pse__Is_Released__c){
    List<Billing_Event_Item_Copy__c> BillingEventItemtoInsert = new List<Billing_Event_Item_Copy__c>();
    AggregateResult[] groupedResults = [SELECT Diab_Description__c, UnitPriceText__c, pse__Billing_Event__c , CurrencyIsoCode, Sum(pse__Quantity__c) qty FROM pse__Billing_Event_Item__c  
         where pse__Billing_Event__c = :trigger.new group by Diab_Description__c, UnitPriceText__c, pse__Billing_Event__c, CurrencyIsoCode ];
    for (AggregateResult ar : groupedResults)  {
        System.debug('Descrition' + ar.get('Diab_Description__c'));
        System.debug('UnitPriceText' + ar.get('UnitPriceText__c'));
        System.debug('Quantity' + ar.get('qty'));
        Billing_Event_Item_Copy__c BECopy = new Billing_Event_Item_Copy__c(
            Billing_Event__c = (Id)ar.get('pse__Billing_Event__c'),
            Quantity__c = (double)ar.get('qty'),
            Unit_Price__c = Decimal.valueOf(ar.get('UnitPriceText__c')),**//Getting error on this line**
            Description__c = (string)ar.get('Diab_Description__c'),
            CurrencyIsoCode = (string)ar.get('CurrencyIsoCode')  

    system.debug('List to Insert--->'+BillingEventItemtoInsert);
    //insert BillingEventItemtoInsert;  

1 Answer 1


When you .get() from an AggregateResult, Salesforce returns an Object.

You pretty much always need to cast that to some other type before it can be used with anything else.

So instead of

Unit_Price__c = Decimal.valueOf(ar.get('UnitPriceText__c'))

You still need to cast, because Decimal.valueOf() expects a String, Long/Integer, or Decimal

Unit_Price__c = Decimal.valueOf((String)ar.get('UnitPriceText__c'))
  • Thanks, @Derek Very much for your help. It worked perfectly
    – Avesh
    Commented Apr 18, 2022 at 11:17
  • Can I ask for another suggestion is the above code correct or can there be still improvement done?
    – Avesh
    Commented Apr 18, 2022 at 11:19
  • 1
    @Avesh Your code, by the way, looks fine. It does everything it needs to do, as far as I can tell. That said, remember to remove System.debug from the code before deploying to production. It can waste a lot of CPU time even when no debug logs are generated.
    – sfdcfox
    Commented Apr 18, 2022 at 14:16
  • Thanks @sfdcfox will remove the debug logs before moving to PROD
    – Avesh
    Commented Apr 18, 2022 at 17:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .