- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 11:20 PM
Hi Team,
I have list collector variable A(user table reference), on selecting multiple users, I want to populate users email ID in another list collector variable B. Tried below code, I'm aware GlideRecord is not suggested on client script. Please suggest.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//get list collector values and populate in string field
var traineeEmailCollection=[];
var traineeNameList=g_form.getValue('u_select_trainee_names');
// give here list collector variable name in above syntax
var trainees=traineeNameList.split(',');
for (var i=0; i < trainees.length; i++) {
var gr = new GlideRecord('sys_user');
gr.query('sys_id',trainees[i]);
gr.query();
while(gr.next())
{
traineeEmailCollection.push(gr.email);
}
}
g_form.setValue('trainees_email_id_s',traineeEmailCollection);
// Give the required field name where you need list collector values in above syntax
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2024 12:07 AM - edited 01-31-2024 12:17 AM
Hello @Ash41 ,
Please try below code and see how it works for you.
I have tried by my side and it is working.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Get list collector values and populate in string field
var traineeEmailCollection = [];
var traineeNameList = g_form.getValue('u_select_trainee_names');
var trainees = traineeNameList.split(',');
// Use asynchronous AJAX calls to fetch user emails
var promises = trainees.map(function(traineeId) {
return getUserEmail(traineeId);
});
// Wait for all AJAX calls to complete
Promise.all(promises).then(function(results) {
// Filter out undefined results (failed AJAX calls)
traineeEmailCollection = results.filter(function(email) {
return email !== undefined;
});
if (traineeEmailCollection.length > 0) {
g_form.setValue('trainees_email_id_s', traineeEmailCollection.join(', '));
} else {
//
}
});
}
// Function to fetch user email asynchronously
function getUserEmail(userId) {
return new Promise(function(resolve, reject) {
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', userId);
gr.query(function(response) {
if (response) {
while (gr.next()) {
resolve(gr.name);
}
} else {
reject(); // Resolve with undefined to filter out in the main Promise.all
}
});
});
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Pratiksha
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 11:27 PM
Hello @Ash41
Use second variable as Multi-line text , as email-id is field in user table & not a record itself.
if you make second variable List collector which I assume is again refer to 'sys_user' table , it will not display email id.
& Yes as you are aware not to use Glide object in client script.
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2024 12:07 AM - edited 01-31-2024 12:17 AM
Hello @Ash41 ,
Please try below code and see how it works for you.
I have tried by my side and it is working.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Get list collector values and populate in string field
var traineeEmailCollection = [];
var traineeNameList = g_form.getValue('u_select_trainee_names');
var trainees = traineeNameList.split(',');
// Use asynchronous AJAX calls to fetch user emails
var promises = trainees.map(function(traineeId) {
return getUserEmail(traineeId);
});
// Wait for all AJAX calls to complete
Promise.all(promises).then(function(results) {
// Filter out undefined results (failed AJAX calls)
traineeEmailCollection = results.filter(function(email) {
return email !== undefined;
});
if (traineeEmailCollection.length > 0) {
g_form.setValue('trainees_email_id_s', traineeEmailCollection.join(', '));
} else {
//
}
});
}
// Function to fetch user email asynchronously
function getUserEmail(userId) {
return new Promise(function(resolve, reject) {
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', userId);
gr.query(function(response) {
if (response) {
while (gr.next()) {
resolve(gr.name);
}
} else {
reject(); // Resolve with undefined to filter out in the main Promise.all
}
});
});
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Pratiksha
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2024 07:58 PM
Thank you, Pratiksha it worked
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2024 08:29 PM - edited 01-31-2024 08:33 PM