Editable Grid - Need a solution to update sort id's

Certified Associate Developer

Hi Everyone ,

Need your help in solving below problem . 

I have a requirement where i can insert data in between any rows but Type will always be Manager.  When i add new row, i need to generate a new id and want to update/increment sort id's  of all the rows which has Type as Manager accordingly.

For example - I added a row after id = 3 or at 4th place  so my new row will have id = 6 , Type = Manager and sort id = 2 . Also, sort id for last row which has Type='Manager' gets updated with 3 .

Thanks in advance

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    I typically add a saveInto to the submit button which updates the list accordingly using a foreach. Do not create a list of a!save(), but have a single a!save() which updates the list at once. In the expression part you can add any logic you need.

  • 0
    Certified Associate Developer
    in reply to Stefan Helzle

    Hi Stefan,

    Thankyou for your reply . 

    I wrote this sample code and it works fine when i click on rows which has type = "Manager" but it doesn't seems to be working when type is something else . Is there any way where i can split my CDT based on index and then take max  sort id of first set . Or is there any better way to achieve this . 

      a!imageField(
          label: "Add " & ri!index,
          images: a!documentImage(
            document: a!iconIndicator("ADD"),
            altText: "Add Employee",
            caption: "Add Employee Here",
            link: a!dynamicLink(
              label: "Add Employee",
              value: 'type!{urn:com:appian:types:Employee}AS_Employee'(
                employeeTag :tostring(1),
                type: "Manager",
                activeFlag: true,
                createdBy: loggedInUser(),
                createdDt: now(),
                modifiedBy: loggedInUser(),
                modifiedDt: now(),
                sortId: if(
                  rule!APN_isEmpty(ri!employees[ri!index].sortId),
                  {},
                  sum(ri!employees[ri!index].sortId, 1)
                )
              ),
              saveInto: {
                a!save(
                  ri!employees,
                  insert(ri!employees, save!value, ri!index + 1)
                ),
                a!save(
                  ri!employees,
                  a!forEach(
                    items: ri!employees,
                    expression: updatedictionary(
                      dictionary: fv!item,
                      fieldsAndValues: if(
                        and(
                          fv!item.type = "Manager",
                          fv!index > ri!index + 1
                        ),
                        {
                          sortId: if(
                            rule!APN_isEmpty(fv!item.sortId),
                            "",
                            sum(fv!item.sortId, 1)
                          )
                        },
                        {}
                      )
                    )
                  )
                )
              
               
              }
            )
            
          )
        ),

  • 0
    Certified Lead Developer
    in reply to Ash

    Your code looks good. Just replace your hard-coded value "Manager" with ri!employees[fv!index].type

    a!imageField(
      label: "Add " & ri!index,
      images: a!documentImage(
        document: a!iconIndicator("ADD"),
        altText: "Add Employee",
        caption: "Add Employee Here",
        link: a!dynamicLink(
          label: "Add Employee",
          value: 'type!{urn:com:appian:types:Employee}AS_Employee'(
            employeeTag: tostring(1),
            type: ri!employees[fv!index].type,
            activeFlag: true,
            createdBy: loggedInUser(),
            createdDt: now(),
            modifiedBy: loggedInUser(),
            modifiedDt: now(),
            sortId: if(
              rule!APN_isEmpty(ri!employees[ri!index].sortId),
              {},
              sum(ri!employees[ri!index].sortId, 1)
            )
          ),
          saveInto: {
            a!save(
              ri!employees,
              insert(ri!employees, save!value, ri!index + 1)
            ),
            a!save(
              ri!employees,
              a!forEach(
                items: ri!employees,
                expression: updatedictionary(
                  dictionary: fv!item,
                  fieldsAndValues: if(
                    and(
                      fv!item.type = ri!employees[fv!index].type,
                      fv!index > ri!index + 1
                    ),
                    {
                      sortId: if(
                        rule!APN_isEmpty(fv!item.sortId),
                        "",
                        sum(fv!item.sortId, 1)
                      )
                    },
                    {}
                  )
                )
              )
            )
          }
        )
      )
    ),

  • 0
    Certified Associate Developer
    in reply to vimalkumars

    Hi Vimal ,

    I am hardcoding  "Manager" because my requirement is that user is allowed only to create managers data not for other types . This is where i am stuck and looking for solution .

  • 0
    Certified Lead Developer
    in reply to Ash

    Is there a reason why you use a mix of fv!index and ri!index? What is ri!index meant to do? I suggest to simply use fv!index as sortId.

  • 0
    Certified Lead Developer
    in reply to Ash

    Understood. This one should help.

    a!imageField(
                label: "Add " & ri!index,
                images: a!documentImage(
                  document: a!iconIndicator("ADD"),
                  altText: "Add Employee",
                  caption: "Add Employee Here",
                  link: a!dynamicLink(
                    label: "Add Employee",
                    value: a!map(
                      id: length(ri!employees) + 1,
                      employeeTag: tostring(1),
                      type: "Manager",
                      activeFlag: true,
                      createdBy: loggedInUser(),
                      createdDt: now(),
                      modifiedBy: loggedInUser(),
                      modifiedDt: now(),
                      sortId: if(
                        rule!ALOG_isNullOrEmpty(ri!employees[ri!index].sortId),
                        {},
                        sum(ri!employees[ri!index].sortId, 1)
                      )
                    ),
                    saveInto: {
                      a!save(
                        ri!employees,
                        insert(ri!employees, save!value, ri!index + 1)
                      ),
                      a!save(
                        ri!employees.sortId,
                        a!localVariables(
                          local!managerIndices: wherecontains("Manager", ri!employees.type),
                          updatearray(
                            ri!employees.sortId,
                            local!managerIndices,
                            enumerate(count(local!managerIndices)) + 1
                          )
                        )
                      )
                    }
                  )
                )
              )