- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 12-22-2021 09:19 AM
After making several updates to a complicated form, I found that even leaving some space between the order value of the original variables wasn't sufficient and I needed to reorder many variables to get space to add new formatters and fields. I also dislike seeing orders like 100, 105, 107, 110, 111, etc. and wanted to leave a 'finished' catalog item with evenly ordered variables.
Searching these forums and elsewhere, I couldn't find any code to automatically reorder these variables and I'm shocked this isn't a built-in function, so I'm publishing my version as a UI Action. It will adjust the variables associated with the current catalog item with a configurable start and interval. Existing effective order is preserved, as are any places where multiple items have the same order, but the numbers are regenerated such that there is [interval] between each existing variable.
Attached is the XML defining my UI Action. It is visible on the sc_cat_item form if the current user is an admin. your environment might need a slightly different condition if you have more well defined roles.
The UI Action script follows:
var variables = new GlideRecord("item_option_new");
variables.addQuery('cat_item',current.sys_id);
variables.orderBy('order');
variables.query();
var interval = 10;
var counter = 100-interval; // we increment at the start of the loop (because we need to test against the previous value), so start at 100 - the interval so the first order set is 100
var previousOrder = 0;
while (variables.next()){
// if this variable didn't have the same order as the last one, then increment the counter
if (variables.getValue('order') != previousOrder){
counter += interval;
}
previousOrder = variables.getValue('order'); // update 'this' variable's order to be the 'previous' variable's order
variables.setValue('order',counter); // change this variable's order to the new numbering scheme
variables.update();
}
action.setRedirectURL(current);
Comments welcome. Did I miss any edge cases or is there a better way to do this?
- 591 Views