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

Are you doing this in a scoped application or in the global scope?



Did you try to call the "execute" function something else? It could be that "execute" is already defined in the prototype object that you are extending and thereby you are overwriting this function.



In general: I would recommend that you start simplifying your code to better debug it. Copy paste your code into notepad and then remove all funcionality in your script include. Make the execute function so that it logs something to your systemlog eg. gs.log("The script include is called"); Check to log to verify that you can find the log statement. If this works, start building from there and continue to test as you move along.



I hope this helps!


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.


pavani19
Kilo Expert

Hi,

i am implimenting a service catalog request.

i want to populate data from sql db in a dropdown list  field of catalog item.

 

i am new to SN development..

please help..