Updating data in a nested CDT

 Hi, 

In a CDT, I have another nested CDT. I would like to update the nested CDT. How can I do this with a!writeToDataStoreEntity while using the main CDT's primary ID? 

      addRowLink: a!dynamicLink(
        label: "Add question",
        value: {},
        saveInto: {
          a!writeToDataStoreEntity(
            dataStoreEntity: cons!CHR_ENTITY_ASSESSMENT_SEGMENT,
            valueToStore: 'type!{urn:com:appian:types:CHR}CHR_AssessmentSegment'(),
            onSuccess: a!save(
              target: ri!segment.questions,
              value: append(ri!segment.questions, fv!storedValues)
            )
          )
        }
      )

In my database, it is creating a row in the main CDT rather than the nested CDT. I am not sure what the best way to refer to the nested CDT is... 

Your help is much appreciated.

Kind regards,

Eric

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer
    Some like to use the index() function, I prefer to be very explicit about what I'm doing using the property() function, or at times dot notation. For instance, myDataType.id or myDataType.name.

    You could do index(mainCDT, mainCDT_id, 0).nestedCDT should give you one item of type nestedCDT corresponding to that row.
    Or you could try property(mainCDT, nestedCDT, {}) which should return a list of all the nestedCDT objects inside the list of mainCDT.

    To do the database write you're going to have to make sure you get the row id of mainCDT squared away, then reconstruct all the other data for it, because if you plug in the id for the mainCDT, it should perform a SQL UPDATE statement rather than an INSERT statement. Any data that doesn't match what was already on that row for mainCDT gets overwritten with the new stuff. I believe if you have a brand new nestedCDT type in the definition of the updated row, and it can't find the ID for the nestedCDT, it will automatically do a SQL INSERT into the nestedCDT table to complete the UPDATE to the mainCDT table.

    Another option might be to split the whole thing up into 2 database writes. Save the nestedCDT to its own table first, use the outputs on the write to datastore node to capture the stored values to get the ID, update the mainCDT row in a script task with the newly captured nestedCDT Id, then perform another write to datastore to update the mainCDT's nestedCDT id.