Populate the Data through On change client script is not working

Vamsi26
Tera Contributor

Hello Everyone

My below script is not populating the Data on Custom table 

 

I want to populate the KB article number on custom table which contains the field reference to sc_cat_item

Vamsi26_1-1736186254701.png

 

I have this relation available on the related list for every Catalog item through "sc_2_kb" relationship table

 

Vamsi26_0-1736186172890.png

 

Script Include

 

var GetKnowledge = Class.create();
GetKnowledge.prototype.prototype = {
    initialize: function() {},
    getKBname: function(catalogItemSysId) {
        var KBGR = new GlideRecord('sc_2_kb');
        KBGR.addQuery('sc_cat_item', catalogItemSysId); // Reference field to Relation table
        KBGR.query();
        if (KBGR.next()) {
            return KBGR.kb_knowledge; // Return the Knowledge
        }
    },
    type: 'GetKnowledge'
};

 

 

Client Script

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('GetKnowledge');
    ga.addParam('sysparm_name', 'getKBname'); // Is Function in the script include 
    ga.addParam('sys_id', newValue);
    ga.getXML(fetchData);

    function fetchData(response) {
        var getKB = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('u_knowledge_name', getKB);

    }

}

 


Thanks

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Your Script Include is not Client callable.  You can check the box, but it will likely not change the formatting for an existing script, so here's what it should look like - also getting the parameter from the Client Script instead of passed in as an argument, which you would use when calling the Script Include from a UI Action, Business Rule, reference qualifier, etc.  The Parameter name will probably work, but it would be best to rename it in both places to something consistent and not system/reserved like 'sysparm_sysid'.  It's also best to always return something from a server script, to avoid browser console errors and aid in troubleshooting - to determine if the script even ran.  This will get you closer.

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

    getKBname: function() {
		var answer = 'false';
		var catalogItemSysId = this.getParameter('sys_id')
        var KBGR = new GlideRecord('sc_2_kb');
        KBGR.addQuery('sc_cat_item', catalogItemSysId); // Reference field to Relation table
        KBGR.query();
        if (KBGR.next()) {
            answer = KBGR.kb_knowledge; // Return the Knowledge
        }
		return answer;
    },
  
    type: 'GetKnowledge'
});

View solution in original post

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

Your Script Include is not Client callable.  You can check the box, but it will likely not change the formatting for an existing script, so here's what it should look like - also getting the parameter from the Client Script instead of passed in as an argument, which you would use when calling the Script Include from a UI Action, Business Rule, reference qualifier, etc.  The Parameter name will probably work, but it would be best to rename it in both places to something consistent and not system/reserved like 'sysparm_sysid'.  It's also best to always return something from a server script, to avoid browser console errors and aid in troubleshooting - to determine if the script even ran.  This will get you closer.

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

    getKBname: function() {
		var answer = 'false';
		var catalogItemSysId = this.getParameter('sys_id')
        var KBGR = new GlideRecord('sc_2_kb');
        KBGR.addQuery('sc_cat_item', catalogItemSysId); // Reference field to Relation table
        KBGR.query();
        if (KBGR.next()) {
            answer = KBGR.kb_knowledge; // Return the Knowledge
        }
		return answer;
    },
  
    type: 'GetKnowledge'
});