Catalog client script and script include

2022_ServiceNow
Tera Expert

Hi all,

I have a requirement in catalog item.

Script Include:

 

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

    getDetails: function() {
        var value;
        var sysid = this.getParameter("sysparm_sysid");
        var check = new GlideRecord("u_conference_room");
        check.addQuery("sys_id", sysid);
        check.query();
        if (check.next()) {
            value = check.u_room_configuration; //field name to get
            return value;
        }
    },
    type: 'autoPopulateDetails'
});

 

On change catalog client script:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax("autoPopulateDetails"); //script include
    ga.addParam("sysparm_name", "getDetails"); //function
    ga.addParam("sysparm_sysid", newValue); //paramter pass to server
    ga.getXMLAnswer(setdetails); //callback funtion
    function setdetails(response) {
        var values = response.split('\n');
//         g_form.clearOptions("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room");
        for (var i = 0; i < values.length; i++) {
            g_form.addOption("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room", values[i], values[i]);
        }
    }
}

 

 

There are 2 question fields.

Variable 1 and Variable 2

when we chose the options in variable 1, based on the values selected in the variable 1, it displays the options for the same value stored in the respective field for variable 2. It is working as expected for one time.

But if we want to change the variable 1 value, in the variable 2 it displays the value for the selected options along with the previously selected value. I don't want this to happen? We want to display the values only based on the values selected in variable 1.

What changes has to be done in the script? Anybody please let me know.

Thanks in advance!!!

@Manmohan K @Ankur Bawiskar @Amit Gujarathi @Karan Chhabra6 

4 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@2022_ServiceNow 

then clear the options whenever onchange runs and then add the option from script

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

View solution in original post

Sagar Pagar
Tera Patron

Hi @2022_ServiceNow,

Try this updated scripts. just remove the comment for ckearOptions() method.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax("autoPopulateDetails"); //script include
    ga.addParam("sysparm_name", "getDetails"); //function
    ga.addParam("sysparm_sysid", newValue); //paramter pass to server
    ga.getXMLAnswer(setdetails); //callback funtion
    function setdetails(response) {
        var values = response.split('\n');
        g_form.clearOptions("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room");

        for (var i = 0; i < values.length; i++) {
            g_form.addOption("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room", values[i], values[i]);
        }
    }
}

 

Thanks,

Sagar Pagar

The world works with ServiceNow

View solution in original post

Hi @2022_ServiceNow,

If you need None as options you can add it after clearOptions() functions.

 

Updated scripts:

 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	var ga = new GlideAjax("autoPopulateDetails"); //script include
	ga.addParam("sysparm_name", "getDetails"); //function
	ga.addParam("sysparm_sysid", newValue); //paramter pass to server
	ga.getXMLAnswer(setdetails); //callback funtion
	function setdetails(response) {
		var values = response.split('\n');
		g_form.clearOptions("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room");
		g_form.addOption("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room", "", "None"); // corrected label and value sequence

		for (var i = 0; i < values.length; i++) {
			g_form.addOption("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room", values[i], values[i]);
		}
	}
}

 

 

Updated: corrected the label and value sequence/order in addOption() method.

 

Thanks,

Sagar Pagar

The world works with ServiceNow

View solution in original post

@2022_ServiceNow 

add none like this

g_form.addOption('please_confirm_the_seating_arrangements_for_your_meeting_in_this_room', '' , '-- None --'); 
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@2022_ServiceNow 

then clear the options whenever onchange runs and then add the option from script

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

@Ankur Bawiskar Thank you for response.

I have added clear option, it's working. 

In the variable I have checked 'include none' under type specifications, but in the options, it's not displaying none instead it directly selects the first option. How can we add none option so that the user can choose the options and not directly populate.

@2022_ServiceNow 

add none like this

g_form.addOption('please_confirm_the_seating_arrangements_for_your_meeting_in_this_room', '' , '-- None --'); 
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Sagar Pagar
Tera Patron

Hi @2022_ServiceNow,

Try this updated scripts. just remove the comment for ckearOptions() method.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax("autoPopulateDetails"); //script include
    ga.addParam("sysparm_name", "getDetails"); //function
    ga.addParam("sysparm_sysid", newValue); //paramter pass to server
    ga.getXMLAnswer(setdetails); //callback funtion
    function setdetails(response) {
        var values = response.split('\n');
        g_form.clearOptions("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room");

        for (var i = 0; i < values.length; i++) {
            g_form.addOption("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room", values[i], values[i]);
        }
    }
}

 

Thanks,

Sagar Pagar

The world works with ServiceNow