Conditional write to multiple data store entities

I have a situation like this with saving data to database:

- in employee update process user can change employee's personal and address data

- address is joined to employee via foreign key addressId on employee entity

- if address is not chosen from codebook it goes on approval to "address admin"

- id address admin approves new address it should be added to the codebook

- this means following: If user updated personal data and added new address (which is approved in the next step), data will be stored to employee table and to address codebook table. If user updated personal data and added existing address, only employee data should be stored to employee table.

How can I configure this conditional save using Write to Multiple Data Store Entities?

Am I allowed to use expression editor to configure IF statement? Is this statement below correct (if I assume I have process variable addressAddedFlag)?

if (pv!addressAddedFlag = true,

{{entity:cons!EEDM_EmployeeDataStoreEntity, data:pv!employee},{entity:cons!EEDM_AddressDataStoreEntity, data:pv!address}},

{{entity:cons!EEDM_EmployeeDataStoreEntity, data:pv!employee},{entity:cons!EEDM_AddressDataStoreEntity, data:{} }}

)

  Discussion posts and replies are publicly visible

  • Of course you're 'allowed' to write a condition to choose what your W2MDSE actually writes to the DB. A couple of things worth mentioning:

    1. you do not need to write if(pv!addressAddedFlag = true - assuming pv!addressAddedFlag is a Boolean you can simply write if(pv!addressAddedFlag  because it is either true or false - there's no need to compare it to the literal 'true'
    2. I would encourage you to encapsulate the expression as rule in its own right because you can test and maintain it independently from the process model you're using it in. It also means you can attach Unit Tests to the Expression and thus validate it still behaves as expected if/when you change it
    3. I believe your second write statement will either fail (if the address table has any non-null attributes) or write an empty row, which I suspect is not what you want - I think you could just leave out the address entity and only have it write the employee entity

    Hope this helps

  • Thx, this is what I needed.

    I've used this empty curly brackets notation for address in second part of the IF statement because I've read somewhere on Appian Community that this is the way how to ommit inserting into table. Looks like I understood it wrongly because it was said that empty brackets won't be treated as "insert empty row" but as "skip insert into this table".

    Also are your advices and my "configuration point" generally "the best practice" to configure conditional inserts or there is something more sofisticated that would be good for me to learn?

  • I think (!) the best way to omit writing something to a table is just not to write it! But I'm curious as to why you just don't try it out? Have a go, see what happens...that's the beauty of Appian, you can try something out in 2 minutes and see what works, what doesn't and learn from the experience!! 

  • +1
    Certified Lead Developer
    in reply to Stewart Burchell
    encapsulate the expression as rule in its own right

    And also, formatting won't be lost every time the input field is saved and closed (because for some reason it's never occurred to them that 1-line expressions are no longer sufficient under most circumstances Rolling eyes)