How to override initialize function in OOTB script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2023 07:40 AM
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'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2023 07:46 AM
To override the initialize function in an OOTB script include, you can follow these steps:
- Create a new script include that extends the OOTB script include.
- In the new script include, define a function with the same name as the initialize function in the OOTB script include.
- In the new function, add your custom code.
- When you are finished, save the new script include.
When you use the new script include, your custom code will be executed instead of the initialize function in the OOTB script include.
When you use the MyScriptInclude script include, the initialize function in the OOTBScriptInclude will be overridden and your custom code will be executed instead.
It is important to note that you cannot directly edit OOTB script includes. You can only override them by creating a new script include that extends the OOTB script include. This is because OOTB script includes are protected and cannot be modified directly.
Please inform me if my feedback was useful and if it resolved your issue. If it did, please click the "Helpful" button below.
Thank you for your valuable feedback! It helps me enhance my responses and serve you better in the future.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2023 12:36 PM
So after creating a copy of the script include and in the initialize function, what custom code should I add when I want to use a sys_id of taxonomy that I created to override the sys_id of employee taxonomy?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2023 08:00 AM - edited 06-12-2023 08:01 AM
@Justin Lucas you can update initialize function of EmployeeTaxonomyUtil like below
initialize: function() {
this.eTSNC = new EmployeeTaxonomyUtilSNC();
this.EMPLOYEE_TAXONOMY = this.eTSNC.EMPLOYEE_TAXONOMY;
},
please mark as correct answer if this solves your issue.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2023 12:34 PM
How does that work if I want to add 1 specific sys_id of taxonomy I created to override the employee taxonomy?