Adding rows to cdt

Hi Everyone,

I am getting the cdt rows through record type using expression rule. Is it possible to add more unique rows to cdt through expression rule. If yes, can anyone please help me out?

  Discussion posts and replies are publicly visible

  • 0
    Certified Senior Developer

    Hello  

    What do you mean by this?

    I am getting the cdt rows through record type using expression rule.

    Are you saying that you are casting the data to RecordType?

    Try out the below code. As per my understanding, You are querying data from CDT and casting it to a recordType and using it in a grid. In the same grid you want to edit or add the data and cast the data to CDT and save it using write to datastore entity. If yes, then this is the code.

    a!localVariables(
      local!data: rule!yourQueryOfCDTToRecordData(),
      local!toCDT: a!forEach(
        items: local!data,
        expression: type!CDT(
          field1:"value",
          field2:"value",
          .
          .
          fieldn:"value"
        )
      ),
      {
        a!gridLayout(
          label: "Editable Grid",
          labelPosition: "ABOVE",
          headerCells: {
            a!gridLayoutHeaderCell(label: "Header Cell"),
            a!gridLayoutHeaderCell(label: "Header Cell"),
            a!gridLayoutHeaderCell(label: "Header Cell"),
            a!gridLayoutHeaderCell(label: "Header Cell"),
            a!gridLayoutHeaderCell(label: "Header Cell")
          },
          columnConfigs: {
            a!gridLayoutColumnConfig(width: "DISTRIBUTE"),
            a!gridLayoutColumnConfig(width: "DISTRIBUTE"),
            a!gridLayoutColumnConfig(width: "DISTRIBUTE"),
            a!gridLayoutColumnConfig(width: "DISTRIBUTE"),
            a!gridLayoutColumnConfig(width: "DISTRIBUTE")
          },
          rows: {
            a!forEach(
              items: local!data,
              expression: {
                a!gridRowLayout(
                  contents: {
                    a!textField(
                    ),
                    a!textField(
                    ),
                    a!textField(
                    ),
                    a!textField(
                    ),
                    a!textField(
                    )
                  }
                )
              }
            )
          },
          addRowLink: a!dynamicLink(
            label: "add Row",
            value: null,
            saveInto: a!save(
              local!data,
              append(
                local!data,
                recordType!yourRecordType()
              )
            )
          ),
          selectionSaveInto: {},
          validations: {},
          shadeAlternateRows: true
        ),
        a!buttonArrayLayout(
          buttons: a!buttonWidget(saveInto: a!save(ri!myCDT, local!toCDT))
        )
      }
    )

  • 0
    Certified Lead Developer

    We need more context to answer your question. A CDT is a data structure and does not have any "rows".

  • From what I understood, you can use the append() function and add as many rows as you want in the dataset queried from the record type. 

  • 0
    Certified Lead Developer

    Not very clear on the ask, but if you are holding CDTs into a variable (array), you can use append() function as you use to add an element to an array. 

  • 0
    Certified Senior Developer

    Hi ,

    you are currently retrieving existing CDT data via a record type using an expression rule.. To add new unique rows to this CDT, you can use the append() function within an expression rule to add new data to your existing dataset.

    Steps:

    1. Retrieve Existing Data: Use an expression rule to get the current rows of data from the CDT through the record type. Let's assume this data is stored in a local variable called local!existingData.

    2. Create New Row Data: Define the new row data that you want to add to your CDT. This should match the structure of your CDT. For example:

                 local!newRow := {
                 field1: "New Value 1",
                 field2: "New Value 2",
                 ...
                 fieldN: "New Value N"
                 }

              3.Add Row to Existing Data: Use the append() function to add your new row to the existing dataset:

              local!updatedData := append(local!existingData, local!newRow)

             Save it.

    Example:

    a!localVariables(
    local!existingData: /* Your expression to retrieve existing CDT data */,
    local!newRow: /* Define the new row data here */,
    local!updatedData: append(local!existingData, local!newRow),
    {
    /* Your interface elements go here, such as a grid or form to display local!existingData */
    /* Use a button or another interface component to trigger the save of local!updatedData */
    }
    )