The CreatorCon Call for Content is officially open! Get started here.

How to use function from base script include into extended script include

Obito
Tera Expert

Hi All,

 

What is the best way to use the function from base script include into extended script include. Can I call that function using this keyword or not.

 

Thanks

1 ACCEPTED SOLUTION

Moin Kazi
Kilo Sage
Kilo Sage

Hi @Obito ,

 

 

The best way to use a function from a base Script Include in an extended Script Include is to ensure that the base Script Include is set to be accessible (i.e., it's not private). You can call the function using the this keyword within the extended Script Include, as it refers to the current instance of the Script Include.

Here's how to do it:

  1. Ensure Base Script Include is Public: Make sure the base Script Include has the appropriate access settings (public or protected).

  2. Inherit the Base Script Include: In your extended Script Include, inherit from the base Script Include by specifying the name of the base Script Include in the "Extends" field.

  3. Call the Function: Use this.functionName() to call the function from the base Script Include within the extended Script Include.

Example

Base Script Include (BaseScriptInclude):

 

var BaseScriptInclude = Class.create();
BaseScriptInclude.prototype = {
    initialize: function() {
    },
    
    myFunction: function() {
        return "Hello from Base Script Include!";
    },
    
    type: 'BaseScriptInclude'
};

 

Extended Script Include (ExtendedScriptInclude):

 

var ExtendedScriptInclude = Class.create();
ExtendedScriptInclude.prototype = Object.extendsObject(BaseScriptInclude, {
    initialize: function() {
    },
    
    callBaseFunction: function() {
        return this.myFunction(); // Calling the function from the base script include
    },
    
    type: 'ExtendedScriptInclude'
});​

 

This approach allows you to effectively reuse and extend functionality from base Script Includes in your ServiceNow environment.

 

Hope this help you.

 

Regards

Moin

View solution in original post

1 REPLY 1

Moin Kazi
Kilo Sage
Kilo Sage

Hi @Obito ,

 

 

The best way to use a function from a base Script Include in an extended Script Include is to ensure that the base Script Include is set to be accessible (i.e., it's not private). You can call the function using the this keyword within the extended Script Include, as it refers to the current instance of the Script Include.

Here's how to do it:

  1. Ensure Base Script Include is Public: Make sure the base Script Include has the appropriate access settings (public or protected).

  2. Inherit the Base Script Include: In your extended Script Include, inherit from the base Script Include by specifying the name of the base Script Include in the "Extends" field.

  3. Call the Function: Use this.functionName() to call the function from the base Script Include within the extended Script Include.

Example

Base Script Include (BaseScriptInclude):

 

var BaseScriptInclude = Class.create();
BaseScriptInclude.prototype = {
    initialize: function() {
    },
    
    myFunction: function() {
        return "Hello from Base Script Include!";
    },
    
    type: 'BaseScriptInclude'
};

 

Extended Script Include (ExtendedScriptInclude):

 

var ExtendedScriptInclude = Class.create();
ExtendedScriptInclude.prototype = Object.extendsObject(BaseScriptInclude, {
    initialize: function() {
    },
    
    callBaseFunction: function() {
        return this.myFunction(); // Calling the function from the base script include
    },
    
    type: 'ExtendedScriptInclude'
});​

 

This approach allows you to effectively reuse and extend functionality from base Script Includes in your ServiceNow environment.

 

Hope this help you.

 

Regards

Moin