How to override a base class function in script include

ramandeepgarg6
Tera Expert

Dear Friends,

We have 2 script includes GRCUtils and GRCUtilsBase. GRCUtilsBase is a Servicenow base class script include and hence is not editable. It has all the functions and code. I want to update a function. So I was overriding that function in GRCUtils which extends the base class. Now when I override then the code does not work. I copied the same code in GRCUtils just for testing purposes but it didn't work. Could you pleas help? Please tell if I am overriding it correctly or not

var GRCUtils = Class.create();

GRCUtils.prototype = Object.extendsObject(GRCUtilsBase, {

                  //Override base class function

generateProfilesFromType: function(type, updateClassIfExists) { --> updateClassIfExists is function in base class

                  return this._generateProfilesFromType(type, updateClassIfExists); },

_generateProfilesFromType: function(typeQuery) {

                  gs.log('++start');

                  var results = {

                                    added: 0,

----

---

--

return results; },

      type: 'GRCUtils'

});

Regards

Ramandeep

1 ACCEPTED SOLUTION

Sorry Ramandeep,



forgot to copy paste the output indeed



Output:



*** Script: Employeee log


*** Script: Consultant log


*** Script: Base function called!



Stijn


View solution in original post

3 REPLIES 3

Stijn Verhulst3
Kilo Guru

Hi Ramandeep,



I have a concrete example for you:



var Employee = Class.create();


Employee.prototype = {


      initialize: function() {


      },



baseClassFunction: function() {



gs.log('Base function called!');



},



printLog: function() {



gs.log('Employeee log');



},




      type: 'Employee'


};


};



var Consultant = Class.create();


Consultant.prototype = Object.extendsObject(Employee, {



printLog: function() {



gs.log('Consultant log');


this.baseClassFunction();



},



      type: 'Consultant'


});


});



With the following statements run via Scripts - Background I retrieve the following result as expected due to overriding:



new Employee().printLog();


new Consultant().printLog();



Hope this helps you.



Kind regards,



Stijn


Hi Stijn,



Thank you for looking into it! Could you please share the output after running the script statements? I think you missed output in your last reply.



Regards


Ramandeep


Sorry Ramandeep,



forgot to copy paste the output indeed



Output:



*** Script: Employeee log


*** Script: Consultant log


*** Script: Base function called!



Stijn