Flow Designer Script to remove certain Items

Vasu2
Tera Contributor
 
10 REPLIES 10

Hello @Vasu2 

 

My bad I gave you code with new javascript set function which won't work in ServiceNow as we still use older version.


Now the code will be as below:

var res =[];
var desc = fd_data.trigger.current.description.toString();
//below is something i tested in my pdi to pass some dummy values
//var desc = '{"Rank 1":["user_id1"],"Rank 6":["user_id2","user_id1"],"Rank 26":["user_id3"] }';
//var desc = '{"Rank 1":["user_id1"],"Rank 6":["user_id2","user_id1"],"Rank 24":["user_id3"] }';

desc = JSON.parse(desc);
var hasRankGreaterThan25 = false;
var defaultUserID = 'YOUR_DEFAULT_USER_ID'; // Replace with the actual default user ID
var uniqueRes =[];

for (var rankKey in desc) {
    if (desc.hasOwnProperty(rankKey)) {
        var tempAr = rankKey.split(' ');
        if (tempAr.length > 1) {
            var index1 = tempAr[1];
            var index1Int = parseInt(index1);
            if (index1 && !isNaN(index1Int)) {
                if (index1Int < 25) {
                    res = res.concat(desc[rankKey]);
                } else if (index1Int >= 25) {
                    hasRankGreaterThan25 = true;
                }
            }
        }
    }
}

// Remove duplicate user IDs using a traditional method
for (var i = 0; i < res.length; i++) {
    if (uniqueRes.indexOf(res[i]) === -1) {
        uniqueRes.push(res[i]);
    }
}
res = uniqueRes;

if (hasRankGreaterThan25) {
    res.push(defaultUserID);
}

return res.join(',');

/*
Output for input
Input = var desc = '{"Rank 1":["user_id1"],"Rank 6":["user_id2","user_id1"],"Rank 24":["user_id3"] }';
Output = user_id1,user_id2,user_id3

Input = var desc = '{"Rank 1":["user_id1"],"Rank 6":["user_id2","user_id1"],"Rank 25":["user_id3"] }';
Output = user_id1,user_id2,YOUR_DEFAULT_USER_ID
*/

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Vasu2
Tera Contributor

Sorry, I missed to paste the details, Below are the details.
Using Flow Designer, we are sending the Approvals. we are trying to pull all the user IDs from a string variable. The data in the string variable is in the format  {"Rank 1": ["user_id1"], "Rank 6":["user_id2"], "Rank 26":["user_id2"] }.

1)some string contains Ranks both less than 25 and greater than 25
2) Some strings contains Rank less than 25 only

If the string contains Ranks both less than 25 and greater than 25, we need to remove the userIDs which are with Rank greater than 25 and send only to the user IDs with less than 25 and also to a default userID. If the string contains only Ranks less than 25 then send the approval for all the userIDs.
The below script is sending for all the ranks below 25 but how to add the condition in the below script to see if the string variable contains Rank more than 25 and send approvl to a default userID along with the UserIDs less than Rank 25. If the string variable contains only Ranks below 25 then we only need to send the Approval for all the userIDs but not the default UserID.

var res = [];
var desc = fd_data.trigger.current.description.toString(); 
//assuming the record is the trigger and field is description feel free to update them 
//test string '{"Rank 1":["Hello"],"Rank 2":["fjalf"],"Rank 3":["fadfa"],"Rank 25":["080"],"Rank 26":["26"]}'
desc = JSON.parse(desc);
for (i in desc) {

    var tempAr = i.split(' ');
    if (tempAr.length > 1) {
        var index1 = tempAr[1];
        var index1Int = parseInt(index1);
        if (index1 && !isNaN(index1Int) && index1Int < 25) {
            res = res.concat(desc[i]);
        }
    }
}
return res.join();




Vasu2
Tera Contributor

Additional Details
Whenever an Incident is created, based on Short description values we have to send the Approval. Here is the flow 

Vasu2_2-1742498453568.png

 

Vasu2
Tera Contributor

Can anyone help on this....?

Hi @Vasu2 ,

Update the script to 

var res = [];
var desc = fd_data.trigger.current.description.toString(); 
//assuming the record is the trigger and field is description feel free to update them 
//test string '{"Rank 1":["Hello"],"Rank 2":["fjalf"],"Rank 3":["fadfa"],"Rank 25":["080"],"Rank 26":["26"]}'
desc = JSON.parse(desc);
for (i in desc) {

    var temp = parseInt(i.slice('5'))
    if (temp && !isNaN(temp) && temp < 25) {
        res = res.concat(desc[i]);
    } else {
        var defalutUserID = 'admin'; /*update the default user id here*/
        if (res.indexOf(defalutUserID) == -1)
            res.push(defalutUserID )
    }

}
return res.join()

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya