How to update the ids in grid of specific rows?

in the editable grid i am having i am displaying Questions and some other values on the basis of Sort id , Where sort ID is not primary key , i have also provided  button to add new rows in between existing rows . below code works fine and increment the values automatically if new row gets added in between 

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:Test}Test_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}
)
)
),
a!save(local!test,true())
,


}
),

)
)

But this code fails beacuse there is a  dropdown field  in grid named entity type having value ("Null", Provisonal ,"Production")  when we are adding new row ,increment should happens only for the rows which are having enity type id null . if other values like "Provisional" and "production" for them sort id should not change or increment. 

(This scenerio is not happening, i know there will be duplicates in ID for null entity type and provisional and production)

  Discussion posts and replies are publicly visible