Populate list collector variable by another list collector variable

Ash41
Kilo Sage

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
}

 

1 ACCEPTED SOLUTION

Pratiksha2
Mega Sage

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

View solution in original post

4 REPLIES 4

Vishal Birajdar
Giga Sage

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.

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Pratiksha2
Mega Sage

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

Thank you, Pratiksha it worked

Hello @Ash41 -

You're Welcome!
Please mark my answer as Helpful ✔️
Happy to help you!😊

Thanks,
Pratiksha