How to override initialize function in OOTB script include

Justin Lucas
Kilo Sage

Hello all,

 

I am trying to override the "initialize" method in an OOTB script include but I'm not sure how to how to do this. For example, I'm trying to change the "EMPLOYEE_TAXONOMY" variable in the "EmployeeTaxonomyUtilSNC" script include below:

 

var EmployeeTaxonomyUtilSNC = Class.create();
EmployeeTaxonomyUtilSNC.prototype = {
    initialize: function() {
        this.EMPLOYEE_TAXONOMY = '1f5d5a40c3203010069aec4b7d40dd93';
    },

    /**
     * This function deactivates and activates and all the topics of 'Employee' which in turn updates the primary topic field ('taxonomy_topic') for all the content assoicated to this taxonomy. This step is required to make Search configurations work with OOTB Employee taxonomy.
     **/
    toggleTopicsActiveState: function() {
        if (!gs.hasRole('taxonomy_admin')) {
            gs.info(gs.getMessage("User {0} does not have write access to the given taxonomy and its topic", [gs.getUserID()]));
            return;
        }
        var topicGr = new GlideRecord('topic');
        topicGr.addQuery('taxonomy', this.EMPLOYEE_TAXONOMY);
        topicGr.addActiveQuery();
        topicGr.query();
        while (topicGr.next()) {
            topicGr.setValue('active', false);
            topicGr.update();
            topicGr.setValue('active', true);
            topicGr.update();
        }
    },

    type: 'EmployeeTaxonomyUtilSNC'
};

 

Since this is an OOTB script include, I need to override the method from the "EmployeeTaxonomyUtil" script include. How can I override the "EMPLOYEE_TAXONOMY" variable from the script below?

 

var EmployeeTaxonomyUtil = Class.create();
EmployeeTaxonomyUtil.prototype = Object.extendsObject(EmployeeTaxonomyUtilSNC, {
    initialize: function() {
        EmployeeTaxonomyUtilSNC.prototype.initialize.apply(this, arguments);
    },

    type: 'EmployeeTaxonomyUtil'
});

 

6 REPLIES 6

Hugo V
Tera Contributor

Hello,

 

In order to override the "EMPLOYEE_TAXONOMY" variable from the 'EmployeeTaxonomyUtilSNC' script include:

 

  1. Create a new script include based on the existing one ('EmployeeTaxonomyUtil')
  2. Edit the initialize function and add the sys_id of your taxonomy:
var MyEmployeeTaxonomyUtil = Class.create();
MyEmployeeTaxonomyUtil.prototype = Object.extendsObject(EmployeeTaxonomyUtilSNC, {
    initialize: function() {
        EmployeeTaxonomyUtilSNC.prototype.initialize.apply(this, arguments);
		this.EMPLOYEE_TAXONOMY = 'Insert your taxonomy sys_id here';
		
    },

    type: 'MyEmployeeTaxonomyUtil'
});

 

    3. Create a new fix script based on the existing one ('Set primary topics for Employee taxonomy') and call your new script include.

var employeeUtil = new sn_ect.MyEmployeeTaxonomyUtil();
employeeUtil.toggleTopicsActiveState();

 

 

Related articles:

 

https://noderegister.service-now.com/kb?id=kb_article_view&sysparm_article=KB1212940

 

https://www.servicenow.com/community/employee-center-forum/translate-topic-names-employee-center/m-p...