I want to create a "numeric" and "currency" type variable in catalog item

Vaishnavi35
Tera Guru

Hi,

 

I have a requirement to create two variables which are off Currency and Numeric type variables.

 

Thanks,

Vaishnavi

6 REPLIES 6

Mohith Devatte
Tera Sage
Tera Sage

Hello @Vaishnavi35 As these type of variables are not present in catalog items you might have to find a custom way to do it.

 

For numeric field you can create a string type variable and  write an on change client script on that field like below

 

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


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


          return;


    }




    //Type appropriate comment here, and begin script below


  var numVal=g_form.getValue('your_string_field_name');



  if (isNaN(numVal))


    {


  alert("Input should only be in numbers");


  g_form.setValue('your_string_field_name','');


  return false;


    }


}

 

For currency field you can try this by creating a string field and then validating it like  in below solution

https://www.servicenow.com/community/developer-articles/custom-currency-field-validation-in-catalog-...

 

Hope this helps 

Mark my answer correct if this helps you 

Thanks

 

Numeric value validation script does not work.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Type appropriate comment here, and begin script below
   var numVal = g_form.getValue('u_dollar_amount');

    if (isNaN(numVal)) {
        alert("Input should only be in numbers");
        g_form.setValue('u_dollar_amount', '');
        return false;
	}
   
}

Riya Verma
Kilo Sage
Kilo Sage

Hi @Vaishnavi35 ,

 

Hope you are doing great.

 

We dont have OOB variable present in servicenow of type currency or Numeric type variable. But you can acheive it by building logic in on change script which will only allow for currency type value for particular field, or other value it can show you alert popup and can clear value out if entered value of other type. 

 

1. Numeric type : PFB reference script for same :

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var numVal = g_form.getValue('your_string_field_name');

    if (isNaN(numVal)) {
        alert("Input should only be in numbers");
        g_form.setValue('your_string_field_name', '');
        return false;
    }
}

 

 

Similarly you can put your logic in on chaneg script for currency type variable.

 

 
 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Hi Riya,

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Type appropriate comment here, and begin script below
   var numVal = g_form.getValue('u_dollar_amount');

    if (isNaN(numVal)) {
        alert("Input should only be in numbers");
        g_form.setValue('u_dollar_amount', '');
        return false;
	}
   
}

 

Thanks & Regards,

Vaishnavi

 

The script does not work somehow. If you can please help?