- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
06-30-2024 09:29 PM - edited 06-30-2024 09:30 PM
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:
- Use of initialize function
- Use of this keyword and how it works
- 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:
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.
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:
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
- 2,892 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Good Article! Very Helpful
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Very good article, it’s really helpful
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Good Article.
Thanks for providing Nice Information
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Much informative, keep positing such good information in future too.