如何从两个不同对象中的两个不同字段中减去值,并在Apex触发器中填充自定义字段? [英] How can I subtract values from two different fields in two different objects and populate a custom field in Apex Trigger?

查看:85
本文介绍了如何从两个不同对象中的两个不同字段中减去值,并在Apex触发器中填充自定义字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计划是在新的自定义字段剩余数量"中填充通过减去"Quantity_Used__c"在发票__c中在另一个对象"Invoice_Log__c"中;从总量"Quantity__c"中得出.我希望每次使用其他项目并且更新Quantity_Used__c字段时都触发该事件.

My plans were to populate a new custom field "Quantity Left" in Invoice__c by subtracting "Quantity_Used__c" in another object "Invoice_Log__c" from the total quantity "Quantity__c". I wanted this to trigger every time another item was used and the Quantity_Used__c field was updated.

我是Apex的新手,所以我不确定自己是否从右脚开始,而且我不知道如何用计算填充新字段.

I'm very new to Apex so I'm not sure if I am even starting off on the right foot and I didn't know how to populate the new field with the calculation.

trigger InvoiceTrigger on Invoice__c(after update) {
    
    Integer I;
    for (Invoice__c p : Trigger.new) {
        I = p.Quantity__c;
        List<Invoice_Log__c> a = [select Quantity_Used__c from Invoice_Log__c];
        
        Integer j = I - a;
        system.debug('Quantity left: ' + j);
    }       
} 

推荐答案

Invoice_Log__cInvoice__c有关,对吗?如果该关系是主从细节",则返回否".您甚至可能不需要此代码.如果仅仅是查找",则想一下是否要将其转换为M-D.您将获得汇总摘要";字段(我们将要使用的字段),级联删除(删除发票后,您不想留下免费的浮动订单项,对),共享规则,这些规则会自动在您可以看到发票的情况下-您可以看到所有订单项"和其他好东西.

Invoice_Log__c is related to Invoice__c, right? If the relationship is "master-detail" you might not even need code for this. If it's just a "lookup" think if you want to convert it to M-D. You'll get "rollup summary" fields (which we're going to use), cascade delete (after you delete invoice you wouldn't want to leave free floating line items, right), sharing rules that will automatically go "if you can see invoice - you can see all line items" and other goodies.

所以简单的方法是

  1. 确保有Invoice__cParent__c或类似Invoice_Log__c上的类似字段指向您的Invoice__c对象.这就是主从细节".
  2. Invoice__c对象上创建新字段,将其称为QuantityUsed__c.类型应为汇总摘要".作为计算,请选择订单项Quantity__cSUM().
  3. Invoice__c对象上创建另一个字段QuantityLeft__c.输入公式,数字.将公式写为Quantity__c - QuantityUsed__c.
  1. Make sure there's Invoice__c, Parent__c or similar field like that on Invoice_Log__c that points to your Invoice__c object. And that it's Master-Detail.
  2. On Invoice__c object create new field, call it QuantityUsed__c. Type should be "rollup summary". As calculation pick SUM() of line items' Quantity__c.
  3. On Invoice__c object create another field, QuantityLeft__c. Type formula, number. Write the formula as Quantity__c - QuantityUsed__c.

繁荣,不需要代码. SF会在从回收站创建/编辑/删除/还原时正确计算出它.

Boom, no code needed. SF will calculate it correctly on create/edit/delete/restore from recycle bin.

如果您想以代码方式进行操作(例如,如果有充分的理由不存在任何关系或无法掌握详细信息,那么……就会出现一些晦涩的情况,例如每个销售代表只看到他们的一小部分订单,只收取一点佣金...虽然微弱,但我见过这种情况发生了)-可以了.理想情况下,您将涵盖插入,更新,删除和取消删除.它可能比您想象的要难.更新应该处理更改数量或将行从一张发票重载到另一张发票的问题;)即使-我仍然会要求您考虑公式字段以进行最终计算,因为当有人在父上编辑数量"时会怎样?

If you want to do it code way (for example if there's very good reason there's no relation or it can't be master-detail... there are obscure cases like each sales rep seeing only their little part of the whole order, receiving just a bit of commission... it's weak but I've seen it happen) - it's work. Ideally you'd cover inserts, updates, deletes and undeletes. It can be trickier than you think. Update should handle changing quantity or reparenting the line from one invoice to another ;) And even if - I'd still ask you to consider formula field for final calculation because what when somebody edits the Qty on the parent?

天真但可以完成工作(只要您没有太多的订单项并进行批量修改)就会在Invoice_Log__c上触发(因为这是要编辑的内容,而不是Invoice__c)

Bit naive but would get the job done (as long as you don't have crazy amount of line items and doing bulk edits) would be trigger on Invoice_Log__c (because that's what being edited, not the Invoice__c).

trigger InvoiceLogTrigger on InvoiceLog__c (after update){
    Set<Id> invoiceIds = new Set<Id>();
    for(InvoiceLog__c il : trigger.new){
        InvoiceLog__c old = trigger.oldMap.get(il.Id);
        if(il.Quantity__c != old.Quantity__c || il.Invoice__c != old.Invoice__c){
            invoiceIds.add(il.Invoice__c);
            invoiceIds.add(old.Invoice__c);
        }
    }
    if(!invoiceIds.isEmpty()){
        List<Invoice__c> invoices = [SELECT Id, Quantity__c,
            (SELECT QuantityUsed__c FROM InvoiceLogs__r WHERE QuantityUsed__c != null)
            FROM Invoice__c
            WHERE Id IN :invoiceIds];
        for(Invoice__c i : invoices){
            Integer total = 0;
            for(InvoiceLog__c il : i.InvoiceLogs__r){
                total += il.QuantityUsed__c;
            }
            i.QuantityLeft__c = i.Quantity__c - total; // as I said, I'd just save total and do final calculation as formula
        }
        update invoices;
    }
}

这篇关于如何从两个不同对象中的两个不同字段中减去值,并在Apex触发器中填充自定义字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