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.
Logic:
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.
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){
return;
}
}
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')
);
BillingEventItemtoInsert.add(BECopy);
}
system.debug('List to Insert--->'+BillingEventItemtoInsert);
//insert BillingEventItemtoInsert;
}