Skip to main content
Inspiring
Aug 25, 2021 5:53 AM UTC
Solved

Update the sort ids if i am adding row in between existing rows .

Grid rows are shown on the based of sort id ,  I have implemented add button  in the grid just to add the new rows in between the existing rows, i have to update sort IDs if i am adding one or more than one row in between existing rows.

For an Example if  1,2,3,4 sort id's are already present and i am adding new row in 2nd index then existing 2 row id should change to 3 and remaining should increment . Where sort id is not primary key But the question id is .

a!imageField(
label: "Add " & ri!index,
images: a!documentImage(
document: a!iconIndicator("ADD"),
altText: "Add Question",
caption: "Add Question Here",
link:
a!dynamicLink(
label: "Add Question",
value: 'type!{urn:com:appian:types:Exit}Exit_Question'(

entityTypeId:"",
activeFlag: true,
sortId:ri!questions[ri!index].sortId+1,
contentAreaId: ri!contentAreaId,
createdBy: loggedInUser(),
createdDt: now(),
modifiedBy: loggedInUser(),
modifiedDt: now(),
questionIsRestricted: true
),
saveInto: {

a!save(
ri!questions,
insert(
ri!questions,
save!value,
ri!index+1
)
),

}

),

)
)

Above code just change the sort id of ne row but doesn't update the existing ones.

selvakumark
Participating Frequently
Aug 25, 2021 6:32 AM UTC

Hi,

We need to update the sort Ids of the other fields in the dictionary like below(this is just a simple method out of many):

a!imageField(
  label: "Add " & ri!index,
  images: a!documentImage(
    document: a!iconIndicator("ADD"),
    altText: "Add Question",
    caption: "Add Question Here",
    link: a!dynamicLink(
      label: "Add Question",
      value: 'type!{urn:com:appian:types:Exit}Exit_Question'(
        entityTypeId: "",
        activeFlag: true,
        sortId: ri!questions[ri!index].sortId + 1,
        contentAreaId: ri!contentAreaId,
        createdBy: loggedInUser(),
        createdDt: now(),
        modifiedBy: loggedInUser(),
        modifiedDt: now(),
        questionIsRestricted: true
      ),
      saveInto: {
        a!save(
          ri!questions,
          a!forEach(
            items: ri!questions,
            expression: if(
              tointeger(fv!item.sortId) <= tointeger(
                index(ri!questions[ri!index], "sortId", null())
              ),
              fv!item,
              updatedictionary(
                fv!item,
                { sortId: tointeger(fv!item.sortId) + 1 }
              )
            )
          )
        ),
        a!save(
          ri!questions,
          insert(ri!questions, save!value, ri!index + 1)
        )
        
      }
    )    
  )
)

Hope this helps!

2 replies

selvakumark
Participating Frequently
Aug 25, 2021 6:32 AM UTC

Hi,

We need to update the sort Ids of the other fields in the dictionary like below(this is just a simple method out of many):

a!imageField(
  label: "Add " & ri!index,
  images: a!documentImage(
    document: a!iconIndicator("ADD"),
    altText: "Add Question",
    caption: "Add Question Here",
    link: a!dynamicLink(
      label: "Add Question",
      value: 'type!{urn:com:appian:types:Exit}Exit_Question'(
        entityTypeId: "",
        activeFlag: true,
        sortId: ri!questions[ri!index].sortId + 1,
        contentAreaId: ri!contentAreaId,
        createdBy: loggedInUser(),
        createdDt: now(),
        modifiedBy: loggedInUser(),
        modifiedDt: now(),
        questionIsRestricted: true
      ),
      saveInto: {
        a!save(
          ri!questions,
          a!forEach(
            items: ri!questions,
            expression: if(
              tointeger(fv!item.sortId) <= tointeger(
                index(ri!questions[ri!index], "sortId", null())
              ),
              fv!item,
              updatedictionary(
                fv!item,
                { sortId: tointeger(fv!item.sortId) + 1 }
              )
            )
          )
        ),
        a!save(
          ri!questions,
          insert(ri!questions, save!value, ri!index + 1)
        )
        
      }
    )    
  )
)

Hope this helps!

Inspiring
Aug 25, 2021 7:14 AM UTC

Yea this is also fine , we can do like this ,just to update sort id by using function update disctionary

a!imageField(
label: "Add " & ri!index,
images: a!documentImage(
document: a!iconIndicator("ADD"),
altText: "Add Question",
caption: "Add Question Here",
link:
a!dynamicLink(
label: "Add Question",
value: 'type!{urn:com:appian:types:GID}GID_Question'(

entityTypeId:"",
activeFlag: true,
contentAreaId: ri!contentAreaId,
createdBy: loggedInUser(),
createdDt: now(),
modifiedBy: loggedInUser(),
modifiedDt: now(),
questionIsRestricted: true
),
saveInto: {

a!save(
ri!questions,
insert(
ri!questions,
save!value,
ri!index+1
)
),

a!save(
ri!questions,
a!forEach(
items: ri!questions,
expression: updatedictionary(
dictionary: fv!item,
fieldsAndValues: {
sortId: fv!index

}
)
))





}

),

)
)