Order Confirmation Email Template:-
Dear {!HandsMen_Order__c.HandsMen_Customer__c},
Your order #{!HandsMen_Order__c.Name} has been confirmed!
Thank you for shopping with us.
Best Regards,
Sales Team
Low Stock Alert Email Template:-
Dear Inventory Manager,
This is to inform you that the stock for the following product is running low:
Product Name: {!Inventory__c.HandsMen_Product__c}
Current Stock Quantity: {!Inventory__c.Stock_Quantity__c}
Please take the necessary steps to restock this item immediately.
Best Regards,
Inventory Monitoring System
Loyality Points Email Template:-
Congratulations! You are now a {!HandsMen_Customer__c.Loyalty_Status__c} member and you are eligible for our Loyalty Rewards Program.
Enjoy exclusive discounts, early access to offers, and special member benefits.
Thank you for your continued Support.
Update Order Total :-
trigger OrderTotalTrigger on HandsMen_Order__c (before insert, before update) {
Set<Id> productIds = new Set<Id>();
for (HandsMen_Order__c order : Trigger.new) {
if (order.HandsMen_Product__c != null) {
productIds.add(order.HandsMen_Product__c);
}
}
Map<Id, HandsMen_Product__c> productMap = new Map<Id, HandsMen_Product__c>(
[SELECT Id, Price__c FROM HandsMen_Product__c WHERE Id IN :productIds]
);
for (HandsMen_Order__c order : Trigger.new) {
if (order.HandsMen_Product__c != null && productMap.containsKey(order.HandsMen_Product__c)) {
HandsMen_Product__c product = productMap.get(order.HandsMen_Product__c);
if (order.Quantity__c != null) {
order.Total_Amount__c = order.Quantity__c * product.Price__c;
}
}
}
}
Stock Deduction Trigger:-
trigger StockDeductionTrigger on HandsMen_Order__c (after insert, after update) {
Set<Id> productIds = new Set<Id>();
for (HandsMen_Order__c order : Trigger.new) {
if (order.Status__c == 'Confirmed' && order.HandsMen_Product__c != null) {
productIds.add(order.HandsMen_Product__c);
}
}
if (productIds.isEmpty()) return;
// Query related inventories based on product
Map<Id, Inventory__c> inventoryMap = new Map<Id, Inventory__c>(
[SELECT Id, Stock_Quantity__c, HandsMen_Product__c
FROM Inventory__c
WHERE HandsMen_Product__c IN :productIds]
);
List<Inventory__c> inventoriesToUpdate = new List<Inventory__c>();
for (HandsMen_Order__c order : Trigger.new) {
if (order.Status__c == 'Confirmed' && order.HandsMen_Product__c != null) {
for (Inventory__c inv : inventoryMap.values()) {
if (inv.HandsMen_Product__c == order.HandsMen_Product__c) {
inv.Stock_Quantity__c -= order.Quantity__c;
inventoriesToUpdate.add(inv);
break;
}
}
}
}
if (!inventoriesToUpdate.isEmpty()) {
update inventoriesToUpdate;
}
}
Execute anonymous window code:-
System.schedule('Daily Inventory Sync', '0 0 0 * * ?', new InventoryBatchJob());