Using Client Script and Script Include to populate a field

Jamie Imms
Kilo Expert

Hello,

With some googling, I attempted to create some functionality to populate a field on the Business Service form upon load. This is my first foray into scripting on the SNOW platform, so I'm having a little trouble.

The process is this:

  1. Client loads a record from the Business Service table
  2. Client Script calls a Script Include upon load
  3. The Script Include grabs a value from an API call
  4. The value retrieved is populated into a field on the Business Service form.

Here is the Client Script:

function onLoad() {

    //Type appropriate comment here, and begin script below

  var ga = new GlideAjax('GetSplynxFUPMonthUsedInGB');  

      ga.addParam('sysparm_name','execute');  

      ga.addParam('splynx_service_id', g_form.getValue('u_splynx_service_id'));  

      ga.getXML(MonthUsedAnswer);

function MonthUsedAnswer(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");

g_form.setValue(u_Used_Quota, answer);

}

}

Here is the Script Include:

var GetSplynxFUPMonthUsedInGB = Class.Create();

GetSplynxFUPMonthUsedInGB.prototype = Object.extendsObject(AbstractAjaxProcessor, {  

execute: function() {            

                      var APIresponseBody = this.GetUsedBytesFromSplynx( this.getParameter('splynx_service_id') );

var UsedBytes = this.ParseSplynxResponseFromJson(APIresponseBody);

var UsedGB = this.ConvertBytesToGigabytes(UsedBytes);

return this.ConvertUsedGBtoString(UsedGB);  

            },  

   

//Call the API which should give a response in JSON that includes the monthly usage

GetUsedBytesFromSplynx : function(splynx_service_id) {

var r = new sn_ws.RESTMessageV2('Splynx Check FUP Usage', 'GET');

r.setStringParameterNoEscape('splynx_service_id', ('splynx_service_id'));

//override authentication profile

//authentication type ='basic'/ 'oauth2'

//r.setAuthentication(authentication type, profile name);

var response = r.execute();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

return responseBody;

},

//Take the response from the API call, and parse the month_down value from it, which will be monthly usage in bytes

ParseSplynxResponseFromJson : function(APIresponseBody) {

var parser = new JSONParser();  

var parsed = parser.parse(APIresponseBody);

var UsedBytes = parsed.month_down;

return UsedBytes;

},

//Take the parsed month_down value in bytes, and convert it to Gigabytes

ConvertBytesToGigabytes :function(BytesConvertFrom)   {

var UsedGB = (BytesConvertFrom/1073741824);

return UsedGB;

},

//Take the converted GB integer and put it into a string for populating the field

ConvertUsedGBtoString :function(UsedGB)   {

var UsedQuota = (Used + ' GB');

return UsedQuota;

},

    type : 'GetSplynxFUPMonthUsedInGB'

});

So, straight away I get this error:

org.mozilla.javascript.EcmaError: Cannot set property "prototype" of undefined to "

function () {

      this.initialize.apply(this, arguments);

}

"

    Caused by error in sys_script_include.f17a70814fe4834012aeccce0310c732.script at line 3

          1: var GetSplynxFUPMonthUsedInGB = Class.Create();

          2:

==>     3: GetSplynxFUPMonthUsedInGB.prototype = Object.extendsObject(AbstractAjaxProcessor, {  

          4:

          5: execute: function() {            

          6:                         var APIresponseBody = this.GetUsedBytesFromSplynx( this.getParameter('splynx_service_id') );

I'm not sure what this means, I've followed the syntax for declaring the prototype from other Script Includes I could find.

1 ACCEPTED SOLUTION

sbrnag
Mega Expert

Hi Jamie,



The first line in your script include is wrong



var GetSplynxFUPMonthUsedInGB = Class.Create();



should be



var GetSplynxFUPMonthUsedInGB = Class.create();



Class.create();   in that statement create function typo.



so the class "GetSplynxFUPMonthUsedInGB " is not created so the error tells undefined.protorype is invalid.



Thanks,


NAg.


View solution in original post

7 REPLIES 7

Shishir Srivast
Mega Sage

I hope you have script include as client callable?



Also, in your client script please have u_Used_Quota in quotes.


g_form.setValue('u_Used_Quota', answer);


Hi Shishir, yes the Script Include is Client Callable


Can you please try with below by adding global, shouldn't matter but let's give a try.


GetSplynxFUPMonthUsedInGB.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {



Also, please have variable name starting with sysparm: like sysparm_splynx_service_id: http://wiki.servicenow.com/index.php?title=GlideAjax#Using_GlideAjax


http://wiki.servicenow.com/index.php?title=GlideAjax#Using_GlideAjax


My Script Include is now this, but I am still getting the same EcmaError:



var GetSplynxFUPMonthUsedInGB = Class.Create();




GetSplynxFUPMonthUsedInGB.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {  




execute: function() {            


                      var APIresponseBody = this.GetUsedBytesFromSplynx( this.getParameter('sysparm_splynx_service_id') );


var UsedBytes = this.ParseSplynxResponseFromJson(APIresponseBody);


var UsedGB = this.ConvertBytesToGigabytes(UsedBytes);


return this.ConvertUsedGBtoString(UsedGB);  


            },  


   


//Call the API which should give a response in JSON that includes the monthly usage


GetUsedBytesFromSplynx : function(splynx_service_id) {


var r = new sn_ws.RESTMessageV2('Splynx Check FUP Usage', 'GET');


r.setStringParameterNoEscape('splynx_service_id', 'splynx_service_id');




//override authentication profile


//authentication type ='basic'/ 'oauth2'


//r.setAuthentication(authentication type, profile name);




var response = r.execute();


var responseBody = response.getBody();


var httpStatus = response.getStatusCode();





return responseBody;


},




//Take the response from the API call, and parse the month_down value from it, which will be monthly usage in bytes


ParseSplynxResponseFromJson : function(APIresponseBody) {


var parser = new JSONParser();  


var parsed = parser.parse(APIresponseBody);


var UsedBytes = parsed.month_down;



return UsedBytes;


},




//Take the parsed month_down value in bytes, and convert it to Gigabytes


ConvertBytesToGigabytes :function(BytesConvertFrom)   {


var UsedGB = (BytesConvertFrom/1073741824);



return UsedGB;


},




//Take the converted GB integer and put it into a string for populating the field


ConvertUsedGBtoString :function(UsedGB)   {


var UsedQuota = (Used + ' GB');



return UsedQuota;


},




    type : 'GetSplynxFUPMonthUsedInGB'


});