0

我有三个对象 1)Truck__c 2)Booking__C 3)Payment___C。Truck 和 Booking 具有 master detail 关系,其中 Truckk 是 master 而 Booking 是 detail。预订和付款具有查找关系,其中预订是父项,付款是子项。

我想根据付款对象中的字段剩余金额中的值更新卡车中存在的字段 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];

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

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

4

1 回答 1

1

简短回答:在引用父字段时,您应该使用关系名称Truck__r而不是Truck__c.
无论如何,这不是该代码的唯一问题。


长答案:

  • 您的触发器位于 中after insert,但您检查更新后事件:if(Trigger.isAfter && Trigger.isUpdate)。可能您希望在插入后和更新后都运行此触发器。
  • 您从未在第一个 for 循环中声明payId也不使用哪个。paidAmount无论如何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 的州长限制,这将引发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
    }
}

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

于 2021-07-18T18:12:49.457 回答