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

Hi @Service_RNow,

 

I have tried your code it produces an error

Kamva_0-1707806296534.png

 

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 

Sonam_Tiwari
Kilo Sage

If you are on or above Utah, do what @Danish Bhairag2  has suggested.

 

 

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

SANDEEP28
Mega Sage

@Kamva  This can be done without scripting. You should use "Auto populate" feature available in variable . Open the "Email" variable and go to "Auto populate" section. 

 

In dependent question, select "Requested for" and then select "Email" in Dot walk path.

 

SANDEEP28_0-1707806621921.png

 

Go for OOB solutions instead of scripting and its very easy configure.

 

I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!