Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Use Array of Location SYS_IDs to restrict the possible choices in a reference field (Catalog)

andrewbettc
Kilo Sage

Hi - We want to have a country field on a catalog item that, when selected, restricts the possible choices from a subsequent reference field. The problem is that we don't want to see every location from the country chosen. It's just a handful.

 

I have a ajax call that returns an array of sys_ids for the locations depending on the country selected. What is the best way to now use that influence the query in the locations field?

Or do I need to change the field type to choice?

1 ACCEPTED SOLUTION

Hi @andrewbettc ,

You should be able to use an advanced reference qualifier on the reference field to perform the filter of the list.

Jon23_0-1713890553794.png

 

In  your case, you would pass your form values to a script include to return the query.

 

For example:

Jon23_1-1713891334341.png

 

View solution in original post

4 REPLIES 4

andrewbettc
Kilo Sage

I've added a select box field and pushed my array into it. The problem is that I've now got a drop down box containing a load of sys_ids. How do I look up the labels and use them instead? My client script is on a VDI which I can't paste out of but I'll add it below.

As above, this is my current client script:

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

    var location = g_form.getValue('room_location');
    var populateBuilding = new GlideAjax('AvMeetingPopulateLocationAjax');
    populateBuilding.addParam('sysparm_name', 'populateLocation');
    populateBuilding.addParam('sysparm_location', location);
    populateBuilding.getXML(populateBuildingField);

}

function populateBuildingField(response) {
    g_form.clearOptions('room_building2'); //clears all choices from room_building2
    var answer = response.responseXML.documentElement.getAttribute('answer');
	g_form.addErrorMessage(answer);
    //answer = JSON.parse(answer)
	var locationArray = answer.split(',');

    for (var i = 0; i < locationArray.length; i++) {
        g_form.addOption('room_building2', locationArray[i], locationArray[i]);
    }

}

Hi @andrewbettc ,

You should be able to use an advanced reference qualifier on the reference field to perform the filter of the list.

Jon23_0-1713890553794.png

 

In  your case, you would pass your form values to a script include to return the query.

 

For example:

Jon23_1-1713891334341.png

 

Thank you Jon. In the end I updated the target variable to a select box. I'll near this in mind for next time.