简体   繁体   English

Salesforce Apex触发器-计算金额字段值的总和并更新商机字段

[英]Salesforce Apex Trigger - Calculate the sum of amount field values and update Opportunity field

My title for this post might be little confusing but i will try to make it as clear as possible. 我对这篇文章的标题可能会有点困惑,但是我会尽力使它尽可能清晰。 I am running into an issue with Apex trigger. 我遇到了Apex触发器的问题。 We have a custom object called Receivables (Managed Packaged). 我们有一个自定义对象,称为“应收款管理”(托管包装)。 Each Opportunity relates with one or more receivable record. 每个机会都与一个或多个应收记录相关。 Master Detailed Relationship is not an option since Receivable object is managed packaged. 由于应收帐款对象是通过打包管理的,因此不能选择“主详细关系”。

Here is my logic: 这是我的逻辑:

Create a trigger on Opportunity (insert and/or update) > Loop all receivables which have matching id with triggered opportunity id and Receivable Opportunity field id (This is an Opportunity look up field in Receivables) > Use aggregated to sum the amount > Auto Populate Total Commission field. 在商机上创建触发器(插入和/或更新) >循环所有具有匹配ID且具有已触发商机ID和应收商机字段ID的应收账款(这是应收账款中的商机查找字段) >使用汇总来求和>自动填充总佣金字段。

Trigger does not throw any error but it is not auto populating as well. 触发器不会引发任何错误,但也不会自动填充。

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'));
            }
        }
    }
}

Any help would be appreciated. 任何帮助,将不胜感激。

Your trigger should fire on before insert, before update . 您的触发器应before insert, before update触发。

Since you're operating on the trigger.new enumeration, that will set the proper value when the object is either first created or updated separately. 由于您对trigger.new枚举进行操作,因此在首次创建或单独更新对象时将设置适当的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM