+ Start a Discussion
adnan ahmad 10adnan ahmad 10 

suming up in map ?

Im maping as map<invoice__c,List<line_item__c>> Now I have to calculate for each invoice sum of quantity(in line item) is greater than 100 ? if its greater then i have to update status in invoice .
Best Answer chosen by adnan ahmad 10
bretondevbretondev
Map<invoice__c,List<line_item__c>> myMap = new Map<invoice__c,List<line_item__c>>(); 

/* Populate myMap */ 

List<Invoice__c> invoiceToUpdate = new List<Invoice__c>(); 

for ( Invoice__c inv : myMap.keySet()) { 
    List<line_item__c> lis = myMap.get(inv); 
    Integer sum = 0;
    if (lis != null) {
        for (line_item__c li :lis)
            sum += li.Quantity;
         if (sum > 100) {
              inv.Status = 'newStatus';
               invoiceToUpdate.add(inv);
          }
     }
}

if (invoiceToUpdate.size() > 0)
     update invoiceToUpdate;
}


 

All Answers

bretondevbretondev
Map<invoice__c,List<line_item__c>> myMap = new Map<invoice__c,List<line_item__c>>(); 

/* Populate myMap */ 

List<Invoice__c> invoiceToUpdate = new List<Invoice__c>(); 

for ( Invoice__c inv : myMap.keySet()) { 
    List<line_item__c> lis = myMap.get(inv); 
    Integer sum = 0;
    if (lis != null) {
        for (line_item__c li :lis)
            sum += li.Quantity;
         if (sum > 100) {
              inv.Status = 'newStatus';
               invoiceToUpdate.add(inv);
          }
     }
}

if (invoiceToUpdate.size() > 0)
     update invoiceToUpdate;
}


 
This was selected as the best answer
adnan ahmad 10adnan ahmad 10
thanks bretondev ! it worked .