- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2025 03:36 AM
I need to get the user or group members emails and sys Ids and sysIds format should be such a way that I can assign them directly to additonal assignee list while I am creating catalog task through oob create catalog task.
But I am not sure what went wrong I am not able to run the script and its producing the error as shown in below??
Can anyone modify the code or correct me what is the wrong?
@Ankur Bawiskar
email format should be same way so that third party system can understand / differentiate mails by ";".
(function execute(inputs, outputs) {
gs.log("array of sys ids entry:"+inputs.approvallist);
var emails = '';
if(inputs.group){
var groupmembers = new GlideRecord("sys_user_grmember");
groupmembers.addQuery("group",inputs.group);
groupmembers.addExtraField("user.email");
groupmembers.query();
while (groupmembers.next) {
emails += groupmembers.user.email + ';';
}
outputs.emaillist = emails.slice(0, -1);
}else if(inputs.assignedto){
var userGr = new GlideRecord("sys_user");
userGr.get(inputs.assignedto);
outputs.emaillist = userGr.email;
}
else if(inputs.approvallist){
for(var i=0; i<inputs.approvallist.length; i++){
var userGr1 = new GlideRecord("sys_user");
userGr1.get(inputs.approvallist[i]);
emails += userGr1.email + ';';
}
outputs.emaillist = emails.slice(0, -1);
}
})(inputs, outputs);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2025 03:43 AM
not sure why you are using slice, but here is the updated code
try this and let me know
(function execute(inputs, outputs) {
gs.log("array of sys ids entry:" + inputs.approvallist);
var emails = [];
if (inputs.group) {
var groupmembers = new GlideRecord("sys_user_grmember");
groupmembers.addQuery("group", inputs.group.sys_id);
groupmembers.query();
while (groupmembers.next()) {
emails.push(groupmembers.user.email.toString());
}
outputs.emaillist = emails.toString();
} else if (inputs.assignedto) {
var userGr = new GlideRecord("sys_user");
userGr.get(inputs.assignedto);
outputs.emaillist = userGr.email.toString();
} else if (inputs.approvallist) {
for (var i = 0; i < inputs.approvallist.length; i++) {
var userGr1 = new GlideRecord("sys_user");
userGr1.get(inputs.approvallist[i]);
emails += userGr1.email + ',';
}
outputs.emaillist = emails.toString();
}
})(inputs, outputs);
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
‎03-21-2025 05:37 AM
@Manikantahere
Please try with the below. I believe it would work if you have given the field datatypes correctly.
(function execute(inputs, outputs) {
gs.log("array of sys ids entry:" + inputs.approvallist);
var emails = [];
var groupID = inputs.group;
var assignedTo = inputs.assignedto;
var approvalList = inputs.approvallist
if (groupID) {
var groupmembers = new GlideRecord("sys_user_grmember");
groupmembers.addEncodedQuery("group=", groupID);
groupmembers.query();
while (groupmembers.next()) {
emails.push(groupmembers.user.email);
}
} else if (assignedTo) {
var userGr = new GlideRecord("sys_user");
userGr.get(assignedTo);
emails.push(userGr.email);
} else if (approvalList) {
for (var i = 0; i < approvalList.length; i++) {
var userGr1 = new GlideRecord("sys_user");
userGr1.get(approvalList[i]);
emails.push(userGr1.email);
}
}
outputs.emaillist = emails.join(';');
})(inputs, outputs);
Please mark my response as helpful and accept it as solution if it solved your problem.
Thanks & Regards,
Aniket Dey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2025 03:43 AM
not sure why you are using slice, but here is the updated code
try this and let me know
(function execute(inputs, outputs) {
gs.log("array of sys ids entry:" + inputs.approvallist);
var emails = [];
if (inputs.group) {
var groupmembers = new GlideRecord("sys_user_grmember");
groupmembers.addQuery("group", inputs.group.sys_id);
groupmembers.query();
while (groupmembers.next()) {
emails.push(groupmembers.user.email.toString());
}
outputs.emaillist = emails.toString();
} else if (inputs.assignedto) {
var userGr = new GlideRecord("sys_user");
userGr.get(inputs.assignedto);
outputs.emaillist = userGr.email.toString();
} else if (inputs.approvallist) {
for (var i = 0; i < inputs.approvallist.length; i++) {
var userGr1 = new GlideRecord("sys_user");
userGr1.get(inputs.approvallist[i]);
emails += userGr1.email + ',';
}
outputs.emaillist = emails.toString();
}
})(inputs, outputs);
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
‎03-21-2025 05:02 AM - edited ‎03-21-2025 05:03 AM
you know @Ankur Bawiskar In the above scinario for each case I need to get both emails as well as sysIDs.
- Email list is for thirdparty as they asked me to send the sring of emails separated by ";" .
- Assignee list also I needed as I need to get the users from the group and approvalist and set them in additional assignee list while I am creating the catalog task in next step.
But I am getting the above error irrespective of the modification I am doing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2025 05:28 AM
you are passing the group Record as input and hence I gave this inputs.group.sys_id
why not send sysId i.e. string and then query group table in your script and process further?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2025 05:36 AM
If you do that we need to query first group table and then group members right? but I just tried with group member table so that i can skip querying another table right?