Request for multiple users - Reference variable for each user.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 12:00 PM
I'm creating a Catalog Item form for users to request a new computer.
One person (manager) should be able to request that for multiple users, I have a Variable set that allows him to add more users to the same request.
Each user has a computer name associated with his account, that information is on cmdb_ci_computer table under "NAME".
This request should create an RITM for each user, I guess I need a Script include and Client Script to achieve that, right?
I tried to create scripts, but I'm not an expert with scripting, so I guess I'm doing something wrong.
Computer name (Computer_name) Reference (cmdb_ci_computer)
Location (Location) reference (cmdb_ci_computer)
Can someone give me some advice here? or a better solution for what I need?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 09:47 PM
Hi @rafaelalves4337 ,
You can write after inter BR on requested item table and use below code.
// Get the variable containing selected users
var users = JSON.parse(current.variables.selected_users);
if (users.length === 0) {
gs.addInfoMessage('No users selected for this request.');
return;
}
// Loop through each user and create RITMs
users.forEach(function(userSysId) {
var userGr = new GlideRecord('sys_user');
if (userGr.get(userSysId)) {
var cmdbRecord = new GlideRecord('cmdb_ci_computer');
cmdbRecord.addQuery('assigned_to', userSysId);
cmdbRecord.query();
if (cmdbRecord.next()) {
var ritm = new GlideRecord('sc_req_item');
ritm.initialize();
ritm.request = current.request; // Link to the same Request
ritm.cat_item = current.cat_item; // Same Catalog Item
ritm.variables.computer_name = cmdbRecord.name; // Computer Name
ritm.variables.location = cmdbRecord.location; // Location
ritm.variables.requested_for = userSysId; // User
ritm.insert();
} else {
gs.addInfoMessage('No computer assigned to user: ' + userGr.name);
}
}
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2025 12:52 AM
you can use CartJS API to create RITM rather than using GlideRecord in after insert BR
OR
you can also use flow designer with no code low code
what did you start with and where are you stuck?
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-14-2025 07:32 AM
Hello @Ankur Bawiskar and @Runjay Patel
What I tried to use was Script include and Client Script, but it's not populating the variables (Single Line Text)
Script Include
var Computer_name_test = Class.create();
Computer_name_test.prototype = Object.extendsObject(AbstractAjaxProcessor, {
fetchUserData: function() {
var arr = [];
var requestedFor = this.getParameter('sysparm_sys_id');
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('sys_id', requestedFor);
gr.query();
while (gr.next()) {
arr.push(gr.name);
arr.push(gr.assigned_to);
}
return arr.toString();
},
type: 'Computer_name_test'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var user=g_user.userID;
g_form.setValue('requested_by',user);
var requestedFor=g_form.getValue('requested_for');
var ga=new GlideAjax('Computer_name_test');
ga.addParam('sysparm_name','fetchUserData');
ga.addParam('sysparm_sys_id',requestedFor);
ga.getXML(callback);
function callback(response){
var answer=response.responseXML.documentElement.getAttribute('answer');
var arr=answer.split(',');
g_form.setValue('computer_name',arr[0]);
g_form.setValue('user_id',arr[1]);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2025 07:52 AM
what's your actual requirement?
you wish to populate those variables on catalog form or want to create multiple RITMs?
For the 2nd point I already shared the link
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