Ask the Expert

How can documents be reordered using an agent?

I am looking for a way to reordered priority numbers -- specifically, a user selects a document with priority 6 and wants to change it to priority 3. How, through an agent, can you reordered all other documents on the view, so each has a unique and ordered priority value? The new priority value may be higher or lower than the current priority.
I'm not a big proponent of modifying multiple documents to change their order in a view, as it raises the likelihood of replication/save conflicts if there are multiple simultaneous users of the database or if there's a single user who likes to work on multiple documents simultaneously.

However, if you want to do it, assuming the priorities are already unique and sequential, the easiest way is probably to create a view action button with the following formula:

current_priority := Priority; REM {or whatever the fieldname is if not Priority};
priority_all := @DbColumn("":"NoCache"; ""; @Subset(@ViewTitle; -1); 1);
REM {above assumes the column with the priority number is number 1};
priority_choices := @Trim(@Replace(@Text(priority_all); @Text
(current_priority); ""));
new_priority := @Prompt([OkCancelList]; "Reprioritize"; "Select new priority 
for this document. NOTE: make sure none of these documents is open in another 
window."; ""; priority_choices);
ENVIRONMENT param1 := @Text(current_priority) + ":" + new_priority;
@Command
([ToolsRunMacro]; "(Adjust Priorities)")
Then create an agent named "(Adjust Priorities)" that runs on all documents in view. Here's the formula for that agent:

oldPri := @TextToNumber(@Left(@Environment("param1"); ":"));
newPri := @TextToNumber(@Right(@Environment("param1"); ":"));
min := @Min(oldPri; newPri);
max := @Max(oldPri; newPri);
SELECT Priority >= min & Priority <= max;
FIELD Priority :=
 @If(Priority = oldPri;
 newPri;
 Priority > oldPri;
 Priority - 1;
 Priority + 1
 )

This was first published in June 2003