You need to sign in to do that
Don't have an account?
Alex Lazarev
Get Sale ID to acquire child objects items quantity and check then with stock
Hello guys,
Continuing with my trials of solving the problematic with initial skills of apex development I have the next problematic:
I'm trying to get Sales__c Id with status 'Sold', with the Sale Id check inside the Order_Item__c that belongs to the Sale values Vehicle_Name__c,Item_Quantity__c.
Then search by Vehicle_Name__c inside Vehicle__c and check if Vehicle__c.Quantity__c <= Order_Item__c.Item_Quantity__c
This is what I managed to do so far but its not working and I have no clue on how to continue.
Could someone help me please with the code?
Continuing with my trials of solving the problematic with initial skills of apex development I have the next problematic:
I'm trying to get Sales__c Id with status 'Sold', with the Sale Id check inside the Order_Item__c that belongs to the Sale values Vehicle_Name__c,Item_Quantity__c.
Then search by Vehicle_Name__c inside Vehicle__c and check if Vehicle__c.Quantity__c <= Order_Item__c.Item_Quantity__c
This is what I managed to do so far but its not working and I have no clue on how to continue.
Could someone help me please with the code?
trigger SaleCheckout on Sales__c (before update) { //Map<Id,Sales__c> salesSold = new Map<Id,Sales__c>([SELECT Id FROM Sales__c WHERE Sale_Satus__c = 'Sold']); List<Sales__c> sale = [SELECT Id FROM Sales__c WHERE Sale_Satus__c = 'Sold']; System.debug(' List<Sales__c> salesSold = ' + sale); Set<String> s = New Set<String>(); for(Sales__c sales : sale) { s.add(sales.Id); } Map<Id,Order_Item__c> orders = new Map<Id,Order_Item__c>([SELECT Id,Vehicle_Name__c,Item_Quantity__c FROM Order_Item__c WHERE Sale__c IN : s.Id]); }
Amit Singh
All Answers
Use below code. Hope this helps :)
Thanks!
AMit Singh
I have one more question, since on the last FOR
I need to place an error but on Sales__c custom object inside the if. But this for only refeers to Order_Item__c.
How can I refeer to the original object to place the error message properly?
Thanks!
Amit Singh
Thank you,
Now I have to update the records in vehicles doing vehicles quantity - ord.Item_Quantity_c
If you have any quick suggestions, i'll be thankful.
Amit Singh
I'll test it in a while and let you know.
Sales__c with fields: Id, Status
Order_Item__c with fields: Id, Sale__c, Vehicle_Name__c, Item_Quantity__c
Vehicle__c with fields: Id, Name, Quantity__c
I'm not sure why you are referencing vehicles by name instead of Id, but that's your call. If that's the case, something like this should work:
trigger SaleCheckout on Sales__c (before update) { List<Order_Item__c> orders = [SELECT Vehicle_Name__c, Item_Quantity__c FROM Order_Item__c WHERE Sale__c IN :Trigger.new AND Sale__r.Status = 'Sold']; System.debug('2-- Orders IDs: ' + orders); Set<String> vehicleNames = new Set<String>(); for (Order_Item__c order : orders) { vehicleNames.add(order.Vehicle_Name__c); } List<Vehicle__c> vehicles = [SELECT Name, Quantity__c FROM Vehicle__c WHERE Name IN :vehicleNames]; for (Vehicle__c vehicle : vehicles) { for (Order_Item__c order: orders) { if (order.Vehicle_Name__c == vehicle.Name && vehicle.Quantity__c < order.Item_Quatity__c) { //do something here, let's say we want to fail the sale Trigger.newMap.get(order.Sale__c).addError('You are trying to sell more cars than we have! You sold ' + String.valueOf(order.Item_Quantity__c) + ' cars of type ' + order.Vehicle_Name__c + ' and we only have ' + String.valueOf(vehicle.Quantity__c) + ' on the lot!'); } } } }
This assumes that you don't have multiple orders for the same car, in which case you'd probably have to add up the quantities of multiple orders. Keep in mind I just wrote that code out in SO and it can easily contain errors.