How to convert array into single value against a single key

Certified Lead Developer

I've CDT which has data like {ID:1, Val: "A,B,C}. Now I want to store the values in another CDT and write to DB where I want to save them in manner like {ID:1, Val:"A", ID:1, Val:"B",ID:1, Val:"C"}. What can be done to achieve this.

Regards:
Nitesh

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    You can't have multiple properties with the same name (i.e. "Val") in a CDT, nor can you really have a variable number of properties (though there's nothing keeping you from adding the maximum number that you'll ever likely need).  They would need unique names, like {val1, val2, val3} etc.

    If the list of "Val" is going to be completely variable then you might want to consider making an association table where you add a new row for each one, where each row has a reference to the "ID", and a reference to the "Val" value.  This table would also need its own primary key, separate from the original "ID".

    But at this point your use case is not clear enough and it's getting to be too much guesswork to say anything specific about it without potentially going in the wrong direction.  Is there any more detail you can provide as to the nature of your input data and what you need it transformed into?  Would "A,B,C" be literally a comma-separated single text value, or would it be an array, for example?  How long would it get?  These are just a few points of clarification needed to really be helpful for you.

  • 0
    Certified Lead Developer

    If I still remember my good ol' merge() function, which I haven't had to much use since a!forEach, merge(1, {A, B, C}) should result in {{1, A},{1, B},{1, C}}

    Does that lead you in the direction you want to go?

  • 0
    Certified Lead Developer
    in reply to Dave Lewis

    Or does merge(1, {A,B,C}) just return {{1,A},B,C}?

  • 0
    Certified Lead Developer

    No!  I realize what you need to do!  Specifically because you want to write to database.

    If I understand your question correctly you have:

    {

    ID: 1,

    Val: "A, B, C"

    }

    and you want A, B, C to each get their own row with a 1 foreign key?  Just split the string. Use split() on "A,B,C" with the comma as your separator, and you should wind up with:

    {

    ID: 1

    Val: {"A";"B";"C"}

    }

    And Appian should automatically save that in DB how you would want it to be saved.