Please check with sample code below which justifies the above requirement.
trigger PurchaseOrderTrigger on Purchase_order__c (before delete) {
Set<id> setPOIds = new Set<Id>();
for(Purchase_order__c objPO:trigger.old)
setPOIds.add(objPO.id);
Map<Id,List<Serialized_Product__c>> mapLinkedSPs = new Map<Id,List<Serialized_Product__c>>();
for(Serialized_Product__c objSP:[select id,Purchase_Order_Line_Item__c,Purchase_Order_Line_Item__r.Purchase_order__r.Id from Serialized_Product__c where Purchase_Order_Line_Item__r.Purchase_order__r.Id=:setPOIds]){
if(mapLinkedSPs.containsKey(objSP.Purchase_Order_Line_Item__r.Purchase_order__r.Id)) {
List<Serialized_Product__c> lstSPs = mapLinkedSPs.get(objSP.Purchase_Order_Line_Item__r.Purchase_order__r.Id);
lstSPs.add(objSP);
mapLinkedSPs.put(objSP.Purchase_Order_Line_Item__r.Purchase_order__r.Id, lstSPs);
} else {
mapLinkedSPs.put(objSP.Purchase_Order_Line_Item__r.Purchase_order__r.Id, new List<Serialized_Product__c> { objSP });
}
}
for(Purchase_order__c objPO:trigger.old){
if (mapLinkedSPs.containsKey(objPO.id)){
objPO.adderror('You cannot delete the Purchase Order as Serialized Products are linked with it');
}
}
}
Please mark this as solved if this helps so that it gets removed from the unanswered queue and will result in helping others who are looking with similar issue.
Please check with sample code below which justifies the above requirement. Please mark this as solved if this helps so that it gets removed from the unanswered queue and will result in helping others who are looking with similar issue.
Regards,
Nagendra.