【问题标题】:Update Parent Object Field in Apex Trigger更新 Apex 触发器中的父对象字段
【发布时间】:2021-07-18 14:03:50
【问题描述】:

我有三个对象 1)卡车__c 2)预订__C 3) 付款___C. Truck 和 Booking 具有 master detail 关系,其中 Truckk 是 master 而 Booking 是 detail。 Booking 和 payment 具有查找关系,其中 Booking 是 parent, payment 是 child。

我想根据付款对象中的字段剩余金额中的值更新卡车中存在的字段 isBooked(checkbox)。我在 Payment 对象上添加了触发器,如下所示

trigger TestTrigger on Payment__c (after insert) {
 if(Trigger.isAfter && Trigger.isUpdate){
    for (Payment__c pay:Trigger.New)
    {
        payId.add(pay.id);
        paidAmount =Integer.valueOf(pay.Paid_Amount__c);
    }
    Map <id,Booking__c> bookingMap = new Map <id,Booking__c> ();
    for (Booking__c obj: [Select Id, Booking_ID__c from Booking__c where Booking_ID__c in:payId])
    {
        bookingMap.put(obj.Booking_ID__c, obj);
    }
    
    for(Booking__c objBooking:matchingIdsMap){
     Truck__c truckObj = [Select id,Price__c from Truck__c where Truck__c.id = objBooking.Truck__c.id];
     //Integer paidAmount = Payment__c.Paid_Amount__c;
     Integer totalAmount = Integer.valueOf(truckObj.Price__c);
     Integer remainingAmount = totalAmount-paidAmount;
        If(remainingAmount == 0){
            truckObj.Booked__c = true
        }
     update truckObj;
 } 
}

}

首先,我获取付款 ID,并基于此获取 Booking 对象,该对象是付款的查找父对象。在此之后,我试图获取作为预订大师的卡车对象。但我不知道如何在查询中将其作为 where 子句进行查询,并给出错误

Truck__c truckObj = [Select id,Price__c from Truck__c where Truck__c.id = objBooking.Truck__c.id];

请注意,卡车和付款之间没有直接关系

如何获取卡车对象 提前致谢

【问题讨论】:

    标签: salesforce apex apex-trigger


    【解决方案1】:

    简短回答:在引用父字段时,您应该使用relationship name,因此Truck__r 而不是Truck__c
    无论如何,这不是该代码的唯一问题。


    长答案:

    • 您的触发器位于after insert,但您检查更新后事件:if(Trigger.isAfter &amp;&amp; Trigger.isUpdate)。可能您希望在插入后和更新后都运行此触发器。
    • 您从未声明过您在第一个 for 循环中使用的 payIdpaidAmount。无论如何,paidAmount 只会保存最后一个值,您可能不需要。
    • [Select Id, Booking_ID__c from Booking__c where Booking_ID__c in:payId] 这个查询应该返回一个空列表,因为在payId 中,您存储了 Payment__c 的 ID,它是 Booking__c 的子代,而在第一个循环中,您应该存储了父母 Booking__c 的 ID
    • [Select id,Price__c from Truck__c where Truck__c.id = objBooking.Truck__c.id]这里没有理由写where Truck__c.id应该只是WHERE Id = :objBooking.Truck__c
    • 注意:将 SOQL 放入循环中,您很容易找到关于 SOQL 的 Governor Limit,这将引发 System.LimitException: Too many SOQL queries: 101。 将 DML 置于循环中也是如此。

    我将假设查找字段的 API 名称与父对象相同,因此 Booking__c 字段存在于 Payment__c 对象上,Truck__c 存在于 Booking__c 对象上。
    如果我对如何在卡车对象上设置标志的逻辑正确,这应该是触发代码。

    trigger TestTrigger on Payment__c (after insert, after update) {
        if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) {
            Map<Id, List<Payment__c>> mapBookingIdPaymentList = new Map<Id, List<Payment__c>>();
            for (Payment__c pay : Trigger.New) {
                List<Payment__c> paymentList = mapBookingIdPaymentList.get(pay.Booking__c);
                if (paymentList == null) {
                    paymentList = new List<Payment__c>();
                    mapBookingIdPaymentList.put(pay.Booking__c, paymentList);
                }
                paymentList.add(pay);
            }
            
            Map<Id, Decimal> mapTruckPrice = new Map<Id, Decimal>();
            Map<Id, Integer> mapTruckRemainingAmount = new Map<Id, Integer>();
            for (Booking__c booking: [SELECT Id, Truck__c, Truck__r.Price__c FROM Booking__c WHERE Id IN :mapBookingIdPaymentList.keySet()]) {
                mapTruckPrice.put(booking.Truck__c, booking.Truck__r.Price__c);
                
                Integer sumOfRemainingAmount = mapTruckRemainingAmount.containsKey(booking.Truck__c) ? mapTruckRemainingAmount.get(booking.Truck__c) : 0;
                for (Payment__c pay : mapBookingIdPaymentList.get(booking.Id)) {
                    sumOfRemainingAmount += pay.Paid_Amount__c != null ? pay.Paid_Amount__c.intValue() : 0;
                }
                mapTruckRemainingAmount.put(booking.Truck__c, sumOfRemainingAmount);
            }
    
            List<Truck__c> trucksToUpdate = new List<Truck__c>();
            for (Id truckId : mapTruckPrice.keySet()) {
                // There is no need to query a record just to update it if you already have its Id.
                Truck__c truck = new Truck__c(Id = truckId);
                truck.Booked__c = mapTruckPrice.get(truckId) - mapTruckRemainingAmount.get(truckId) == 0;
                trucksToUpdate.add(truck);
            }
            update trucksToUpdate; // dml outside the loop
        }
    }
    

    顺便说一句,您应该按照最佳实践移动处理程序类中的逻辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-25
      • 2011-07-29
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多