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 fetch users sys_id as i am using var user = current.variables.first_name+" "+current.variables.last_name;

sureshp1690
Kilo Contributor

Hi SN Developers,

I am in requirement to fetch sys_id of a user using var user = current.variables.first_name+" "+current.variables.last_name;

and once done i would like to insert it in list of groups.

Many Thanks

SP

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Suresh,



You don't have a reference field to user table?


If you are having only first_name and last_name then you will have to query the user table as following to get sys_id.


i.e. concact first name and last name and query for name field. it's not always that you will get a user because sometimes name field is empty.



var user = current.variables.first_name+" "+current.variables.last_name;


var userSysId = '';


var gr = new GlideRecord("sys_user");


gr.addQuery("name", user);


gr.query();


if (gr.next()) {


    userSysId = gr.sys_id;


}



For better approach have a reference field to sys_user table



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Hi Ankur,



It was not able to fetch the sysid   var user = current.variables.first_name+" "+current.variables.last_name; is used in same workflow previous step activity by which SN user gets created successfully.



I tried to debug using logs



can you please advice on below script if i directly give groupMember.user = current.variables.first_name+" "+current.variables.last_name;



emepty rows gets created where as if i edit groups i am able to see that user in that list.



but again able to see him in collection list as well



Any advice please.




var groups = current.variables.groups + '';  


var user = current.variables.first_name+" "+current.variables.last_name;



var userSysId = '';


var gr = new GlideRecord("sys_user");


gr.addQuery("name", user);


gr.query();


if (gr.next()) {


    userSysId = gr.sys_id;


}


gs.log("AON4" + userSysId);


gs.log("AON5" + user);


var group = current.variables.groups + '';


gs.log("AON6" + group);




var groupList = [];  


 


// if we have more than one they will be comma delimited  


if (groups.indexOf(',') > -1) {  


  groupList = groups.split(',');  


}  


else {  


  groupList.push(groups);   // only one group  


}  


 


// just a few debug messages - remove these when going to QA  


message += 'groups: ' + groups + '\n';  


message += 'groupList: ' + groupList.length + '\n';  


message += 'user: ' + user + '\n';  




gs.log(message, identifier);




// retrieve the list of user names  


var userRecords = new GlideRecord('sys_user');


userRecords.addQuery('sys_id','IN',user);  


userRecords.query();  


 


var userNames = [];  


while(userRecords.next()) {  


  userNames.push(userRecords.name + '');  


}  




// retrieve group manager, and group name  


var groupInfo = new GlideRecord('sys_user_group');  


if (groupInfo.get('sys_id', group)) {  


  //workflow.scratchpad.manager = groupInfo.manager + '';  


  workflow.scratchpad.groupName = groupInfo.name + '';  


 


  //messages += '---> workflow.scratchpad.groupName: ' + workflow.scratchpad.groupName + '-' + identifier + '\n';  


  //messages += '---> workflow.scratchpad.manager: ' + workflow.scratchpad.manager + '-' + identifier + '\n';  


}  


workflow.scratchpad.userNames = userNames;  


 


workflow.scratchpad.group = group;  


//workflow.scratchpad.users = userList;  


 


//workflow.scratchpad.messages = messages;




addUsersToGroup(user, workflow.scratchpad.group);  


 


function addUsersToGroup(user, groupID) {  


  for (var i=0; i < groupList.length; i++) {  


  var group = groupList[i] + '';      


 


//workflow.scratchpad.messages += '\tUser (' + userIDList[i] + ') added to group: '    


//+ workflow.scratchpad.group + '\n';  


 


  var groupMember = new GlideRecord("sys_user_grmember");  


  groupMember.initialize();  


  //groupMember.user = current.variables.first_name+" "+current.variables.last_name;


  groupMember.user = userSysId;


  gs.log("AON7" + groupMember.user);  


  groupMember.group = groupID + '';  


  gs.log("AON9" + groupMember.user);


  groupMember.insert();  


  }  


}  



Many Thanks


SP