I am having a problem iterating over a map and pulling values back for all associated records. I am trying to update a text area in a work order object with the warranty status of each part in the child work detail. I have tried a few things, but get the same result. It is probably something really simple and I have just been looking at it too long too see it....
How do I iterate correctly through a map? I have tried a few things but only seem to get one result.
Here is my class code:
// query for the work details from the work order
Map<Id, SVMXC__Service_Order_Line__c> workDetailsMap = new Map<Id, SVMXC__Service_Order_Line__c>();
// query for the data and build the map using Product as the key
for (SVMXC__Service_Order_Line__c wd : [SELECT Id
, Name
, SVMXC__Product__c
, BSP_Part_Warranty_Code__c
, SVMXC__Service_Order__c
, SVMXC__Line_Type__c
, BSP_Covered_Under_Product_Warranty__c
FROM SVMXC__Service_Order_Line__c
WHERE SVMXC__Service_Order__c IN :workDetailsToQuery
AND SVMXC__Line_Type__c = :'Parts']) {
// add the entry to the workDetailMap and use the Product field as the key
workDetailsMap.put(wd.SVMXC__Service_Order__c, wd);
productsToQuery.add(wd.SVMXC__Product__c);
}
Then I try to pull results back -
for (SVMXC__Service_Order__c workOrder : triggerNew) {
Integer i = 0;
String p = ' ';
String s = ' ';
workOrder.BSP_Parts_Warranty_Status__c = ' ';
SVMXC__Warranty__c warranty = warrantiesMap.get(workOrder.SVMXC__Component__c);
//SVMXC__Service_Order_Line__c part = workDetailsMap.get(workOrder.Id) ;
for (Id key : workDetailsMap.keySet()) {
SVMXC__Service_Order_Line__c parts = workDetailsMap.get(key);
if (parts != null){
if (warranty != null) {
if (parts.BSP_Covered_Under_Product_Warranty__c != null) {
workOrder.BSP_Claim_Warranty__c = warranty.Id;
if (parts.BSP_Part_Warranty_Code__c == 'P') {
p = p + ' Compressor-';
s = S + parts.BSP_Covered_Under_Product_Warranty__c;
}
if (parts.BSP_Part_Warranty_Code__c == 'W') {
p = p + ' Part-';
s = s + parts.BSP_Covered_Under_Product_Warranty__c;
}
if (parts.BSP_Part_Warranty_Code__c == 'B') {
p = p + ' Board-';
s = s + parts.BSP_Covered_Under_Product_Warranty__c;
}
workOrder.BSP_Parts_Warranty_Status__c = workOrder.BSP_Parts_Warranty_Status__c + p + s + ' ' ;
}
}
else {
workOrder.BSP_Parts_Warranty_Status__c = 'No Warranty';
}
}
else {
workOrder.BSP_Parts_Warranty_Status__c = 'No Parts Attached';
workOrder.BSP_Claim_Warranty__c = null;
}
}
}
The warranty is always set on the work order for the last work detail record in the list. Clearly, I am missing something...Ideas?