- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2024 03:14 AM - edited ‎07-24-2024 04:10 AM
Hi Community,
I have requirement where have to add rows in multirow variable set based on ascending order
means in multi row variable set have one choice variable where choices are 1, 2, 3 ,4
when i am adding row of variable with choice 2 and then adding with 1
then rows should be sort by based on this choice variable like first 1 then 2
How to archive this?
went through below link but this solution is on workflow
https://www.servicenow.com/community/itsm-forum/sorting-a-multi-row-variable-set/m-p/684223
and in my requirement will not change anything on workflow as workflow is common
@Ankur Bawiskar can you please help here
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2024 04:27 AM - edited ‎07-24-2024 04:31 AM
You could still use the script you've referenced if you made a few amendments to it. Only the line where the values MRVS is being retrieved and set need to be amended, although i've added in a line at the top so the MRVS name only needs to be specified in one place. The other lines referencing "current" have all been removed.
Taking their script and modifying it to work client-side for an on Submit Client Script:
var mrvsInternalName = 'some_name'; // The internal name of the MRVS
var jsonString = g_form.getValue(mrvsInternalName);
var key = 'v_string2';
var array = JSON.parse(jsonString);
var finalObj = sort_by_key(array, key);
g_form.setValue(mrvsInternalName, JSON.stringify(finalObj));
function sort_by_key(array, key){
return array.sort(function(a, b){
var x = a[key]; var y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
I've not validated this at all as I don't have any test Catalog Items to validate it against but both JSON.parse() and JSON.stringify() are available in Client Scripts so I don't know why it can't work
You could also use a Business Rule to use the existing code on the solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2024 04:27 AM - edited ‎07-24-2024 04:31 AM
You could still use the script you've referenced if you made a few amendments to it. Only the line where the values MRVS is being retrieved and set need to be amended, although i've added in a line at the top so the MRVS name only needs to be specified in one place. The other lines referencing "current" have all been removed.
Taking their script and modifying it to work client-side for an on Submit Client Script:
var mrvsInternalName = 'some_name'; // The internal name of the MRVS
var jsonString = g_form.getValue(mrvsInternalName);
var key = 'v_string2';
var array = JSON.parse(jsonString);
var finalObj = sort_by_key(array, key);
g_form.setValue(mrvsInternalName, JSON.stringify(finalObj));
function sort_by_key(array, key){
return array.sort(function(a, b){
var x = a[key]; var y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
I've not validated this at all as I don't have any test Catalog Items to validate it against but both JSON.parse() and JSON.stringify() are available in Client Scripts so I don't know why it can't work
You could also use a Business Rule to use the existing code on the solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2024 04:38 AM
Thank you for replying!
but in my case have to sort Rows based on selected variable first choice
please find below screenshot
where added rows but after adding have to sort it based on level
and above solution is sorting variable order
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2024 04:43 AM
Would this not just require setting the 'key' of the above script to the name of the 'level' Variable? key appears to be the variable that the rows are sorted based on
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2024 04:33 AM
Hello @shweta14 ,
Refer the below article if relatable:
https://www.servicenow.com/community/developer-forum/multi-variable-set-sort-the-rows/m-p/1610373
Thank you!!
Dnyaneshwaree Satpute
Tera Guru