Scope AJAX Script Include and Catalog Client Script

Luis Roman1
Tera Contributor

I have the following Ajax Scripts (Include and Client) in a scope application. For some reason I am getting a browser error: There is a JavaScript error in your browser console. Can you please advise me how to addressed this problem. I am in a lost and not sure how to proceed forward.  Help appreciated.

find_real_file.png

Catalog client script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
if (newValue === 'true') {
    alert('onChangeTelecommuter_telecommuter =true= ' + newValue);
    g_form.clearValue('building');
    g_form.clearValue('column');
    g_form.clearValue('floor');
    g_form.clearValue('room_cube');
} else {
    alert('onChangeTelecommuter_telecommuter =False= ' + newValue);
        var sys_id_asset = g_form.getValue('asset');
        var ga = new GlideAjax('x_g_lme_ham_classi.getStandardFields'); //this is the script include
        ga.addParam("sysparm_name", "getFields"); //this is the function within the script include
        ga.addParam("sysparm_asset", sys_id_asset);
        ga.getXML(getResponse);
    }
}

    function getResponse(response) {
        var values = response.responseXML.documentElement.getAttribute('answer').toString().split('|');
        g_form.setValue('location', values[0]);
        g_form.setValue('building', values[1]);
        g_form.setValue('floor', values[2]);
        g_form.setValue('column', values[3]);
        g_form.setValue('room_cube', values[4]);
    }

Script Include:

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

getFields : function() {
var assetID = this.getParameter('sysparm_asset');
var standardFields = new GlideRecord('alm_asset');
standardFields.addQuery('sys_id',assetID);
 standardFields.query();
if(standardFields.next()) {
return standardFields.location.getDisplayValue()+ '|' +
standardFields.u_building.getDisplayValue()+'|' +
standardFields.u_floor.getDisplayValue()+ '|'+
standardFields.u_column.getDisplayValue()+ '|'+
standardFields.u_room_cube.getDisplayValue();
}
return '';
},
    type: 'getStandardFields'
});

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi @Luis Roman 

try to use getXMLAnswer() in client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue === 'true') {
        alert('onChangeTelecommuter_telecommuter =true= ' + newValue);
        g_form.clearValue('building');
        g_form.clearValue('column');
        g_form.clearValue('floor');
        g_form.clearValue('room_cube');
    } else {
        alert('onChangeTelecommuter_telecommuter =False= ' + newValue);
        var sys_id_asset = g_form.getValue('asset');
        var ga = new GlideAjax('x_g_lme_ham_classi.getStandardFields'); //this is the script include
        ga.addParam("sysparm_name", "getFields"); //this is the function within the script include
        ga.addParam("sysparm_asset", sys_id_asset);
        ga.getXMLAnswer(getResponse);
    }
}

function getResponse(response) {
    var values = response.toString().split('|');
    g_form.setValue('location', values[0]);
    g_form.setValue('building', values[1]);
    g_form.setValue('floor', values[2]);
    g_form.setValue('column', values[3]);
    g_form.setValue('room_cube', values[4]);
}

Update script include as this since you are in scope application

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

getFields : function() {
var assetID = this.getParameter('sysparm_asset');
var standardFields = new GlideRecord('alm_asset');
standardFields.addQuery('sys_id',assetID);
 standardFields.query();
if(standardFields.next()) {
return standardFields.location.getDisplayValue()+ '|' +
standardFields.u_building.getDisplayValue()+'|' +
standardFields.u_floor.getDisplayValue()+ '|'+
standardFields.u_column.getDisplayValue()+ '|'+
standardFields.u_room_cube.getDisplayValue();
}
return '';
},
    type: 'getStandardFields'
});

Regards
Ankur

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

View solution in original post

5 REPLIES 5

Aman Kumar S
Kilo Patron

Can you confirm if your script include has "Accessible from" field set as "All application scopes"?

Are you getting the alert that you have in your client script?

Best Regards
Aman Kumar

Yes, the "All application scopes" is set up. The message is in the browser.

find_real_file.png

Kumar, If look at the inspect in the browser if have the following message. Kumar thank you for collaboration and education is appreciated.  Need anything else please let me know.

find_real_file.png

Also I am getting the message below.

find_real_file.png

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi @Luis Roman 

try to use getXMLAnswer() in client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue === 'true') {
        alert('onChangeTelecommuter_telecommuter =true= ' + newValue);
        g_form.clearValue('building');
        g_form.clearValue('column');
        g_form.clearValue('floor');
        g_form.clearValue('room_cube');
    } else {
        alert('onChangeTelecommuter_telecommuter =False= ' + newValue);
        var sys_id_asset = g_form.getValue('asset');
        var ga = new GlideAjax('x_g_lme_ham_classi.getStandardFields'); //this is the script include
        ga.addParam("sysparm_name", "getFields"); //this is the function within the script include
        ga.addParam("sysparm_asset", sys_id_asset);
        ga.getXMLAnswer(getResponse);
    }
}

function getResponse(response) {
    var values = response.toString().split('|');
    g_form.setValue('location', values[0]);
    g_form.setValue('building', values[1]);
    g_form.setValue('floor', values[2]);
    g_form.setValue('column', values[3]);
    g_form.setValue('room_cube', values[4]);
}

Update script include as this since you are in scope application

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

getFields : function() {
var assetID = this.getParameter('sysparm_asset');
var standardFields = new GlideRecord('alm_asset');
standardFields.addQuery('sys_id',assetID);
 standardFields.query();
if(standardFields.next()) {
return standardFields.location.getDisplayValue()+ '|' +
standardFields.u_building.getDisplayValue()+'|' +
standardFields.u_floor.getDisplayValue()+ '|'+
standardFields.u_column.getDisplayValue()+ '|'+
standardFields.u_room_cube.getDisplayValue();
}
return '';
},
    type: 'getStandardFields'
});

Regards
Ankur

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

Ankur, yes the script is fixed. Now the only confusing aspect is that building and location are not displaying. I wonder if it is because they are reference fields and I am not pulling the right value. The floor, column and room_cube are displayed correctly.  Any thoughts regarding the building and location!!!!