Skip to main content
January 6, 2021
Solved

Problem of saving updated grid field in database

  • January 6, 2021
  • 11 replies
  • 0 views

Here is my code. I have one editable grid in which one priority field is there:

a!integerField(
label: "Priority",
labelPosition: "ABOVE",
value: if(not(isnull(fv!item.priority)),int(fv!item.priority)-rule!CRP_CheckPriority(fv!item.priority,fv!item.contentdomain),fv!item.priority),
/*saveInto: a!save(fv!item.priority,if(rule!CRP_CheckPriority(fv!item.priority,fv!item.contentdomain)>0,a!save(save!value,int(fv!item.priority)-rule!CRP_CheckPriority(fv!item.priority,fv!item.contentdomain)),save!value)), */
saveInto: {a!save(fv!item.priority,save!value)
/*if(rule!CRP_CheckPriority(fv!item.priority,fv!item.contentdomain)>0,a!save(fv!item.priority,int(fv!item.priority)-rule!CRP_CheckPriority(fv!item.priority,fv!item.contentdomain)),a!save(fv!item.priority,save!value))*/
},
refreshAfter: "UNFOCUS",
readOnly: if(local!gridreadonly="Edit",true,false),
validations: {if(and(local!gridreadonly="Save",not (isnull(fv!item.priority))),{if(count(rule!CRP_GetPriority(priority: fv!item.priority,contentdomain: fv!item.contentdomain,id:fv!item.id)) >0,"This Priority is already assigned to other Project..",null)},{}),
if(and(not (isnull(fv!item.priority)),int(fv!item.priority)>10),"Priority must be between 1 and 10","")}

),

Here one of the value of priority field of grid fv!item.priority  is 10 which is in database . when the grid is displayed it shows value 5 that is right as per my code in value line. But when i save the data it stores 10 not 5. but i want 5 to be stored in database. Tell me solution. what is the wrong in my code.

Please reply asap.

Best answer by peter.lewis

Ok this looks ok to me - how are you then saving the data to the database? Do you have a button that on save transfers the values from local!dataUpdated to a rule input?

11 replies

peter.lewis
Employee
January 6, 2021

One important thing to know about SAIL is that the saveInto is only triggered if you interact with the field. So even if the value displays as 5, that value doesn't get saved upon submit on the form unless you typed something into the field.

Usually in cases like this, I'd suggest one of two other approaches:

  • Define the value on load of the page rather than in the value section - this can be done by setting the initial value of the data equal to the value you want either by setting the data in a local variable or passing data into your rule input via a parameter.
  • Use the submit button to update the results - you can be confident that the submit button will be interacted with, so this gives you an opportunity to save your default value of "5" in any fields that don't currently have a value.
January 7, 2021

your first approach is good but this value is if one of the field of grid. so it's in foreach loop. So how can i use local variable where i want each row value in rule input of expression rule.

peter.lewis
Employee
January 7, 2021

You have to define the data before you even get to the grid. Here's an example: 

a!localVariables(
  local!data: {
    a!map(
      id: 1,
      name: "Test",
      value: ""
    ),
    a!map(
      id: 2,
      name: "Test Again",
      value: ""
    )
  },
  local!dataUpdated: a!forEach(
    items: local!data,
    expression: a!map(
      id: fv!item.id,
      name: fv!item.name,
      value: "New Value"
    )
  ),
  a!gridLayout(
    headerCells: {
      a!gridLayoutHeaderCell(label: "ID"),
      a!gridLayoutHeaderCell(label: "Name"),
      a!gridLayoutHeaderCell(label: "Value"),
    },
    rows: a!forEach(
      items: local!dataUpdated,
      expression: a!gridRowLayout(
        contents: {
          a!textField(
            value: fv!item.id,
            saveInto: fv!item.id
          ),
          a!textField(
            value: fv!item.name,
            saveInto: fv!item.name
          ),
          a!textField(
            value: fv!item.value,
            saveInto: fv!item.value
          )
        }
      )
    )
  )
)