In a Script Include what does 'initialize: function()' do?

Chris McDevitt
ServiceNow Employee
ServiceNow Employee

OK experts,

I was looking at Script Includes(SI) and I noticed that some SI have:

  • an 'initialize: function()' and some do not.
  • some of SI have functions inside of  'initialize: function()' and some have functions outside of  'initialize: function()'.

What does  'initialize: function()' actually do (because now I'm not sure), when do you use it (and when to not use it) and how to use it correctly? 

Thanks in advance

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

Mostly it's used for getting values that you might use in multiple function.

Like I use it to call system properties that has username or password stored so I don't have to call that properties in each function.

initialize: function() {
// Get authorization details
var authProfileID = gs.getProperty('auth_profile');
var authProfileGr = new GlideRecord('sys_auth_profile_basic');
authProfileGr.get(authProfileID);
this.soap_un = authProfileGr.getValue('username');
this.soap_pw = new GlideEncrypter().decrypt(authProfileGr.getValue('password'));

 

createContact: function (userGr) {
var s = new sn_ws.SOAPMessageV2('XXXXX', 'create');

// Set parameters for authentication
s.setStringParameterNoEscape("soap_un", this.soap_un);
s.setStringParameterNoEscape("soap_pw", this.soap_pw);

View solution in original post

2 REPLIES 2

AbhishekGardade
Giga Sage

Hello Chris,

yes, initialize function is something like which holds the code that is made available to all the other functions defined over that script include 

This links may help you to understand:

Scripting 101: Script Includes - Class vs Classless

https://community.servicenow.com/community?id=community_question&sys_id=b31001c6db645b884e1df4621f96...

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

Mike Patel
Tera Sage

Mostly it's used for getting values that you might use in multiple function.

Like I use it to call system properties that has username or password stored so I don't have to call that properties in each function.

initialize: function() {
// Get authorization details
var authProfileID = gs.getProperty('auth_profile');
var authProfileGr = new GlideRecord('sys_auth_profile_basic');
authProfileGr.get(authProfileID);
this.soap_un = authProfileGr.getValue('username');
this.soap_pw = new GlideEncrypter().decrypt(authProfileGr.getValue('password'));

 

createContact: function (userGr) {
var s = new sn_ws.SOAPMessageV2('XXXXX', 'create');

// Set parameters for authentication
s.setStringParameterNoEscape("soap_un", this.soap_un);
s.setStringParameterNoEscape("soap_pw", this.soap_pw);