Client Script doesn't work on Service Operation Workspace.

Community Alums
Not applicable
Hi All,
The client scripts which are working in normal default view of forms aren't working in Service Operation Workspace(UI).
Here, I am implementing an on Load client script - on change of multiple fields to set the short description every time any of the fields changes. by using get control.
 
For example - the below script is not working. 
 
function onLoad() {
    //Type appropriate comment here, and begin script below

    g_form.getControl('category').observe('change', function() {
        updateShortDescription();
    });

    g_form.getControl('subcategory').observe('change', function() {
        updateShortDescription();
    });
    g_form.getControl('u_item').observe('change', function() {
        updateShortDescription();
    });
    g_form.getControl('u_zone').observe('change', function() {
        updateShortDescription();
    });
    g_form.getControl('u_store_number').observe('change', function() {
        updateShortDescription();
    });
    g_form.getControl('u_city').observe('change', function() {
        updateShortDescription();
    });
    g_form.getControl('u_state').observe('change', function() {
        updateShortDescription();
    });
}

function updateShortDescription() {
    var category = g_form.getValue('category');
    var subcategory = g_form.getValue('subcategory');
    var item = g_form.getValue('u_item');
    var zone = g_form.getValue('u_zone');
    var store = g_form.getValue('u_store_number');
    var city = g_form.getValue('u_city');
    var state = g_form.getValue('u_state');
    var template = g_form.getValue('u_selected_template');
   
    var shortDescription = '';

    if (category) {
        shortDescription = 'BK'+ ' - ' + template + '- ' + zone + ' - ' + 'Store' + ' ' + store + ' - '+ '(' + city + ' , ' + state + ')';
        if (subcategory) {
            shortDescription = 'BK'+ ' - ' + template + '- ' + zone + ' - ' + 'Store' +' '+ store + ' - '+ '(' + city + ' , ' + state + ')' + ' - ' + subcategory;
            if (item) {
                shortDescription = 'BK'+ ' - ' + template + '- ' + zone + ' - ' + 'Store' + ' ' +store + ' - '+ '(' + city + ' , ' + state + ')' + ' - ' + subcategory +' - ' + item ;
            }
        }
    } else {
        shortDescription = 'AUTOMATICALLY SET WHEN ITEM IS MODIFIED';
    }

    g_form.setValue('short_description', shortDescription);
}
 

Does anyone have any idea why this would happen?

Thank you in advance!

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Sorry for not posting earlier. I forgot to post the answer at that time. below are the raw code - 

Thank You.

 

var load_data = {
 'u_reference_1': [{
'value': "" }],
 'company': [{
 'value': ""}],
 'u_city': [{
'value': ""}],
 'u_state_province': [{
 'value': ""}],
'category': [{
'value': "" }],
'subcategory': [{
'value': ""}],
 'u_item': [{
 'value': ""}],};

function onLoad() {
var unregister = g_form.onUserChangeValue(user_selected_field_value);
var short_description = g_form.getValue('short_description');
if (short_description != "") {
var split_short_description = short_description.split("-");
var sity_province = split_short_description[3].replace(/[()]/g, '').split(',');
load_data['u_reference_1'][0].value = split_short_description[0];
load_data['company'][0].value = split_short_description[1] + "-" + split_short_description[2];
 load_data['u_city'][0].value = sity_province[0];
load_data['u_state_province'][0].value = sity_province[1];
load_data['category'][0].value = split_short_description[4];
load_data['subcategory'][0].value = split_short_description[5];
 load_data['u_item'][0].value = split_short_description[6];}}

function user_selected_field_value(filedvalue, originalValue, newValue) {
if (filedvalue == "company") {
var run_after_company_selected = setTimeout(function() {
update_company_details(newValue);}, 1000);
 }
 if (filedvalue == "u_reference_1") {
 if (newValue) {
 g_form.setValue('company','');
 load_data['company'][0].value = " ";
var parentAccount = g_form.getReference('u_reference_1', parentAccount_fun);
} else {
 load_data['u_reference_1'][0].value = " ";
 concate_data(); }
} else { if (filedvalue != "company") {
if (load_data.hasOwnProperty(filedvalue)) {
load_data[filedvalue][0].value = newValue;
concate_data();}}}}

function parentAccount_fun(parentAccount) {
 load_data['u_reference_1'][0].value = parentAccount.name;
concate_data();}

function update_company_details(compeny_sysid) {
var script_include = new GlideAjax('global.incident_shortdescription_details');
 script_include.addParam('sysparm_name', 'getDetails');
 script_include.addParam('Account_sysid', compeny_sysid);
script_include.addParam('Paccount_sysid', g_form.getValue("u_reference_1"));
script_include.getXML(function(response) {
 var account = response.responseXML.documentElement.getAttribute("answer");
 var data = account.split(",");
load_data.company[0].value = data[0];
load_data.u_reference_1[0].value = data[1];
load_data.u_state_province[0].value = g_form.getValue('u_state_province');
 load_data.u_city[0].value = g_form.getValue('u_city');
concate_data();  });}

