Checking for and updating existing records on post

I have a table of tasks with a Priority column and I need to make sure the Priority of all tasks are sequential. I’m not planning to do this with validation, but rather when the form is submitted, the server action will update the other records as needed.

So if the user edits or enters a new task, and assigs it a Priority that already exists, when it’s saved, the server action needs to bump the Priority value all other records with a value equal to or greater than the one being saved.

So in my server action, I have the following:

The query checks if there are other records with a Priority greater or equal to the one being saved:
image

Then, if there are any, the repeat loops over them and increments the values. The problem I’m having is the SetValue is concatenating the value instead of adding it. So 6 becomes 61 instead of 7 like it should.
I’ve used the Data Formats screen to configure the value:
image

Any idea how to get this to work?

  1. First of all check that Priority field in database is Integer and made 0 as a default
  2. On a first step I would query current task priority and
  3. On step 2 I would made an Update query which will update current priority with value (priority value from step 1) + 1

I think 1 and 2 are already taken care of. As I mentioned, #3 is concatenating the value + 1 instead of adding it. So if the current priority is 6, it sets it to 61 instead of 7.

Well if it’s concatenating rather then adding +1 - you really missing correct data type in DB.
Just checked my solution - all worked as expected - on each update it’s adds +1 to previous value.

I’m not adding the 1 inside the update, but rather to the Set Value just before the update, then using that value in the update:

As you can see, Priority is a number:
image

61 is a valid number so the database is accepting it.

I think it really comes down to the question of how to get the Set Value to add one to itself as in:
{{PriorityValue + 1}}
instead of:
{{PriorityValue}}1
which it insists on doing when I save.

If I open the pencil editor and correct it like:

When I save and reopen, it is right back to this:

How can I get it to add instead of concatenate?

It looks Ok to me {{PriorityValue + 1}}. If this is changed on save, then there is a glitch in the program.

Grab a text editor, like Notepad, and open the relevant JSON file. Change the value as you want it to be and save the file.

The JSON file can be found in app -> api -> <<Api Folder>> -> <<json>>

How does it show if you click to update using the data pickers (rather than the text editor)? I have a similar step in a while loop:

image

If I view it using the lightning icon it looks fine,
image

in the text editor it displays incorrectly and keeps reverting as you describe, though this looks to be a display issue only.

image

The actual json shows correctly. It may be if you update using the lightning icon you’ll end up with the correct output (though there still looks to be a bug in how it displays in the text editor!).

image

I tried that, but it doesn’t work.

I set it to this:
image

It shows this in the text editor:
image

And, when I look at the json, it shows:
image

I did change the json to:
image

And that took care of the concatenation, so I think it is a bug. I hope someone from the team will see this and look into it.

But, I have another question that I’ll post next.

If you look at my Server Action:

Everything works as expected (with the JSON correction) down through the first loop in the repeat. The query finds the correct records and the Priority value is incremented and the first record is updated with the incremented value. But then it stops. It seems the Condition (selected in the image) is not being met after that. The output from Developer tools shows this:

As you can see, the first one is true and the rest are false. The first record was updated from 7 to 8, so PriorityValue now holds the value of 8 and the second record has a Priority of 8 so the Condition should be true for index 1. So, why is it failing??

This could be down to how you’ve named the set value step. Again I think mine was a similar use case/ issue and took a while to work through (with some valued help from @mebeingken ).

Try adding a global name of PriorityValue to both the Set Value steps, and then change the name of the steps to PriorityValueInitial and PriorityValueUpdated (or similar).

This has to do with the scope of access of the variable in each step, by making it global it can be accessed within the repeat too (or something along those lines!). By just using the name field it may be only the initial PriorityValue is accessible (I’m sure there a technically better explanation than that but hopefully you get the general idea).

You are correct, @sbecks! I added a global name of PriorityValue to both of the Set Value steps, and bingo, it works! I had seen something about scopes in another thread, but thought it must not apply to this case, because of the fact it was working the first loop through. Would be really nice to have some good documentation for how this is to work.

I didn’t quite follow you on changing the name of the steps, but this seems to have taken care of the problem. Thanks so much for your time!