【问题标题】:Salesforce Apex Trigger - Calculate the sum of amount field values and update Opportunity fieldSalesforce Apex 触发器 - 计算金额字段值的总和并更新机会字段
【发布时间】: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


【解决方案1】:

您的触发器应该在before insert, before update 上触发。

由于您正在对 trigger.new 枚举进行操作,因此当对象首次创建或单独更新时,它将设置正确的值。

【讨论】:

  • 感谢您的建议,但它仍未填充 Opportunity 上的 Total_Commission 字段。
猜你喜欢
  • 1970-01-01
  • 2017-05-12
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-20
  • 1970-01-01
相关资源
最近更新 更多