I'm trying to loop through 3 nested loop. To retrieve product Id, I need the first outer loop. To retrieve the specific product based on the product Id, I need the second loop. Finally the last loop will retrieve the qty. I've tried putting various break statement on the loop and change the position of the loop. Any ideas on how do I retrieve my product without repetition of the product or qty. Currently it's either my product or qty get repeated multiple times. How do I break out from this? Thank you so much!

      products = new List<DisplayProducts>();

       for (String i : theCookie.getValue().split('\\,')){
           for(Product__c item : [Select ProductID__c,Name, Price__c, Size__c, Colour__c, Image2__c, Description__c, Quantity__c
                From Product__c WHERE  ProductID__c = :i]) {

                   for (String q : theCookie2.getValue().split('\\,')){
                     j  = Integer.valueof(q);    
                    //If i put the products.add here, the products will be repeated
                    products.add(new DisplayProducts(item,j));  
             //If i put products.add here, the top value of the qty will be repeated
             products.add(new DisplayProducts(item,j));  

          }   // if I put break; here, the top value of the product will be repeated

Not sure what you mean by the qty being repeated as it cycles through your theCookie2 values and I am assuming they are unique so if you can explain with a data example I can help more.

Here I am removing the SOQL from the for loop and using collections:

products = new List<DisplayProducts>();

String[] prodIDs = theCookie.getValue().split('\\,');
String[] prodQty = theCookie2.getValue().split('\\,');

if(prodIDs.size() != prodQty.size()) return; //throw error here

Map<String, Product__c> prodMap = New Map<String, Product__c>();

for(Product__c item : [SELECT ProductID__c, Name, Price__c, Size__c, Colour__c, Image2__c, Description__c, Quantity__c
                       FROM Product__c WHERE ProductID__c IN :prodIDs]) {
    prodMap.put(item.productID__c, item);

for(Integer x=0; x<prodIDs.size(); x++) {
    if(prodMap.containsKey(prodIDs[x])) {
        products.add(new DisplayProducts(prodMap.get(prodIDs[x]), prodQty[x]));  
| improve this answer | |
  • Hi @eric, Thanks for your reply. I've tried your code, however the product is still repeated. What I meant was for e.g. Product, Qty -> Top, 3 Top, 1 Pants, 3 Pants, 1 The correct sequence should be Top 3, Pants 1. – taradyn Dec 21 '14 at 17:39
  • @taradyn - So the qty in theCookie2 is in the order of the theCooke values? – Eric Dec 21 '14 at 17:45
  • Yes, it's in order of the theCookie value – taradyn Dec 21 '14 at 17:46
  • @taradyn - I have updated my answer – Eric Dec 21 '14 at 17:48
  • Thanks for your reply. I've got an Error: Compile Error: Expression must be a list type: MAP<String,Product__c> for the line new DisplayProducts(prodMap.get(prodMap[x]),prodQty[x]). It supposed to add the new DisplayProducts(Product__c, qty) though.. – taradyn Dec 21 '14 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.