Script Include

Kamva
Giga Guru

Hi Developers,

 

I am battling with script include, please assist by pointing out what am I doing wrong and a possible solution to the error. I have created a catalog item which I need an email address field to be auto-filled on the OnChange event of the requested for field.

 

Below is the UI of my catalog item:

Kamva_0-1707804137832.png

 

Below is my Script Include named GetUserEmailAjaxUtils:

var GetUserEmailAjaxUtils = Class.create();
GetUserEmailAjaxUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
	getUserEmail: function() {
        var userId = this.getParameter('sysparm_requested_for'); //value from the catalog item

		if (!gs.nil(userId)){	//if userID is not empty
			var requestedForUser = new GlideRecord('sys_user');
			if (requestedForUser.get(userId)){
				return requestedForUser.email;
			}
		}
		return 'empty address';
    }
});

 

Below is my catalog client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var getEmail = new GlideAjax('GetUserEmailAjaxUtils'); //referencing the script include
    getEmail.addParam('sysparm_name', 'getUserEmail'); //invoking the function in the script include
    getEmail.addParam('sysparm_requested_for', g_form.getValue('requested_for'));	//passing requested_for value function to the fuction
	getEmail.getXML(parseResponse);

	g_form.hideFieldMsg('e_mail_address');
	function parseResponse(response){
		var answerFromXML = response.responseXML.documentElement.getAttribute("answer");
		alert(answerFromXML);
		if (answerFromXML == "empty address"){
			g_form.showFieldMsg('e_mail_address', 'Failed to fetch email address', 'error');
		}else{
			g_form.setValue('e_mail_address', answerFromXML);
		}
	}
}

 

Regards,

Kamva

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Kamva ,

 

Intead use Autopopulate feature of ServiceNow It does not require any scripting. Open the email variable there will be a tab for auto populate. Select the requested for field there, table as user & dot walk field as email. You are done.

 

Thanks,

Danish 

View solution in original post

8 REPLIES 8

Sonam_Tiwari
Kilo Sage

Is the alert empty?

 

Does the user have email updated in the user's record?

 

 

Consider indicating the response as helpful and marking it as correct if it meets your needs.

Hi @Sonam_Tiwari,

 

The alert produces null (as shown below), and yes users have their email addresses updated in the user table

Kamva_0-1707804845933.png

 

Please log 

userId 

 in your script include and check if you are getting the userid correctly from client side.

 

Also, better keep 

  //getEmail.addParam('sysparm_requested_for', g_form.getValue('requested_for'));
getEmail.addParam('sysparm_requested_for', newValue);

 

 

Consider indicating the response as helpful and marking it as correct if it meets your needs.

Service_RNow
Mega Sage

Hi @Kamva 

please try to this code.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var name = g_form.getValue('requested_for');
    var user = new GlideAjax('global.script INclude Name');
    user.addParam('sysparm_name', 'getuserEmail');
    user.addParam('sysparm_usrsysid', name);
    user.getXML(getData);
}

function getData(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    var data = JSON.parse(answer);
    g_form.setValue('email', data.email);

}
getuserEmail: function() {
        var obj = {};
        var sysid = this.getParameter('sysparm_usrsysid');
        var grusr = new GlideRecord("sys_user");
        grusr.addQuery('sys_id', sysid);
        grusr.query();
        if (grusr.next()) {
			
            obj.email = grusr.getValue('email');
        }
        return JSON.stringify(obj);
    },

 

Please Mark Correct answer if it will help you.