【发布时间】:2017-03-07 20:05:29
【问题描述】:
我的这篇文章的标题可能有点混乱,但我会尽量让它清楚。我遇到了 Apex 触发器的问题。我们有一个名为 Receivables (Managed Packaged) 的自定义对象。每个机会都与一个或多个应收记录相关。主详细关系不是一个选项,因为应收账款对象是管理打包的。
这是我的逻辑:
在商机上创建触发器(插入和/或更新)>循环所有具有匹配 id 与触发商机 id 和应收商机字段 id 的应收账款(这是应收账款中的商机查找字段) > 使用聚合对金额求和 > 自动填充总佣金字段。
触发器不会引发任何错误,但它也不会自动填充。
trigger newRecaivables on Opportunity (after insert, after update)
{
set<Id> oppid = new set<id>();
list<opportunity> opplist = [select id from opportunity where id in : oppid ];
for(Opportunity Opp : trigger.new)
{
List<aggregateResult> results = [select Fees_Received_Category__c ,sum(McaApp__Amount__c) total from McaApp__Receivable__c Where McaApp__Opportunity__c in:oppid group by Fees_Received_Category__c];
for(AggregateResult ar : results)
{
if (String.valueOf(ar.get('Fees_Received_Category__c'))=='Received')
{
Opp.Total_Commission__c = String.valueOf(ar.get('total'));
}
}
}
}
任何帮助将不胜感激。
【问题讨论】:
-
oppid为空。您没有向它添加值并使用该集合进行查询。 -
嗨 Reshma,谢谢你的线索。我能够让它工作。
-
尽量避免在 for 循环中使用 SOQL
标签: salesforce aggregate apex-trigger