How to define variables of type Price in the Service Catalog

johannes5
Giga Expert

Hi ServiceNow Community Developers,

I have a requirement to define a field called 'Cost' on the Service Catalog form. However when I looked at the list of available variable types on the Service catalog I don't see type Price which is the type that I think this field should be defined as. The default value for this field should be $0.00. Would you please advise as to what I need to do in order to get the desired functionality in this case.

Thanks,

Johannes

1 ACCEPTED SOLUTION

Hi ServiceNow Community Developers,


I managed to figure this out using a script, here is a script that I wrote which works well for everything i wanted to validate for in the variable:



function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }


  //Type appropriate comment here, and begin script below


  var cost = g_form.getValue('cost');


  cost = cost.trim();



  // first character should be dollar sign



  var firstChar = cost.substring(0,1);


  if (firstChar != '$') {


  alert ("Please enter cost in $0.00 format");


  g_form.setValue("cost", oldValue);


  return;


  }



  // characters after the $ sign should be numerics



  var costType = isNaN(cost.substring(1));


  if (costType == true) {


  alert ("Please enter cost in $0.00 format");


  g_form.setValue("cost", oldValue);


  return;


  }



  // entered value should have a decimal point



  var num = cost.substring(1);


  if (num.indexOf('.') == -1) {


  alert ("Please enter cost in $0.00 format");


  g_form.setValue("cost", oldValue);


  return;


  }



  // there must be 2 digits only after the decimal



  var decNum = num.substring(num.indexOf('.')+1, num.length);


  if (decNum.length != 2) {


  alert ("Please enter cost in $0.00 format");


  g_form.setValue("cost", oldValue);


  return;


  }


}



If there is a better to write this I will greatly appreciate it but the above script worked for me.


Thanks,


Johannes


View solution in original post

7 REPLIES 7

edwin_munoz
Mega Guru

Hi Johannes,



The field type is called currency:



Introduction to Fields - ServiceNow Wiki


Hi Edwin,



When you are defining a new variable for a catalog item neither currency type nor price type appears in the list of available types in my instance. The question I have then is how do I see this field type so I can select it as the type for my field.



Thanks for your response.



Johannes


Sorry Johannes, you are right,



I think there is not an option to use currency fields on service catalog item. Maybe someone will come up with a solution.


Hi ServiceNow Community Developers,


I managed to figure this out using a script, here is a script that I wrote which works well for everything i wanted to validate for in the variable:



function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }


  //Type appropriate comment here, and begin script below


  var cost = g_form.getValue('cost');


  cost = cost.trim();



  // first character should be dollar sign



  var firstChar = cost.substring(0,1);


  if (firstChar != '$') {


  alert ("Please enter cost in $0.00 format");


  g_form.setValue("cost", oldValue);


  return;


  }



  // characters after the $ sign should be numerics



  var costType = isNaN(cost.substring(1));


  if (costType == true) {


  alert ("Please enter cost in $0.00 format");


  g_form.setValue("cost", oldValue);


  return;


  }



  // entered value should have a decimal point



  var num = cost.substring(1);


  if (num.indexOf('.') == -1) {


  alert ("Please enter cost in $0.00 format");


  g_form.setValue("cost", oldValue);


  return;


  }



  // there must be 2 digits only after the decimal



  var decNum = num.substring(num.indexOf('.')+1, num.length);


  if (decNum.length != 2) {


  alert ("Please enter cost in $0.00 format");


  g_form.setValue("cost", oldValue);


  return;


  }


}



If there is a better to write this I will greatly appreciate it but the above script worked for me.


Thanks,


Johannes