Script include not setting catalogue variable

Steven Watts2
Tera Guru

Hi,

Trying to set a variable on a catalogue item automatically via a script include which is triggered then the value of another variable is set, onChange. The variable isn't being set but i'm not seeing anything in the logs either. Anyone got any ideas?

SI:

var getUserDetails = Class.create();
getUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    retrieveDetails: function() {

        var user = this.getParameter('sysparm_businessOwner');
        obj = {};

        var getUser = new GlideRecord("sys_user");

        if (getUser.get(user)) {
            obj.email = getUser.getValue('email');
        }
        return JSON.stringify(obj);
    },

CCS:

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

    var ajaxCall = new GlideAjax("getUserDetails"); //Name of the SI
    ajaxCall.addParam('sysparm_name', 'retrieveDetails'); //Name of the function being called
    ajaxCall.addParam('sysparm_businessOwner', 'business_owner'); //newValue will be the value stored in business_owner attribute
    ajaxCall.getXMLAnswer(getResponse); //Callback funtion to retrieve attributes from the user record

    function getResponse(response) {
        var answer = JSON.parse(response);
        g_form.setValue('business_owner_email', answer.email);
    }
}
    type: 'getUserDetails'
});
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

there is no need to use JSON object as you are just returning single value from script include

I hope your script include is client callable and both the client script + script include are in same scope

update script include as this

var getUserDetails = Class.create();
getUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	retrieveDetails: function() {
		var user = this.getParameter('sysparm_businessOwner');
		var email;
		var getUser = new GlideRecord("sys_user");
		if (getUser.get(user)) {
			email = getUser.getValue('email');
		}
		return email;
	},

	type: 'getUserDetails'
});

Client Script: Ensure your ajax runs only when newvalue != oldValue

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

	if(newValue == '')
		g_form.clearValue('business_owner_email');

	if(oldValue != newValue){
		var ajaxCall = new GlideAjax("getUserDetails"); //Name of the SI
		ajaxCall.addParam('sysparm_name', 'retrieveDetails'); //Name of the function being called
		ajaxCall.addParam('sysparm_businessOwner', newValue); 
		ajaxCall.getXMLAnswer(function(answer){
			if(answer != '')
				g_form.setValue('business_owner_email', answer);
		});
	}
}

You can also use Catalog Lookup Definition for this and no scripting will be required

Catalog Data Lookup Definition on any table, eliminating Catalog Client Scripting

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Allen Andreas
Administrator
Administrator

Hello,

You can add log statements to both client (alert/console) and the script include (gs.info()) to check your values.

I'm unsure what you mean by 'business_owner' will contain the newValue value...you're not sending over the value of whatever field has been selected for this onChange client script. You'd want to ensure it's a sys_id (i.e., reference field value). So use... newValue there? or g_form.getValue('name_of_field').

You also wouldn't need to build an object to send back to the client script, but what you have should be fine as you're stringifying it anyway and it's being set using getValue as well.

Please use appropriate log statements to check your values and such.

Also, using "getUserDetails" may conflict as I believe there is another script include that is named that/uses that and that is commonly a used word. You may want to try something else (recreate), just throwing that out there.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi,

My apologies if my reply above wasn't at least Helpful.

Glad you found a correct answer that worked for you.

Take care! 🙂


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

there is no need to use JSON object as you are just returning single value from script include

I hope your script include is client callable and both the client script + script include are in same scope

update script include as this

var getUserDetails = Class.create();
getUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	retrieveDetails: function() {
		var user = this.getParameter('sysparm_businessOwner');
		var email;
		var getUser = new GlideRecord("sys_user");
		if (getUser.get(user)) {
			email = getUser.getValue('email');
		}
		return email;
	},

	type: 'getUserDetails'
});

Client Script: Ensure your ajax runs only when newvalue != oldValue

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

	if(newValue == '')
		g_form.clearValue('business_owner_email');

	if(oldValue != newValue){
		var ajaxCall = new GlideAjax("getUserDetails"); //Name of the SI
		ajaxCall.addParam('sysparm_name', 'retrieveDetails'); //Name of the function being called
		ajaxCall.addParam('sysparm_businessOwner', newValue); 
		ajaxCall.getXMLAnswer(function(answer){
			if(answer != '')
				g_form.setValue('business_owner_email', answer);
		});
	}
}

You can also use Catalog Lookup Definition for this and no scripting will be required

Catalog Data Lookup Definition on any table, eliminating Catalog Client Scripting

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks,

Hadn't come across the Catalog Data Lookup Definitions before, so much simpler :).