金額の合計をしようとして、下記のコードを書きましたが、エラーになります。
コード
TotalCharges__c = [SELECT sum(EntryCharge__c) FROM AllocationTable__c WHERE id=:id];
TotalCharges__c : 通貨
EntryCharge__c : 数式 (通貨)
エラー
Save error: Illegal assignment from LIST<AggregateResult> to Decimal CalculateCharge.trigger
集計関数を利用する場合には AggregateResult 型を利用します。また集計結果のデータを直接 Decimal 型の変数に格納することはできない為、各型の valueof メソッドを使用し、型変換を行う必要があります。
具体的には下記サンプルを参照してください。
*******************************
AggregateResult ar = [SELECT sum(EntryCharge__c) s FROM AllocationTable__c WHERE id=:id];
TotalCharges__c = decimal.Valueof(string.valueof(ar.get('s')));
system.debug('data = '+String.valueof(j.TotalCharges__c));
*******************************
また、String の型変換メソッドについてはこちらを参照してください。
000385401

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.