Classless Script include

Prateek Tyagi
Tera Contributor

hi i have a requirement that i have to write two classless script include and call the first one in second script include.i have written 

script include 1 for addition

var a;
var b ;
function Add(a,b){
var c = a+b;
gs.addErrorMessage(c);
return a,b;
}

script include 2 for multiplication

function Mul(){
var objdemo=new Add(a,b);

//gs.addErrorMessage(c);

var e = a*b;
gs.addInfoMessage(e);
}

BR on insert and update of incident

var s = 10;
var d = 20;
//var t = 30;
var Script = Mul(s,d);

but it is giving me the message both error and info as  NaN.

please if anyone can help

 

2 REPLIES 2

Allen Andreas
Administrator
Administrator

Hi,

Should you be passing in the parameters to your function in the script include?

Instead of:

function Mul(){

use?

function Mul(a,b){

Please use logging within your script includes to check values so you aren't coding in the dark.

More info here: https://developer.servicenow.com/dev.do#!/learn/learning-plans/sandiego/new_to_servicenow/app_store_...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

John Dahl
Tera Guru

There are several issues with your sample. If you are making the script includes classless, then you cannot treat them like a class:

  1. Do not capitalize functions, which is what a classless script include is. This is optional, but a best practice. Don't forget to update the signature as well as all of the places where the function is called.
  2. Do not instantiate function calls.

var objdemo=new Add(a,b);

should be:

var objdemo=add(a,b);

 

  • Your add function isn't returning the sum, but the original values.
  • The mul function doesn't explicitly return anything, which means it will implicitly return the undefined object.
  • The main body is not passing any values into the mul function, so they will be defaulted to undefined inside the function... which means you are passing them into the add function and trying to add two undefined values. Adding two undefined values will result in NaN.