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.

needing to use toString in current.variables to convert value from a maintain item form

Robb4
Tera Contributor

I have a Catalog form that takes in a string in the form of a dollar amount $5.00

I'm putting that into a table that has a cost field set as currency.

 

Doing a background script it works:

removed all the variables not related to this issue- the rest of them are populating

var operational_spend_per_month = "$5000.00",
 

var operationalSpend = operational_spend_per_month.substring(1,15);

var tableGr = new GlideRecord('x_table_name');
tableGr.addQuery('name', account_name);
tableGr.query();
// If the account name doesn't exist insert the values from the form:

if(!tableGr.hasNext()){
    tableGr.initialize();
    tableGr.setValue('name', account_name);
    tabletGr.setValue('cost', operationalSpend);
  
   tableGr.insert();

}

But when I use this in the worklfow using current.variables

var awsAccountGr = new GlideRecord('x_wec_sn_rel_mgt_aws_account_management');

var operationalSpend = current.variables.operational_spend_per_month.substring(1,15);

var tableGr = new GlideRecord('x_table_name');
tableGr.addQuery('name', account_name);
tableGr.query();
// If the account name doesn't exist insert the values from the form:

if(!tableGr.hasNext()) {
    tableGr.initialize();
    tableGr.setValue('name', current.variables.account_name.account_name);
    tabletGr.setValue('cost', operationalSpend);
  
   tableGr.insert();

}

I've logged current.variables.account_name.account_name  and operationalSpend, the results are  like $55.00 and undefined, respectively.  When I log in the background script I get $55.00 and 55.00, respectively. 

I saw another post needing to use toString().   which I tried:

var operationalSpend = current.variables.operational_spend_per_month.toString().substring(1,15);

which resulted in NaN,

should I try parseFloat() ? seeing as it technically is a float?

var operationalSpend = parseFloat(current.variables.operational_spend_per_month.substring(1,15));

 

Thanks,

Robb

2 REPLIES 2

Yousaf
Giga Sage

Hi Robb,

 

Check this link please.

https://community.servicenow.com/community?id=community_question&sys_id=f88a4be9db5cdbc01dcaf3231f96...

 

Mark Correct or Helpful if it helps.


***Mark Correct or Helpful if it helps.***

Robb4
Tera Contributor

Here is what I came up with  that fixed the issue:

 

var tempValue  = current.variables.operational_spend_per_month;
var costString = tempValue.toString().replace(/\$/g,'');
var operationalSpend = parseInt(costString);

 

it was based off this solution to a similar problem:

Convert Single Line Text variable to Integer