Help with Catalog Client Script

Jen11
Tera Expert

I need help with a catalog client script.  For the ''Operational work type' drop down, whatever that is selected there, I would like the 'Work category' drop down to to change to the same thing.  The choices for both the drop down are the same, but the value is different. 

 

Jen11_0-1739574152856.png

 

Thank you in advance for any help. 

5 REPLIES 5

AshishKM
Kilo Patron

Hi @Jen11 , 

So the label and value pairs are same for both select box but records are different in Question Choice [question_choice] table.

 

Write onChange client script on  Operational Work Type field and read the current value using below code

var opWorkType = g_form.getValue('operation_work_type'); // update with actual variable name

 

and then set the work category with that value using the same value 

g_form.setValue('work_category',opWorkType); // update with actual variable name for work category

 

Please test and let us know if works for you or not.

 

-Thanks,

AshishKM

 

the label is same, but the value is differnt. 

Barath P
Kilo Sage

Hi @Jen11,

Please refer to the screenshot below.

To proceed, go to the Catalog Client Script section and create a new script using the same code provided below.

Let me know if you'd like any further adjustments!

BarathP_0-1739593328046.png

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thanks & Regards,

Barath.P

 

PoojashreeP
Kilo Sage

Hi @Jen11 ,

 

This is a scalable code that will also be helpful in the future if any new choices are added or removed.

Client Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    // Exit if the form is loading or the new value is empty
    if (isLoading || newValue === '') {
        return;
    }

    var choiceValue = g_form.getValue('u_operational_work_type');
    var choiceLabel = g_form.getOption('u_operational_work_type', choiceValue).text;
    var tableName = g_form.getTableName();
    var element = 'u_work_category';

    // Create an AJAX request to fetch the label of the category
    var ajax = new GlideAjax('global.getLabelOfCategory');
    ajax.addParam('sysparm_name', 'getValue');
    ajax.addParam('label', choiceLabel);
    ajax.addParam('table', tableName);
    ajax.addParam('elementName', element);
    
    // Call the callback function once the response is received
    ajax.getXML(callMe);
}

function callMe(response) {
    // Retrieve the answer from the response and set it in the form
    var answer = response.responseXML.documentElement.getAttribute("answer");
    g_form.setValue('u_work_category', answer);
}

 

 

Client Callable Script Include:

 

 

 

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

    // Method to get the value of the category label
    getValue: function() {
        var choiceLabel = this.getParameter('label');
        var table = this.getParameter('table');
        var element = this.getParameter('elementName');

        var choiceGr = new GlideRecord('sys_choice');
        choiceGr.addQuery('name', table);
        choiceGr.addQuery('element', element);
        choiceGr.addQuery('label', choiceLabel);
        choiceGr.query();

        // Return the value if found
        if (choiceGr.next()) {
            return choiceGr.value.toString();
        }
    },

    type: 'getLabelOfCategory'
});

 

If this answers your query, please mark it as an accepted solution or helpful!

 

Best Regards,

Pooja