0

Attempted to solve using wrapper:

list<ProductWrapper> pws = new list<ProductWrapper>();
for(product2 p : products) {  
  for(asset_line_items__c b : p.asset_line_items__r) { 
     ProductWrapper pw = new ProductWrapper(p);
     if(b.quantity__c != null) {
          pw.ali = b;
     }
     else{
        //system.debug('products: '+p);
     }
     pws.add(pw);
}

Product wrapper class:

public class ProductWrapper {

    public Product2 product {get;set;}
    public asset_line_items__c ali {get;set;}    

    public ProductWrapper(Product2 p) {
        product = p;
        ali = new asset_line_items__c();
    }
}

END

I have the following SOQL:

List<Product2> products = [SELECT Id, Name, cost__c, remaining__c, user__c,isactive,
                        (SELECT Name, quantity__c from asset_line_items__r)
                        FROM Product2];

the above soql shows the following results:

enter image description here

What I want to do is to build the list combining this two:

so the end result should be looks like this:

quanity__c name         cost remaining used
8          painted blue 0.25   100       0.0
0          Velona       0.5    100       0.0 //if there is no qty default to 0
5          poli         0.5    100       0.0
0          green        0.75   100       0.0 //if there is no qty default to 0
1          dabra        0.75   100       0.0
0          itum         0.8    100       0.0 //if there is no qty default to 0
1          rican        0.35   100       0.0
1          rican        0.35   100       0.0

I have tried with different options using for loop but does not work.

  • back to a wrapper? our use formulas to pull the info from Product2 to the Asset Line Item then simply use the line items as the list source and display the formula values – Eric Sep 17 '16 at 4:14
  • formulas to pull? sorry did not get it – Nick Kahn Sep 17 '16 at 4:16
  • i.e Add formula fields on the line item to pull the Product name, cost, and remaining values from the product. Then you can use the line item in a list and display the new formula fields to show the product values. This would give you the multiple lines for rican since you are using the line items as the source of the list – Eric Sep 17 '16 at 4:18
  • i can not add or change anything to the object for other reasons, but is there a way you can do with collections? my brain is freez – Nick Kahn Sep 17 '16 at 4:20
  • You should use a wrapper class like you were doing. You can create whatever you need to do, combine objects, add fields, change field types. The wrapper is a very useful tool and seems to fit this situation well. You could also use two repeats. Once for the list then inside that a repeat for the lines – Eric Sep 17 '16 at 4:22
2

I guess strictly with visual force you could:

<apex:repeat value="{!products}" var="p">

     <apex:repeat value="{!p.Asset_Line_Items__r}" var="ali">

            ..Column to display p.xxx and ali.xxx..
            <apex:column headervalue="Quantity" value="{!ali.Quantity__c}"/>
            <apex:column headervalue="Name" value="{!p.name}"/>
            <apex:column headervalue="Cost" value="{!Cost__c}"/>
            ..Etc..

     </apex:repeat>

</apex:repeat>

This basically displays all the line items while preserving access to the parent product2. The display order will be based first on product2 list order then the line item order

Using your code

Class

list<ProductWrapper> pws = new list<ProductWrapper>();
for(product2 p : products) {  
  //product has not lines so create a wrapper item with default ALI
  if(p.Asset_Line_items__r.isEmpty()){
      pws.add(New ProductWrapper(p,New Asset_Line_Item__c(Quantity__c = 0));
      continue;
  }
  for(asset_line_items__c b : p.asset_line_items__r) { 
     ProductWrapper pw = new ProductWrapper(p,b);
     pws.add(pw);
}

Wrapper

public class ProductWrapper {

    public Product2 product {get;set;}
    public asset_line_items__c ali {get;set;}    

    public ProductWrapper(Product2 p, asset_line_items__c ali) {
        product = p;
        this.ali = ali;
    }
}

VF

<apex:repeat value="{!pws}" var="p">

     <apex:output panel layout="none" rendered="{!p.qty != null>

            ..Column to display p.xxx and ali.xxx..
            <apex:column headervalue="Quantity" value="{!p.ali.Quantity__c}"/>
            <apex:column headervalue="Name" value="{!p.product.name}"/>
            <apex:column headervalue="Cost" value="{!p.produtc.Cost__c}"/>
            ..Etc..

     </apex:outputPanel>

</apex:repeat>
| improve this answer | |
  • but the catch is, i do not want to display products if the assetlinitems has qty in it – Nick Kahn Sep 17 '16 at 4:31
  • @NickKahn - Thats not what you show in your table. – Eric Sep 17 '16 at 4:32
  • let me step back, what i meant to say is that lets say product has 7 rows and line item has 5 rows with qty, so at the end the total rows should be displayed is 7, two rows missing qty and 5 rows that have qty, i hope you get the point – Nick Kahn Sep 17 '16 at 4:34
  • let me give it a shot – Nick Kahn Sep 17 '16 at 4:38
  • A bit confused. Product and lines are separate. How do you know which products to use? Put a possibility in the answer so check it again – Eric Sep 17 '16 at 4:41

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.