Updating values on a catalogue item based on the Business Owner field

matthew_hughes
Kilo Sage

In ServiceNow, I've got a requirement on a catalogue item where I need to update the value of to fields based on the value of another field:

matthew_hughes_0-1765807981436.png

For my catalogue item, when a user updates the New Business Owner field, I want the New Business Owning Business Unit of Function and New Business Owning Platform fields to be updated if the New Business Owner's Department doesn't currently align with the current fields on the left handside. 

 

New Business Owning Business Unit of Function can have the department type of 'Function' or 'Business Unit'. This can have children departments with the type of 'Platform'

 

I've written the below function in my script include:

 

checkDepartmentsAlign(newOwner, currentBusinessUnit, currentFunction) {
    var requiredDepartment = newOwner.department;
    var grDepartMentValidation = new GlideRecord('cmn_department');
    grDepartMentValidation.addQuery('sys_id', requiredDepartment.sys_id);
    grDepartMentValidation.addEncodedQuery("parent=" + currentPlatform + "^parent.parent=" + currentFunction + "^NQparent.parent=" + currentPlatform + "^parent.parent.parent=" + currentFunction + "^NQparent.parent.parent=" + currentPlatform + "^parent.parent.parent.parent=" + currentFunction);
    grDepartMentValidation.query();
    if (grDepartMentValidation.next()) {
        return true;
    } else {
        return false;
    }
},
 
I've written the below onChange Client Script:
matthew_hughes_1-1765808340541.png

 

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

    var deptId = 'new_business_owner.department';
    var currentBU = 'current_business_owning_business_unit_or_function';
    var currentPF = 'current_business_owning_platform';
   
    var ga = new GlideAjax('LBGBusinessAppCatItems');
    ga.addParam('sysparm_name', 'checkDepartmentsClient');
    ga.addParam('sysparm_department', newValue);
    ga.addParam('sysparm_bu', currentBU);
    ga.addParam('sysparm_platform', currentPF);
    ga.getXML('departmentResult');

    function departmentResult(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer === 'false') {
            g_form.setValue('new_business_owning_business_unit_or_function', deptId.parent);
            g_form.showFieldMsg('new_business_application_name', 'This Business Unit now aligns with the new Business Owner', 'error', true);
            g_form.setValue('new_business_owning_platform', deptId.parent.parent);
            g_form.showFieldMsg('new_business_application_name', 'This Platform now aligns with the new Business Owner', 'error', true);
        }
    }
}
 
If somebody could advise me on what I would need to do, that would be much appreciated
3 REPLIES 3

Brad Bowman
Kilo Patron

Take another look at the GlideAjax guide.  You're not getting the parameters from the client script correctly, and make sure your Script Include is client callable with the extended object included for starters:

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

Mohammed8
Giga Sage

Hi @matthew_hughes ,

 

One thing i observe is  you have assigned field names?  not the actual field values.

In a catalog client script, you should always use g_form.getValue() to retrieve values and g_form.setValue() to update them.

 

var deptId = 'new_business_owner.department';

var currentBU = 'current_business_owning_business_unit_or_function';

var currentPF = 'current_business_owning_platform';

.....
 
 
Regards,
Mohammed Zakir

Sandeep Rajput
Tera Patron

@matthew_hughes This looks like an AI generated code to me as following lines used in the client script seems incorrect.

 

    var deptId = 'new_business_owner.department';
    var currentBU = 'current_business_owning_business_unit_or_function';
    var currentPF = 'current_business_owning_platform';

 

Also, the script include seems to be a server side script include and doesn't seem to be client callable. As recommended by fellow members. Please go through the GlideAjax and Script include tutorials.