- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 07:33 AM
I am getting the response in Client Script from the Script Include as below.
["5390f47f97e892545a4b31511153af96","2a21e73697709e901b85f0511153afce","42c87060471e46d0fcf52501e36d436f"]
I would like to convert it as below by removing the double quotes.
[5390f47f97e892545a4b31511153af96,2a21e73697709e901b85f0511153afce,42c87060471e46d0fcf52501e36d436f]
Please help me to achieve this by adding the piece of code in Script Include or Client Script.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 06:40 PM
Hi @Bhaskar24
If you just want to get rid of the double quotes, you can convert your array to string followed by replacing all the double quotes and finally trimming the white spaces. Refer below screenshots and code:
var arr = ["5390f47f97e892545a4b31511153af96","2a21e73697709e901b85f0511153afce","42c87060471e46d0fcf52501e36d436f"];
arr = arr.toString().replaceAll('"',"").trim();
gs.print(arr);
Output -
Thanks and Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 07:35 AM
that's how array stores value, you can't do anything for it.
What's your requirement?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 08:16 AM
I need to pass the response to List Collector variable which should be as [a,b,c] instead ["a","b","c"].
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var userID = g_form.getValue('requested_for');
var holdingGroups = g_form.getValue('holding_memberships');
g_form.showFieldMsg('add_remove_groups', 'Old Groups: ' + oldValue);
g_form.showFieldMsg('add_remove_groups', 'New Groups: ' + newValue);
g_form.showFieldMsg('add_remove_groups', 'Holding Groups: ' + holdingGroups);
var ga = new GlideAjax('ManageAtYourServiceMembers');
ga.addParam('sysparm_name', 'validateMembership');
ga.addParam('sysparm_requested_for', userID);
ga.addParam('sysparm_holdingGroups', holdingGroups);
ga.addParam('sysparm_updatedGroups', newValue);
ga.getXMLAnswer(function(answer) {
var objAnswer = JSON.parse(answer);
var arrAddGroups = [];
arrAddGroups = objAnswer.addGroups;
//var parsedAGarray = JSON.parse(arrAddGroups);
var resultAGarray = arrAddGroups.map(function(item) {
return item;
});
g_form.showFieldMsg('add_groups', 'Add Groups Obj: ' + resultAGarray);
g_form.showFieldMsg('remove_groups', 'Remove Groups Obj: ' + objAnswer.removeGroups);
//g_form.setValue('add_groups', objAnswer.addGroups);
//g_form.setValue('remove_groups', objAnswer.removeGroups);
});
}
Script Include:
validateMembership: function() {
gs.log('1-In validateMembership', 'MAYSM');
var userID = this.getParameter('sysparm_requested_for');
var inputHoldingGroups = this.getParameter('sysparm_holdingGroups');
var inputHoldingGroups1 = inputHoldingGroups.split(',');
var holdingGroupsArray = [];
for (var i = 0; i < inputHoldingGroups1.length; i++) {
holdingGroupsArray.push('"' + inputHoldingGroups1[i] + '"');
}
holdingGroupsArray.join();
var inputUpdatedGroups = this.getParameter('sysparm_updatedGroups');
var inputUpdatedGroups1 = inputUpdatedGroups.split(',');
var updatedGroupsArray = [];
for (var j = 0; j < inputUpdatedGroups1.length; j++) {
updatedGroupsArray.push('"' + inputUpdatedGroups1[j] + '"');
}
updatedGroupsArray.join();
gs.log('2-holdingGroups: ' + inputHoldingGroups + ', updatedGroups: ' + inputUpdatedGroups, 'MAYSM');
gs.log('3-userID: ' + userID + ', holdingGroupsArray: ' + holdingGroupsArray + ', updatedGroupsArray: ' + updatedGroupsArray, 'MAYSM');
var removeGroupsArray = [];
var addGroupsArray = [];
var arrayUtil = new global.ArrayUtil();
removeGroupsArray = arrayUtil.diff(holdingGroupsArray, updatedGroupsArray);
addGroupsArray = arrayUtil.diff(updatedGroupsArray, holdingGroupsArray);
gs.log('4-removeGroups: ' + removeGroupsArray + ', addGroups: ' + addGroupsArray, 'MAYSM');
var parsedArray = JSON.parse(response);
var resultArray = parsedArray.map(function(item) {
return item;
});
var groupsObj = {
"addGroups": addGroupsArray,
"removeGroups": removeGroupsArray
};
gs.log('5-removeGroupsObj: ' + groupsObj.removeGroups + ', addGroupsObj: ' + groupsObj.addGroups, 'MAYSM');
return JSON.stringify(groupsObj);
},
Response:
Add Groups Obj: "dfb2f909dbe76200b81ad8c5ce96194f","1fb2f909dbe76200b81ad8c5ce961950" from Client Script.
Below logs from Script Include:
1-In validateMembership
2-holdingGroups: 5390f47f97e892545a4b31511153af96,2a21e73697709e901b85f0511153afce,42c87060471e46d0fcf52501e36d436f,1767f3551372d60090fd54985144b0bf,98eb9f4bdbab5200051159ffdf9619f7,064f18a213429600727db9785144b095,4a132fba97709e901b85f0511153af3d,05f1abb697709e901b85f0511153af97,b66fdaccdb5a80102d7d4562399619aa,2a04e33697b09e901b85f0511153af4b,ef13871b13256200727db9785144b0f1,72c078e2138e2280727db9785144b040,b33df7bcdb4faa007b6cf55eae9619c0,2eed2a401b044c505e6bfccbde4bcb55, updatedGroups: 5390f47f97e892545a4b31511153af96,2a21e73697709e901b85f0511153afce,42c87060471e46d0fcf52501e36d436f,1767f3551372d60090fd54985144b0bf,98eb9f4bdbab5200051159ffdf9619f7,064f18a213429600727db9785144b095,4a132fba97709e901b85f0511153af3d,05f1abb697709e901b85f0511153af97,b66fdaccdb5a80102d7d4562399619aa,2a04e33697b09e901b85f0511153af4b,ef13871b13256200727db9785144b0f1,72c078e2138e2280727db9785144b040,b33df7bcdb4faa007b6cf55eae9619c0,2eed2a401b044c505e6bfccbde4bcb55,dfb2f909dbe76200b81ad8c5ce96194f,1fb2f909dbe76200b81ad8c5ce961950
3.userID: d1337ce6db4308509b24456239961970, holdingGroupsArray: "5390f47f97e892545a4b31511153af96","2a21e73697709e901b85f0511153afce","42c87060471e46d0fcf52501e36d436f","1767f3551372d60090fd54985144b0bf","98eb9f4bdbab5200051159ffdf9619f7","064f18a213429600727db9785144b095","4a132fba97709e901b85f0511153af3d","05f1abb697709e901b85f0511153af97","b66fdaccdb5a80102d7d4562399619aa","2a04e33697b09e901b85f0511153af4b","ef13871b13256200727db9785144b0f1","72c078e2138e2280727db9785144b040","b33df7bcdb4faa007b6cf55eae9619c0","2eed2a401b044c505e6bfccbde4bcb55", updatedGroupsArray: "5390f47f97e892545a4b31511153af96","2a21e73697709e901b85f0511153afce","42c87060471e46d0fcf52501e36d436f","1767f3551372d60090fd54985144b0bf","98eb9f4bdbab5200051159ffdf9619f7","064f18a213429600727db9785144b095","4a132fba97709e901b85f0511153af3d","05f1abb697709e901b85f0511153af97","b66fdaccdb5a80102d7d4562399619aa","2a04e33697b09e901b85f0511153af4b","ef13871b13256200727db9785144b0f1","72c078e2138e2280727db9785144b040","b33df7bcdb4faa007b6cf55eae9619c0","2eed2a401b044c505e6bfccbde4bcb55","dfb2f909dbe76200b81ad8c5ce96194f","1fb2f909dbe76200b81ad8c5ce961950"
4-removeGroups: , addGroups: "dfb2f909dbe76200b81ad8c5ce96194f"
5-removeGroupsObj: , addGroupsObj: "dfb2f909dbe76200b81ad8c5ce96194f","1fb2f909dbe76200b81ad8c5ce961950"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 07:36 AM
See:
No need to post the same question twice.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 11:31 AM
ChatGPT can do this for you