sabell2012
Mega Sage
Mega Sage

 

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.

 

DIFFICULTY LEVEL:    INTERMEDIATE
Assumes having taken the class SSNF and has good intermediate level of knowledge and/or familiarity with Scripting in ServiceNow.


I have been asked this question a few times when teaching Scripting:

 

With Script Includes what is the "type" for, and why is it automatically generated when I create one?

 

The answer is actually pretty straight-forward!

 

  1. Type is for allowing any developer using your Script Include to identify the type of object it is.
  2. ServiceNow did a nice thing and placed it into their non-client callable Script Include template for us. I used to have to do this myself. Now, it is done for me, and they tweaked the Script Include editor so that if I should change the name, then the type changes as well.
  3. Type is not necessary, there is one JavaScript built-in alternative.


Ok, so, demonstrating what it is I am talking about:


First let's create a simple Script Include that we can use to play with detecting its type.

 

  1. Create a new Script Include
  2. Name: type_test
  3. Accessible from: All Application Scopes
  4. Client Callable: false
  5. Script:

 

var type_test = Class.create();

type_test.prototype = {
	initialize: function() {},

	type: 'type_test'
};

 

You will note that when you initially created the Script Include that a template was automatically created for us, and that the name field is propagated throughout the script. If you change the name field; the script will follow suit.

 

6. Click on the Submit button to save your script.

 

sabell2012_1-1699136100767.png

 

 

Now fire up Scripts-Background and let's do some testing!

 

Testing

 

1. First we have to instantiate (fancy programmer term for "bring-into-existence") our Script Include object

 

var typeTest = new type_test();

 

2. Now lets try the typeof command and see what we get back

 

gs.info('---> typeof test: ' + (typeof typeTest)); // object

 

We get back "object". That makes sense. The typeof command is limited on what it can detect. When it comes to a user-based object; that is all that you will get back.

 

3. I don't like the following command as it is sometimes inaccurate in JavaScript, but we will try it here anyway.

 

gs.info('---> typeTest.constructor test: ' + typeTest.constructor.name); // Object

 

This returns "Object". This, again, reflects the overall type. All user created objects will show thusly. So, once again, my feeling toward this command has been proven correct. 😋

 

4. The following command I DO like, and it still does not return anything useful!

 

gs.info('---> toString.call test: ' + Object.prototype.toString.call(typeTest)); // object Object

 

This returned "object Object". Sigh.

 

5. Now let's tap into the type variable inside the object.

 

gs.info('---> typeTest.type test: ' + (typeTest.type == 'type_test')); // true

 

Usage:

 

if (typeTest.type == 'type_test') {
	// do something
}

 

That's better! However, it requires that extra bit of code; whether it is created by ServiceNow or ourselves. So is there a way of doing this, that exists, where we don't need that internal type variable? Yes!

 

6. Here is an alternative method for the type variable:

 

gs.info('---> instanceOf test: ' + (typeTest instanceof type_test)); // true

 

The JavaScript command "instanceof" does the job nicely, and it does not require that you program the value into your Script Include.   So really the type variable is not necessary.

 

Usage:

 

if (typeTest instanceof type_test) {
	// do something
}

 

 

My final script in Scripts - Background:

 

sabell2012_3-1699136401035.png

 

Results:

 

sabell2012_2-1699136357855.png

 

For further reading on instanceof: link

 

Here also, is a great side-by-side explanation of typeof vs. instanceof: link


I want to highly recommend taking the ServiceNow Scripting training class should you get the opportunity.

 

Enjoy!

Steven Bell.

 

If you find this article helps you, don't forget to log in and mark it as "Helpful"!

 

sabell2012_0-1699135629398.png


Originally published on: 07-16-2016 05:15 PM

I updated the code and brought the article into alignment with my new formatting standard.

1 Comment