Get Group Email upon select group - Script Include / Catalog Client Script

Edxavier Robert
Mega Sage

Hi, I am currently working on a catalog item where the user selects a group, and I wanted to populate the email group on another field. 

 

The first field is called name which is a reference to the sys_user_group table and the field email is where I want the email of the group selected. This is what I got so far: 

 

Script Include: 

 

var getGroupEmail = Class.create();
getGroupEmail.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getData: function() {
	var recordGroup = this.getParameter('sysparm_group');
	var group = new GlideRecord('sys_user_group');
	group.get(recordGroup);
	var result = this.newItem('result');

	result.setAttribute('email', group.email);	
	
}

   // type: 'getGroupEmail'
});

 

 

And this is the catalog client script that I have: 

 

function onChange(control, oldValue, newValue, isLoading) {
       if (isLoading || newValue == '') {
         return;
     }
    if (newValue == '') {
        return;
    }
    var group = g_form.getDisplayValue('name');
	alert(group);
		
    var ga = new GlideAjax('getGroupEmail');
    ga.addParam('sysparm_name', 'getData');
    ga.addParam('sysparm_group', group);
    ga.getXML(ajaxResponse);

    function ajaxResponse(serverResponse) {
        var result = serverResponse.responseXML.getElementsByTagName('result');
		
		g_form.setValue('email', result);
    }
}

 

 

In the email field, I am getting: [object HTML.Collection] 

EdxavierRobert_0-1699905175777.png

 

1 ACCEPTED SOLUTION

Edxavier Robert
Mega Sage

I figured out that, no need to go with the script included. I just created this other catalog client script 

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var result = g_form.getDisplayValue('name');
   // alert(result);

    var gr = new GlideRecord('sys_user_group');
    gr.addQuery('name', result);
    gr.query(myCallbackFunction);

    function myCallbackFunction(gr) {
        while (gr.next()) {
           // alert(gr.email);
            g_form.setValue('email', gr.email);
        }
    }
}

 

 

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Try returning the email field, forced to a string:

var getGroupEmail = Class.create();
getGroupEmail.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getData: function() {
	var answer = '';
        var recordGroup = this.getParameter('sysparm_group');
	var group = new GlideRecord('sys_user_group');
	if (group.get(recordGroup)){
	    answer = group.email.toString();
    }
    return answer;
}

   // type: 'getGroupEmail'
});

Then use 'answer' in the client script callback function instead of result.

 

Note that in the Utah version and later you can do this without the scripts via the auto populate feature

https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-referenc... 

Hi Brad, thanks for your reply. I tried but it didn't work. 

Edxavier Robert
Mega Sage

I figured out that, no need to go with the script included. I just created this other catalog client script 

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var result = g_form.getDisplayValue('name');
   // alert(result);

    var gr = new GlideRecord('sys_user_group');
    gr.addQuery('name', result);
    gr.query(myCallbackFunction);

    function myCallbackFunction(gr) {
        while (gr.next()) {
           // alert(gr.email);
            g_form.setValue('email', gr.email);
        }
    }
}