Trouble creating a nested list using nested for loops

I have multiples expression rules that gather data as lists, and I am trying to organize them as 1 list to display in a form. 

Long story short I need to show a list of interfaces, and when building the list with nested for loops, I need to add a list to the main list, and it only adds the first item of the list. I have tested the for loop structure, and the fv!itemCount of the inner for loop is 4, and only adds the first value to the main list. Is there a way to add a list to a list as the individual items? 

Basic code structure looks like

forEach(

items: list from variable,

expression: forEach(

items: list from variable,

expression: if(

condition,

forEach(

items: list from expression based on parent for loop value,

expression: interface *Here is where it only adds the first value*

),

interface

)

)

)

 

What do I need to do so this results in a flat list of interfaces? in the if statement I either need to add one interface, or add multiple based on the condition. It currently only adds one interface even when the for loop is given a list of 4 items

  Discussion posts and replies are publicly visible

Parents
  • Appian will try to flatten if you just append or type cast.  Have you considered using dictionary to help preserve the nested list structuring? 

    load(
      local!a: enumerate(9),
      local!b: enumerate(11) + 1000,
      {
        a!forEach(
          items: local!a,
          expression: {
            first: fv!item,
            second: local!b
          }
        )
      }
    )

  • I have not tried that, how can I get a dictionary to work with interfaces? I am making a list of component interfaces to use in a column layout, and I get an error that a dictionary is invalid in a column layout
  • 0
    Appian Employee
    in reply to tylers358

    Could you share the components you're attempting to nest? or a code snippet?  with interfaces usually the flattened nested list is enough to satisfy most display needs.  Here's an example of simply iterating the same few fields for each section

    load(
      local!a: repeat(3,a!sectionLayout(label:"1")),
      local!b: {a!textField(label:"A"),a!textField(label:"B"),a!textField(label:"C")},
      {
        a!forEach(
          items: local!a,
          expression: {
            fv!item,
            local!b
          }
        )
      }
    )

     

    also, if you're seeing rendering issues because the code isn't flat, you can use a!flatten() docs.appian.com/.../fnc_array_a_flatten.html

  • Ok so orderPartSummary() is an abstract section layout I use and pass it the information to show for a specific part. To get all of the parts, I need to use expressions to get a list of parts as some items are parts themselves, or an assembly which can be traversed to get a list of all parts. I use the following to try to get all orderPartSummary() for all the parts.

     

    a!forEach(
      items: local!data.sales,
      expression:
        with(
          local!id: fv!item,
          a!forEach(
            items: local!data.li[fv!index],
            expression: {
              with(
                local!bomId: fv!item.id,
                local!item: fv!item,
                if(
                  fv!item.recordTypeId = 5,
                  a!forEach(
                    items: rule!MRP_GetAllPartsForAssembly(local!item.recordId),
                    expression: {
                      first: {

                      rule!MRP_OrderPartSummary(
                        part: rule!MRP_GetPartById(fv!item),
                        partPrice: null,
                        vendorPart: null,
                        salesOrderLineItemId: local!id,
                        bomLineItemId: local!bomId
                      )

                    },
                      second: fv!itemCount
                    }
               ),
               rule!MRP_OrderPartSummary(
                 part: rule!MRP_GetPartById(fv!item.recordId),
                 partPrice: null,
                 vendorPart: null,
                 salesOrderLineItemId: local!id,
                 bomLineItemId: local!bomId
               )
              )/*end if */
           )/*end load*/
         }
       )/*end bom line item for loop */
     ) /*end load */

    )

     

    So basically I need to find all parts using the local variable, and either give a section layout (OrderPartSummary) or a list of OrderPartSummary

    currently this gives me 5 order part summaries, when there should be 11 (two assemblies each having 4 items but only showing the first)

  • 0
    Appian Employee
    in reply to tylers358

    Given that it's interfaces, you likely do not need the dictionary. (first: , second:) (the dicstionary was a suggestion on how to preserve data nesting)

    By looking at your script, something like this should work. 

    a!forEach(
      items: local!data,
      expression: 
        with(
          local!id: fv!item.sales,
          a!forEach(
            items: fv!item.li,
            expression: {
              with(
                local!bomId: fv!item.id,
                local!item: fv!item,
                if(
                  fv!item.recordTypeId = 5,
                  a!forEach(
                    items: rule!MRP_GetAllPartsForAssembly(local!item.recordId),
                    expression: {
                      rule!MRP_OrderPartSummary(
                        part: rule!MRP_GetPartById(fv!item),
                        partPrice: null,
                        vendorPart: null,
                        salesOrderLineItemId: local!id,
                        bomLineItemId: local!bomId
                      )
    
                    }
               ),
               rule!MRP_OrderPartSummary(
                 part: rule!MRP_GetPartById(fv!item.recordId),
                 partPrice: null,
                 vendorPart: null,
                 salesOrderLineItemId: local!id,
                 bomLineItemId: local!bomId
               )
              )/*end if */
           )/*end load*/
         }
       )/*end bom line item for loop */
     ) /*end load */
    
    )

  • For some reason this still gives me 5 section layouts. removing the a!flatten, I still see a list of 1 where I expect a list of 4.

    See the result here:

    List of Variant: 2 items

       List of Variant: 3 items

           SectionLayout2 (System Type)

           SectionLayout2 (System Type)

           List of SectionLayout2: 1 item

               SectionLayout2 (System Type)

       List of Variant: 2 items

           SectionLayout2 (System Type)

           List of SectionLayout2: 1 item

               SectionLayout2 (System Type)

    I have tried separating out the function, and getAllPartsForAssembly does return a list of {1,2,1,3} but it is only running the for loop once. This seems to be the key issue, can you think of any reason why the for loop doesn't even try the last 3 elements?

  • 0
    Appian Employee
    in reply to tylers358
    Is it the inside or outside for loop?
  • The flatten was around the entire main for loop, it didn't change much, I removed it to show that the list was length 1. The result with the flatten was 5 section layouts. I am currently approaching this a different way, trying to get a list of the part ids themselves, so the interfaces don't have to be used in such a complicated way. I can't seem to figure out why the list returned is only length 1 when the for loop itemCount is 4.

    I will leave the question open if someone else has this issue, but thanks for your help Domen
Reply
  • The flatten was around the entire main for loop, it didn't change much, I removed it to show that the list was length 1. The result with the flatten was 5 section layouts. I am currently approaching this a different way, trying to get a list of the part ids themselves, so the interfaces don't have to be used in such a complicated way. I can't seem to figure out why the list returned is only length 1 when the for loop itemCount is 4.

    I will leave the question open if someone else has this issue, but thanks for your help Domen
Children
No Data