Clean up NULL values and copy good values on another Array CDT - a!forach loop with append() & inssert()

Hi Community,

I am struggling with a simple expression rule.

I tried with append, insert and other functions, but i didn't manager. I expect those functions to return the previous array + the new addition.

I would like to generate a second array without "null" values of my CDT type, so I have an array only with proper data.

For that I did below expression rule:

 

load(
  local!itemsWithNull:{{null, null, 'type!{urn:com:appian:types}MyData'('uId': 5, 'name': "changed", 'symbol': "changed", 'market': "aa", 'isActive': false), null, null, null, null, 'type!{urn:com:appian:types}MyData'('name': "new", 'symbol': "new")}},
  local!itemsClean:{'type!{urn:com:appian:types}MyData'()},

  
  {a!forEach(
    items: (local!itemsWithNull),
    expression: {
      if(
        isnull(fv!item),
         fv!index & " is null",
         {
           "ADDED" & append(local!itemsClean,fv!item) & "END ADDED",
         }

        ) 
    }
      
  ),
  reverse(local!itemsClean),
  }

)

 

On my first iteration, I expect to get something like this (currently the expression rule is kind of in my debug mode :) )

 

FIRST ITERATION

 

"ADDED[uId=5, name=changed, symbol=changed, market=aa, isActive=false]END ADDED"

However, I have something like this:

 

"ADDED[uId=, name=, symbol=, market=, isActive=]; [uId=5, name=changed, symbol=changed, market=aa, isActive=false]END ADDED

 

 

SECOND ITERATION:

On my second iteration of valid data, I would expect to have a result like:

"[uId=5, name=changed, symbol=changed, market=aa, isActive=false]; [uId=, name=new, symbol=new, market=, isActive=]END ADDED"

However, I get something like this:

(the new data is present, but i lost my previous one)

"ADDED[uId=, name=, symbol=, market=, isActive=]; [uId=, name=new, symbol=new, market=, isActive=]END ADDED"

 

This is how my output looks like: 

 


List of Variant: 9 items
    List of Text String: 1 item
        "1 is null"
    List of Text String: 1 item
        "2 is null"
    List of Text String: 1 item
        "ADDED[uId=, name=, symbol=, market=, isActive=]; [uId=5, name=changed, symbol=changed, market=aa, isActive=false]END ADDED"
    List of Text String: 1 item
        "4 is null"
    List of Text String: 1 item
        "5 is null"
    List of Text String: 1 item
        "6 is null"
    List of Text String: 1 item
        "7 is null"
    List of Text String: 1 item
        "ADDED[uId=, name=, symbol=, market=, isActive=]; [uId=, name=new, symbol=new, market=, isActive=]END ADDED"
    MyData
        uId: null (Number (Integer))
        name: null (Text)
        symbol: null (Text)
        market: null (Text)
        isActive: null (Boolean)

 

Thanks very much in advance for any recommendation! 

 

Best regards,

Manuel

 

 

 

 

  Discussion posts and replies are publicly visible

  • Hi Manuel,

    if you want to remove just null values from array what about using this expression?

     

    load(
        local!itemsWithNull:{{null, null,'type!{urn:com:appian:types}MyData'('uId': 5, 'name': "changed", 'symbol': "changed", 'market': "aa", 'isActive': false), null, null, null, null, 'type!{urn:com:appian:types}MyData'('name': "new", 'symbol': "new")}},
    
        local!itemsClean: reject(fn!isnull,local!itemsWithNull),
        
        reverse(local!itemsClean)
    )

  • 0
    A Score Level 1
    in reply to erikm192
    Thank you very much! it is way more simple than the way i was doing, did not know about the reject function!.

    Anyways, I will still want to do more operations with the arrays (besides the clean-up). Any idea why in my loop append is not doing what I am expecting?
  • +1
    A Score Level 1
    in reply to ManuelHTG

    Manuel,

    1st problem:

    Initialization of local!itemsClean. If you put there 'type!{urn:com:appian:types}MyData'() it means that there is already empty object. Thats why in first and second iteration of valid data you see first object empty. It is enough if you set it like local!itemsClean: {}

    2nd problem

    If you want to update other variables with some functions you have to do it from saveInto, so it means for example on button click. However if you want to filter array or do anything else you can do it directly on local variable. For your case like this:

    local!itemsClean: a!forEach(
        items: local!itemsWithNull,
        expression: {
          if(
            isnull(fv!item),
               {},
               fv!item
            ) 
        }
      )

     

    What you see as output from your expression is output from forEach. Append, forEach and all appian functions (except a!save() ) are just returning text or other values, not doing any direct save. 

     

    Is it clearer now or do you have more questions?

    Erik

  • 0
    A Score Level 1
    in reply to erikm192

    it is much clear thanks.

    For the second, append returns the new array, so i was expecting to give me on the second non-null iteration the new array.

    The problem that I find with a!save() is that it is not clear how should I use it when what I want is to add a new array of my CDT type and my understanding was that the functions like append was adding the CDT and returning me the new array.

     

    https://docs.appian.com/suite/help/18.4/fnc_array_append.html

    "

    Append one item to an array:

    append({10, 20, 30, 40}, 50) returns 10, 20, 30, 40, 50

    Append an array to an empty array:

    append({}, {60, 70}) returns 60, 70

    Append one array to another:

    append({10, 20, 30, 40}, {50, 60, 70}) returns 10, 20, 30, 40, 50, 60, 70

    Append multiple arrays to an original array:

    append({"Red", "Green"}, {"Blue", "Yellow"}, {"Brown", "White"}) returns Red, Green, Blue, Yellow, Brown, White

    "

     

     

    .

  • hey,

    I am facing the same problem 1. Could you please elaborate more on it?

  • The trick with Appian being a functional language is that these functions return a modified copy of the array instead of modifying the original array and returning a reference. This is not Java.