Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to create calculated field script

kmathis
Kilo Contributor

I have created the following in a calculated field to add the numerical contents of three fields:

var a = parseInt(g_form.getValue('current.u_currency'));

var b = parseInt(g_form.getValue('current.u_currency_3'));

var c = parseInt(g_form.getValue('current.u_currency_5'));

var d = a+b+c;

g_form.setValue('current.u_total_project_capex',d);

It returns 0.   Any ideas?

Thank you!

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

Kari,



  Here is your calculated value


if(!isNaN(current.u_currency) && !isNaN(current.u_currency_3) && !isNaN(current.u_currency_5)){


current.u_total_project_capex=(parseInt(current.u_currency)+parseInt(current.u_currency_3)+parseInt(current.u_currency_5)).toString();


}




Thanks,


Abhinay


View solution in original post

5 REPLIES 5

Never underestimate the power of a good example on the system. In this case, sys_user.name is a good example of how crazy you can get with a calculated field. One would think it's simply first+last. Nope!



if (current.first_name.nil() && current.last_name.nil() && !current.name.nil()) {


  var names = current.name.toString().split(" ");


  if (names.length > 1) {


      current.first_name = names[0];


      names.shift();


      current.last_name = names.join(" ");


  } else


      current.last_name = names[0];


}  



if(current.first_name.nil()) {


      current.last_name;


  } else {


      current.first_name + ' ' + current.last_name;


  }