function concate_data() {
var category = load_data['category'][0].value;
var u_item = load_data['u_item'][0].value;
var u_city = load_data['u_city'][0].value;
var u_state_province = load_data['u_state_province'][0].value;
var company = load_data['company'][0].value;
 var u_parent_account = load_data['u_reference_1'][0].value;
 var subcategory = load_data['subcategory'][0].value;
var testmy = u_parent_account + " - " + company + " - (" + u_city + "," + u_state_province + ")" + " - " + category + " - " + subcategory + " - " + u_item;
 g_form.setValue('short_description', testmy);}

 

 

View solution in original post

11 REPLIES 11

Is your client script working on Service Operations Workspace?

 

Have you found the solution? We have 3 level categories: Top category, category and subcategory. We write client script: onChange, to display subcategory. It works fine in incident form on platform, but it doesn't work on SOW. 

In script onChange, we get subcategories by Ajax call and display it.  I have checked "Isolate script" CheckBox too.

1. Ajax call - get subcategories

2. Function below to display drop-down. Data isn't loaded to the drop-down

function populateIssue(response){
    g_form.clearOptions('u_issue');
    var answer = response.responseXML.documentElement.getAttribute("answer");
    var answer1 = JSON.parse(answer);
    g_form.addOption('u_issue','','-- None --',0);
    for(var i = 0; i < answer1.length; i++) {
             g_form.addOption('u_issue', answer1[i], answer1[i], i+1);
  }
}

Sumanth16
Kilo Patron

Hi @Community Alums ,

 

Set the "Isolate Script" field on the client script to "True" and that should help resolve the issue.

 

Please refer to below articles:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB1220159

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0960131

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

Thank you for reply. 

I have Set the "Isolate Script" field on the client script to "True"

I notice SOW doesn't support "response.responseXML.documentElement.getAttribute("answer");"

Does anyone know how to get the return from GlideAjax?

Community Alums
Not applicable

Sorry for not posting earlier. I forgot to post the answer at that time. below are the raw code - 

Thank You.

 

var load_data = {
 'u_reference_1': [{
'value': "" }],
 'company': [{
 'value': ""}],
 'u_city': [{
'value': ""}],
 'u_state_province': [{
 'value': ""}],
'category': [{
'value': "" }],
'subcategory': [{
'value': ""}],
 'u_item': [{
 'value': ""}],};

function onLoad() {
var unregister = g_form.onUserChangeValue(user_selected_field_value);
var short_description = g_form.getValue('short_description');
if (short_description != "") {
var split_short_description = short_description.split("-");
var sity_province = split_short_description[3].replace(/[()]/g, '').split(',');
load_data['u_reference_1'][0].value = split_short_description[0];
load_data['company'][0].value = split_short_description[1] + "-" + split_short_description[2];
 load_data['u_city'][0].value = sity_province[0];
load_data['u_state_province'][0].value = sity_province[1];
load_data['category'][0].value = split_short_description[4];
load_data['subcategory'][0].value = split_short_description[5];
 load_data['u_item'][0].value = split_short_description[6];}}

function user_selected_field_value(filedvalue, originalValue, newValue) {
if (filedvalue == "company") {
var run_after_company_selected = setTimeout(function() {
update_company_details(newValue);}, 1000);
 }
 if (filedvalue == "u_reference_1") {
 if (newValue) {
 g_form.setValue('company','');
 load_data['company'][0].value = " ";
var parentAccount = g_form.getReference('u_reference_1', parentAccount_fun);
} else {
 load_data['u_reference_1'][0].value = " ";
 concate_data(); }
} else { if (filedvalue != "company") {
if (load_data.hasOwnProperty(filedvalue)) {
load_data[filedvalue][0].value = newValue;
concate_data();}}}}

function parentAccount_fun(parentAccount) {
 load_data['u_reference_1'][0].value = parentAccount.name;
concate_data();}

function update_company_details(compeny_sysid) {
var script_include = new GlideAjax('global.incident_shortdescription_details');
 script_include.addParam('sysparm_name', 'getDetails');
 script_include.addParam('Account_sysid', compeny_sysid);
script_include.addParam('Paccount_sysid', g_form.getValue("u_reference_1"));
script_include.getXML(function(response) {
 var account = response.responseXML.documentElement.getAttribute("answer");
 var data = account.split(",");
load_data.company[0].value = data[0];
load_data.u_reference_1[0].value = data[1];
load_data.u_state_province[0].value = g_form.getValue('u_state_province');
 load_data.u_city[0].value = g_form.getValue('u_city');
concate_data();  });}

function concate_data() {
var category = load_data['category'][0].value;
var u_item = load_data['u_item'][0].value;
var u_city = load_data['u_city'][0].value;
var u_state_province = load_data['u_state_province'][0].value;
var company = load_data['company'][0].value;
 var u_parent_account = load_data['u_reference_1'][0].value;
 var subcategory = load_data['subcategory'][0].value;
var testmy = u_parent_account + " - " + company + " - (" + u_city + "," + u_state_province + ")" + " - " + category + " - " + subcategory + " - " + u_item;
 g_form.setValue('short_description', testmy);}