How to use UI script in service portal.

Aishwarya Puri
Giga Expert

UI script is not working getting error "Uncaught ReferenceError: Class is not defined" on service Portal, and this error is caused by using the class.create in UI script.

 

We have include the UI script in Theme.

 

Any help is much appreciated.

 

Thanks,

Aishwarya

4 REPLIES 4

Carol11
Kilo Expert

You have to add the UI Script as a dependancy on your widget i believe.

Ajaykumar1
Tera Guru

Hi Aishwarya,

1. Create UI script.

2. goto -> Service Portal -> Dependency-> New

        1. check "include page on load"

        2. select the page to load

3. add the JS Includes -> new and your UI script here.

4. Go to the widget which is included in your page.

      1. go to the related list " Dependencies"

      2. click "Edit"

      3. Add the JS includes added in step 3.

 

Mark If Correct/Helpful

Regards,
Ajay

Slava Savitsky
Giga Sage

I guess your UI Script looks like this:

var myClass = Class.create();

myClass.prototype = {
    
    myMethod: function() {
        // do something
    }

    myOtherMethod: function() {
        // do something else
    }

};

You can avoid Class.create() by using a different object design pattern:

var myClass = function() {

    this.myMethod = function() {
        // do something
    };
   
};

myClass.prototype = {

    myOtherMethod: function() {
        // do something else
    }

}

Note that you can define methods either using this keyword or via the prototype.

The SN Nerd
Giga Sage
Giga Sage

The Class "Class" is actually a Server Side Script Include, alas you cannot use in a UI Script.

You can see how it works by looking at the Script Include "PrototypeServer".

You'll need to use one of the two patterns mentioned by Slava.

 


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022