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

Murthy Ch
Giga Sage

Hello there,

In this article, I'm going to share few interesting features in script include which are very helpful in day to day scenarios:

  1. Use of initialize function
  2. Use of this keyword and how it works
  3. Calling script include in reports

 

var CommunityArticle = Class.create();
CommunityArticle.prototype = {
    initialize: function(days) {
        this.dateTime = new GlideDateTime(); //declare today's date
        this.days = days;
    },
    getCurrentDate: function() { //get today's date
        return this.dateTime;
    },

    addDaysToCurrentDate: function(noOfDays) { //add days to today's date
        var currentDate = this.getCurrentDate(); //get today's date
        currentDate.addDaysLocalTime(noOfDays);
        return currentDate;
    },
	
    getDays: function() {
        return this.days;
    },

    type: 'CommunityArticle'
};

 

Let's talk about the initialize function and it's uses:

In ServiceNow, the initialize function within a Script Include is used to set up the initial state or perform any necessary setup when an instance of the Script Include is created. This function acts as a constructor and is automatically called when the Script Include object is instantiated.

Let' dive into the example:

In the above piece of code, In the initialize function I've declared the today's date to the this.dateTime

So, if you observe the getCurrentDate method I'm returning the this.dateTime which means it checks for that value in initialize function. So, the use of initialize method is whenever we want to get today's date (or) if you want to declare any initial values we can make use of this and we don't need to declare again and again.

TEST RUN:

getCurrentDate.PNG

Now, Let's explore the this keyword and it's uses:
As, we have seen in the above scenario the this keyword is also uses to call another function with in the same script include. Let's go through the example:

In the addDaysToCurrentDate method we are adding the day's to the current date by utilizing the getCurrentDate method. So, if you see this keyword is used to call that particular function.

addDaysToCurrentDate.PNG

Now, Let's take a look at the getDays method. In this, I'm getting the value from the initialize method but intrestingly it is a dynamic value which we will be passing from the script include where it is being called.

Inorder to pass the value into the initialize method we need to call the script include like below:

getDays.PNG

Now, Let's explore calling the script include in reports:

If you are calling the script include in conditions section in report's make sure to check the client callable (or) Glide AJAX enabled checkbox. You can refer this for more information on this topic.

 

I hope this information is helped you in someway. If you understand (or) if you think that you learned something new today. Don't forgot to hit the Like button.

See you soon with interesting topics again:)

Thanks

Comments
kumarm3i
Tera Explorer

Good Article! Very Helpful

Supriya_K
Tera Explorer

Very good article, it’s really helpful 

SAI VENKATESH
Tera Sage
Tera Sage

Good Article. 

Thanks for providing Nice Information

Tejesh4
Giga Contributor

Much informative, keep positing such good information in future too.

Version history
Last update:
‎06-30-2024 09:30 PM
Updated by:
Contributors