I have three objects 1)Truck__c 2)Booking__C 3)Payment___C. Truck and Booking has master detail relationship where Truckk is master and Booking is detail. Booking and payment has lookup relationship where Booking is parent and payment is child.
I want to update a field present isBooked(checkbox) in Truck based on the value present in a field remainingAmount in payment object. I have added trigger on Payment object like below
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;
}
}
}
Here first I am getting payment ID's and based on that I am fetching Booking objects which is lookup parent of payment. After this I am trying to fetch the truck object which is master of Booking. But I don't know how to query on this as where clause in query giving error
Truck__c truckObj = [Select id,Price__c from Truck__c where Truck__c.id = objBooking.Truck__c.id];
Please note there is no direct relationship between Truck and Payment
How can I fetch truck object Thanks in Advance
Short answer: while referring to parent fields, you should use the relationship name, so
Truck__rinstead ofTruck__c.Anyway it is not the only problem with that code.
Long answer:
after insert, but you check for an after update event:if(Trigger.isAfter && Trigger.isUpdate). Probably you want to run this trigger in both after insert and after update.payIdnorpaidAmountwhich you use in your first for-loop. AnywaypaidAmountwould just hold the last value, which probably you won't need.[Select Id, Booking_ID__c from Booking__c where Booking_ID__c in:payId]this query should return an empty list because inpayIdyou've stored the ids of Payment__c, that is a child of Booking__c, while in the first loop you should have stored the ids of parents Booking__c[Select id,Price__c from Truck__c where Truck__c.id = objBooking.Truck__c.id]Here there is no reason to writewhere Truck__c.idIt should be justWHERE Id = :objBooking.Truck__c.System.LimitException: Too many SOQL queries: 101. The same goes by putting a DML in a loop.I'm going to assume that the API Name of the lookup fields is the same of the parent object, so that a
Booking__cfield exists onPayment__cobject and aTruck__cexists onBooking__cobject.If I got the logic right about how setting the flag on Truck object, this should be the trigger code.
By the way, you should move the logic in an handler class, following the best practices.