How to convert CDT list into List of Varient

I have a List of Data of type CDT, i wanted to convert that into List of varient to use that data in A!forEach() ??? 

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    Considering that a!forEach() can act on any list type (including list of CDT), i'm not sure why you would want to do this - can you explain a bit more about your use case?

  • 1.      In my process model I have a process variable of type 

    type!MB_Medicine__Cost_Data_fromExcel (urn:com:appian:types:mb) id, name, perCost, quantity, totalCost

    id (Number (Integer))

    name (Text)

    perCost (Number (Integer))

    quantity (Number (Integer))

    totalCost (Number (Integer))

    2.     Inside this I already have data stored. Assume a list of 5.

            Now what I need to do is I need to find the name: "Cipla" and wanted to change its perCost. 

           So, I have created a rule expression and passed my variable into that rule using rule input.

    3.     a!localVariables(
            local!dataInput:ri!oldData,
            local!finalUpdate:a!forEach(
            items: local!dataInput,
            expression: a!localVariables(
            local!checking:"cipla",
           'type!{urn:com:appian:types:mb}MB_Medicine__Cost_Data_fromExcel'(
           name: if(fv!item[1]=local!checking,"ciplex",fv!item[1]),
           perCost: fv!item[2],
           quantity: fv!item[3],
           totalCost: fv!item[4],
         )
         )
         ),
         local!finalUpdate
        )

    4. In my rule Exp . ri!oldData contains the data of type!MB_Medicine__Cost_Data_fromExcel

    5. So , it is not parsing in foreach and gives error

    6. I also used tojson() and fromjson() but it is also converting into dictionary

    I need to convert ri!OldData into List of varient so i can parse it and change the data of cipla only ??

  • you can also suggest some other ways or shortcut to make it done

  • 0
    Certified Lead Developer
    in reply to akshays0003

    my initial feedback is that you're misusing "fv!item[1]" (etc).  Each "fv!item" will be a single instance of your CDT but your square-bracket index calls make Appian want to treat "fv!item" like an array -- which it isn't.  So you'll need to fix that before this expression will work, regardless of the data type.

  • How can we parse 2-D array then? . In my case the variable contains:

    [id=, name=pfizer, perCost=100, quantity=2, totalCost=200],[id=, name=cipla, perCost=120, quantity=2, totalCost=240],[id=, name=beta, perCost=90, quantity=1, totalCost=90],[id=, name=pathlabs, perCost=88, quantity=4, totalCost=352],[id=, name=nlabs, perCost=30, quantity=3, totalCost=90],[id=, name=cx100, perCost=230, quantity=2, totalCost=460],[id=, name=, perCost=, quantity=, totalCost=1432]

    ----------------------------------------------------------------------------------------------------------

    I have load this data from excel with method below and it is working fine:

          a!localVariables(
             local!excelFile:readexcelsheet(
             excelDocument:ri!docId,
             sheetNumber:0,
             startRow:5
          ).result,
          local!excelinArray:a!flatten(local!excelFile),
          local!finalOut:a!forEach(
             items: local!excelinArray.values,
             expression:'type!{urn:com:appian:types:mb}MB_Medicine__Cost_Data_fromExcel'(
             name: fv!item[1],
             perCost: fv!item[2],
             quantity: fv!item[3],
             totalCost: fv!item[4]
          )
         ),
         local!finalOut

    )

    -------------------------------------------------

    the problem comes after this when i want to change some values

  • 0
    Certified Lead Developer
    in reply to akshays0003

    The following expression should work if your intention is to find any item where the name is "cipla" and update the "perCost" of those new entry(ies).  You will need to install whatever logic you need to determine the new "perCost" value.  If you're really out to change the value of a different field (you seem to mention two conflicting things in your original post), you would simply need to change the a!update() values.  Note: if you're not yet in 21.2, a!update() will not be available but you can simply use updateDictionary() which is available in a plug-in.

    a!localVariables(
      local!dataInput: ri!oldData,
      local!finalUpdate: a!forEach(
        items: local!dataInput,
        expression: a!localVariables(
          local!checking: "cipla",
          if(
            fv!item.name = local!checking,
            
            /* for items where the name is the checked-for value, update the item to new value(s) */
            a!update(
              data: fv!item,
              index: "perCost",
              value: 333 /* update perCost value here */
            ),
            
            /* for items where the name isn't the checked value, just return the item unchanged */
            fv!item
          )
          
          /*'type!{urn:com:appian:types:mb}MB_Medicine__Cost_Data_fromExcel'(*/
            /*name: if(fv!item[1]=local!checking,"ciplex",fv!item[1]),*/
            /*perCost: fv!item[2],*/
            /*quantity: fv!item[3],*/
            /*totalCost: fv!item[4],*/
          /*)*/
        )
      ),
      local!finalUpdate
    )

  • 0
    Certified Lead Developer
    in reply to akshays0003

    BTW, it's 20x easier to read code snippets if you use a Code Box (Insert --> Insert Code), as this preserves indentation and uses a monospace font and (if you select "Java" from the type dropdown) gives a little bit of syntactic highlighting.  (I have no idea why "Appian SAIL" or something similar still isn't an option there.)

  • 0
    Certified Lead Developer
    in reply to akshays0003
    I have load this data from excel with method below and it is working fine:

    I don't really know why this would be working at all - the square bracket indexes would not work at all as far as I know, unless they accidentally refer to the correct dot properties by position in this context, which I didn't know was possible (and even if it is seemingly working, I would NEVER recommend coding it this way).

  • Hi Mike, This works fine. But don't know why it only returns only first data from an array of 5. 

    Only return this : [id=, name=pfizer, perCost=100, quantity=2, totalCost=200] everytime 

  • I have checked it. It was working only when I use my CDT type in expression in foreach.

    I have also tried one more thing when I don't use type! in expression. It just simply returns Type:List of variant and not storing any data in the process variable. fails the process earlier than before

    So, I left with no other option instead of using type! in expression and brackets with fv!item