简体   繁体   English

批量触发字段更新Salesforce

[英]bulk trigger field update salesforce

I'm trying to write a trigger where inserting, updating, or deleting a record(line item) will update an amount field. 我试图编写一个触发器,在其中插入,更新或删除记录(行项目)将更新金额字段。 Now all this records will have same ParentID (expense) and Name(of the line items). 现在,所有这些记录将具有相同的ParentID(费用)和Name(订单项的名称)。 Basically duplicate records except the Contact name will be different. 除了联系人姓名,基本上重复的记录将有所不同。 So, when I add a new line item with new amount with same Parents and name, the trigger should go off and query all the line items with same Parent and should re-calculate the amount. 因此,当我添加具有相同父项和名称的新金额的新订单项时,触发器应关闭并查询具有相同父项的所有行项,并应重新计算金额。

So, if I enter first line item and say Total amount should be 100. Then I enter 2nd line Item, trigger should fire and update Amount on both record saying '50.00'. 因此,如果我输入第一行项目并说总金额应为100。然后输入第二行项目,则触发器应触发并更新两条记录上的“ 50.00”金额。 For some reason, my trigger is not updating even though it's calculating it correctly. 由于某种原因,即使触发器计算正确,它也不会更新。 Where is the bug? 错误在哪里? Please help!!! 请帮忙!!!

trigger Test on Expense_Line_Item__c (after insert, after update, after delete) {
    set<id>testlist = new set<id>();
    //List<Expense_Line_Item__c> listItem= new List<Expense_Line_Item__c>();

    for (Expense_Line_Item__c a : trigger.new) {
        testlist.add(a.expense__c);
    }

    list<Expense_Line_Item__c> mapParent = 
      new list<Expense_Line_Item__c>([SELECT name,
                                             id,
                                             Amount__c
                                      FROM Expense_Line_Item__c
                                      WHERE expense__c IN:testlist]);

    Decimal Total = 0.0;
    Integer Count = 0;

    for (Expense_Line_Item__c exp : mapParent) {
        Total = Total + exp.Amount__c;
        Count++;
        System.debug('Total during iterator::::::::::::::::::::::' + Total);
        System.debug('Counter:::::::::::::::::::::::::::::::::::::' + Count);
    }

    if (Count > = 1)
        Total = Total / Count;

    System.debug('Total count after division::::::::::::::::::::::' + Total);

    List <Expense_Line_Item__c> insertLineItem = new List <Expense_Line_Item__c>();

    for (Expense_Line_Item__c lineItem : MapParent) {
        lineItem.Amount__c = Total;
        //insertLineItem.add(lineItem);
        //System.debug('LineItem Amount getting inserted::::::::::::::::::::::'+lineItem.Amount__c);
    }

    // upsert insertLineItem;

}

It looks like your code is an After trigger. 看起来您的代码是After触发器。 As you're in after trigger context you have to do DML to update the records. 在触发上下文之后,您必须执行DML更新记录。

It also seems to me you're trying to do too much in a single trigger, its always better to take each method individually so that you can fully control what you're doing. 在我看来,您也想在单个触发器中执行太多操作,最好总是单独使用每种方法,以便您可以完全控制正在执行的操作。

I see you do have an Upsert DML statement commented out but you'd be better off using Update for this particular instance as you're not using an external id or making a decision to create or insert which is the purpose of upsert 我看到您确实已注释掉Upsert DML语句,但是最好在此特定实例上使用Update,因为您不使用外部ID或决定创建或插入这是Upsert的目的

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

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