Setting changeset object in trigger

I have a table inside a retool database and have a query to update that table.

I want to run that query from javascript, but always get a 422 error ('A changeset must be specified'). I have this code now and have tried all kinds of formatting for the changeset object inside the additionalScope object.

if (pickModel.value == 'KLZIZO_selected') {
  let eval = [{key: 'Annif_KL_OmiPar', value: 'Bad'}];
  //let eval2 = {key: 'Annif_KL_OmiPar', value: 'Bad'};
  //let eval3 = {Annif_KL_OmiPar: 'Bad'};
  
await model1.trigger({
  additionalScope: { changeset: eval },
  onSuccess: function (data) {
    console.log(data);
  },
  onFailure: function (error) {
    console.log(error);
  },
});

}

Can a changeset not be set like this?

If not how else can i pass a key/value pair as changeset when triggering a query. I don't want to have to make umpteen queries with predefined changesets for all possible table updates.

The database update query:

Hello!, im unsure if it's possible to define the changeset thru a JS query as additional scope, but i think a quick workaround would be to:

  1. have your js query (i.e. query1) to return the key/pair object
  2. in your retool_db query reference the query output in Changeset > Object with query1.data
  3. trigger your retool_db query when query1 Success
1 Like

Instead of trying to set that value directly, can you pass it through additional scope and then reference it in the query using the 'Object' tab instead of 'Key value pairs'?

1 Like

Hi @Kris,

I think you've got a couple things going on here, so let me know if this is what you're trying to do.

From your JS script, you want to trigger that query. The query matches the row to update based on some input fields elsewhere in your app that map to the columns Titel and Text.

For that matching row, you want to update the column Annif_KL_OmiPar to the value Bad?

If that's what you're trying to do, I would suggest changing the value of eval to:

if (pickModel.value == 'KLZIZO_selected') {
  // Notice the change here, both the object and the name - avoid "eval" in JS
  let evalData = { "Annif_KL_OmiPar": "Bad" };
  
await model1.trigger({
 //Changed this as well, so we pass the evalData object as additionalScope
  additionalScope: { evalData },
  onSuccess: function (data) {
    console.log(data);
  },
  onFailure: function (error) {
    console.log(error);
  },
});
}

Then in your query, change the Changeset section to Object instead of Key value pairs,
image

3 Likes

Thank you all for the speedy responses.

The object-method resulted in a statusCode:422 error:"Unprocessable Entity" at first, so different error. Which led me to adapt the filter and use a filter on id (primary key) instead. When configured like that the update method/query works.

But that means I need to know the id already which I don't when running the query.
I could write another query that looks up the title/text combination and returns the id, then use the value that query returns in the update-query. Would prefer a more elegant solution with just 1 query though, is that possible?

@Kris - What do your other inputs look like? The ones used in the filter. If they are on the page, it should work using the 2 filters for Title and Text. Maybe try console.log'ing them from your function to confirm they are accurate. I can't tell what they are in the original screenshot, but "Title" is spelled differently in the column name vs the variable name "Titel" vs "Title", and the Text value looks like it might be a couple different value?

The spelling difference is because my data is in Dutch but I'm writing my code in English :smile:

But I figured out what whas happening, feel so stupid, there was an unwanted empty space inside the 'Titel' string in the database which made it differ from the input value :person_facepalming:

Haha I knew Titel could have been right, so I didn't want to call it a "Typo", just funny to see it used both ways at the same time! Glad you figured it out!

I'm also getting this error. Have you resolved yours? I'm trying to update a firebase realtime database entry through a component script. I've been on this for some days now. Someone should please help me out. @Kris @kschirrmacher @MikeCB @OOSC3

let update_field = {isActive: false}
await updateActiveQuery.trigger({
additionalScope: {update_field},
onSuccess: (data) => {
console.log("success",data)
},
onFailure: (error) => {
console.log("error", error)
}
})

How do I also direct the update to the selected entry

Hi @Alabi_Mujeeb,

Can you share a screenshot of your updateActiveQuery function?