0

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?

1
  • what is the size you are getting for workDetailsMap?
    – Himanshu
    Oct 20, 2015 at 16:52

1 Answer 1

0

Since you are using a parentID as the key to store child records, each time a child records is put into the map it overwrites the previous entry because the KEY is the same. What you want to do is this:

Map<Id, SVMXC__Service_Order_Line__c[]> yourmap

for(Obj var : query){

   SVMXC__Service_Order_Line__c[] tmp = yourmap.get(wd.SVMXC__Service_Order__c);

   if(tmp != null)
      tmp.add(wd);
   else
      tmp = New SVMXC__Service_Order_Line__c[]{wd};

   yourmap.put(wd.SVMXC__Service_Order__c,tmp);

}

Then to when you get the values you will need to:

SVMXC__Service_Order_Line__c parts[] = workDetailsMap.get(key);

and iterate over them.

7
  • Thanks Eric, I am now getting an error: Illegal assignment from SVMXC__Service_Order_Line__c to List<SVMXC__Service_Order_Line__c>. Looks like this line is the culprit: SVMXC__Service_Order_Line__c[] WDList = workDetailsMap.get(wd.SVMXC__Service_Order__c);
    – Troy Smith
    Oct 20, 2015 at 20:45
  • I removed the "[]" and now I get: "Method does not exist or incorrect signature."
    – Troy Smith
    Oct 20, 2015 at 20:49
  • @TroySmith - You will have to adjust the rest of your code to deal with a list. You initial problem was that it only had the last line item. So I assumed you wanted all the line items. You will have to iterate through the list once you get it FROM the map in the second code snippet you have
    – Eric
    Oct 20, 2015 at 20:51
  • Yes, but the error is coming from the first snippet you provided (third line). I had replaced the variable names, but it posts the same error if I use the ones you provided. I have adjusted the code further down the line to pull it, but it isn't getting that far.
    – Troy Smith
    Oct 20, 2015 at 20:58
  • @TroySmith - did you change you map workDetailsMap to be: Map<Id, SVMXC__Service_Order_Line__c[]> - It sounds like you didn't
    – Eric
    Oct 20, 2015 at 21:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .