Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to sort in alphabetic order

SNOW39
Tera Expert

In my catalog item i have 5 fields, User 1, User 2, User 3, User 4 and User 5.

I need these values in my Workflow, sort them in A to Z format. How to achieve this?

10 REPLIES 10

Mohith Devatte
Tera Sage
Tera Sage

Hello,

Use a run script activity and  get all the variables access and push it to an array like below

var fd1 = current.variables.field1 back end name;
var fd2 = current.variables.field2 back end name;
var fd3 = current.variables.field3 back end name;
var fd4 = current.variables.field4 back end name;
var fd5 = current.variables.field5 back end name;

If its a reference field use current.variables.field back end name.getDisplayValue();

var arr=[];
arr.push(fd1);
arr.push(fd2);
arr.push(fd3);
arr.push(fd4);
arr.push(fd5);

var sortedArr = arr.sort();

you can access elements one by one which should be sorted in alphabetical order like sortedArr[0] ,sortedArr[1],...... 

Please mark my answer correct if it helps you

Tried this, But not getting this in log.

Last name is single line text field.

 

var arr =[];
var fd1 = current.variables.FND_RecLastName;
var fd2 = current.variables.last_name_2;
var fd3 = current.variables.last_name_3;
var fd4 = current.variables.last_name_4;
var fd5 = current.variables.last_name_5; 

arr.push(fd1);arr.push(fd2);arr.push(fd3);arr.push(fd4);arr.push(fd5);

 

var sortedArr = arr.sort();

gs.log("sortedArr"+sortedArr);

hello,

Can you try putting logs for arr

gs.info('before sorting'+arr);

Try converting the array back to string?