- 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 04:04 PM - edited 01-13-2025 04:05 PM
A list collector's value should be a string in the format of sys_id1,sys_id2,sys_id3 so without the brackets. What you want is to join your array of strings into one string, which you can do with .join():
var objAnswer = JSON.parse(answer);
var arrAddGroups = objAnswer.addGroups; // ["sys_id1", "sys_id2", ...]
var strAddGroups = arrAddGroups.join(','); // "sys_id1,sys_id2,..."
g_form.setValue("add_groups", strAddGroups);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 10:22 PM
Thank you for the response Nick. Sorry, I tried it but no luck.
Client Script updated as below.
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 = objAnswer.addGroups;
var strAddGroups = arrAddGroups.join(',');
g_form.showFieldMsg('add_groups', 'Add Groups Obj: ' + strAddGroups);
g_form.showFieldMsg('remove_groups', 'Remove Groups Obj: ' + objAnswer.removeGroups);
});
}
Response received as below.
Add Groups Obj: "dfb2f909dbe76200b81ad8c5ce96194f","11d14762db89b704a949e59b8a961958"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 10:23 PM
Thank you for the response Nick. Sorry, I tried it but no luck.
Client Script updated as below.
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 = objAnswer.addGroups;
var strAddGroups = arrAddGroups.join(',');
g_form.showFieldMsg('add_groups', 'Add Groups Obj: ' + strAddGroups);
g_form.showFieldMsg('remove_groups', 'Remove Groups Obj: ' + objAnswer.removeGroups);
});
}
Response received as below.
Add Groups Obj: "dfb2f909dbe76200b81ad8c5ce96194f","11d14762db89b704a949e59b8a961958"
- 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-14-2025 05:40 AM
Thank you very much for your help, Amit! It worked.