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 get emails and syids through sub flow inputs ???

Manikantahere
Tera Contributor

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);

req-5.png

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Manikantahere 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

Aniket1498
Tera Guru

@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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@Manikantahere 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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.

@Manikantahere 

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?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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?