logo

NJP

Service Catalog Currency Variable - Currency Format Enforcer (Foolproof)

Import · May 18, 2018 · article

This script is for users who want a currency-like variable in service catalog; the script enforces a variable in service catalog (i'm using single line text variable) to maintain a correct currency format. Copy paste the following script in an onSubmit - Catalog Client Script.

[NOTE: currencySign & fieldName are the ONLY 2 variables in the script you need to configure.]

FOOLPROOF

Detects & notifies invalid characters in the input field.

image

image

Detects & notifies too many numbers after decimal.

image

image

Detects & removes leading zeroes.

image

Detects & re-formats incorrectly formatted input.

image

On successful submission, input will always be in this format.

image

THE SCRIPT

//configurable variables var currencySign = '$'; var fieldName = ''; //DO NOT CHANGE FOLLOWING CODE: var rawValue = g_form.getValue(fieldName); var fieldLabel = g_form.getLabelOf(fieldName); var signValue, amount, element, formattedValue, value, decimalValue, decimal, outputString; //check for invalid characters if(!checkValidChars(rawValue)){ return false; } //check for valid decimal value (.00) if(!checkDecimalValue(decimalValue)){ return false; } //functions function getAmount(tempElement){ var tempAmount; for(i=0; i < tempElement.length; i++){ if(i==0){ tempAmount = tempElement[i]; } else{ tempAmount += tempElement[i]; } } return tempAmount; }

function checkValidChars(tempRawValue){ if(tempRawValue.length==0){ getNumAmount(); formatValidInput(); return true; } else if(containsSingleValue(tempRawValue, currencySign)){ return checkValidChars(tempRawValue.replace(currencySign, '')); } else if (containsSingleValue(tempRawValue, '.')){ return checkValidChars(tempRawValue.replace('.', '')); } else if (containsSingleValue(tempRawValue, ',')){ return checkValidChars(tempRawValue.replace(',', '')); } else if (containsSingleValue(tempRawValue, '0')){ return checkValidChars(tempRawValue.replace('0', '')); } else if (containsSingleValue(tempRawValue, '1')){ return checkValidChars(tempRawValue.replace('1', '')); } else if (containsSingleValue(tempRawValue, '2')){ return checkValidChars(tempRawValue.replace('2', '')); } else if (containsSingleValue(tempRawValue, '3')){ return checkValidChars(tempRawValue.replace('3', '')); } else if (containsSingleValue(tempRawValue, '4')){ return checkValidChars(tempRawValue.replace('4', '')); } else if (containsSingleValue(tempRawValue, '5')){ return checkValidChars(tempRawValue.replace('5', '')); } else if (containsSingleValue(tempRawValue, '6')){ return checkValidChars(tempRawValue.replace('6', '')); } else if (containsSingleValue(tempRawValue, '7')){ return checkValidChars(tempRawValue.replace('7', '')); } else if (containsSingleValue(tempRawValue, '8')){ return checkValidChars(tempRawValue.replace('8', '')); } else if (containsSingleValue(tempRawValue, '9')){ return checkValidChars(tempRawValue.replace('9', '')); } else{ swal('Invalid Currency Input', 'Field (' + fieldLabel + ') should only include '+ currencySign +', numbers, and decimal. Please remove the following invalid character(s): ' + tempRawValue, 'error'); return false; } } function checkDecimalValue(tempDecimalValue){ var tempDecimalValueList = tempDecimalValue.split('.'); if(tempDecimalValueList.length > 1 || tempDecimalValueList[0].length > 2){ swal('Invalid Decimal Input', 'Field (' + fieldLabel + ') should only include 2 numbers after the decimal (e.g. $9.99).'); return false; } return true; } function removeLeadingZeros(tempAmount){ while(tempAmount.charAt(0) == 0){ if(tempAmount.length==1){ return tempAmount; } tempAmount = tempAmount.substring(1); } return tempAmount; } function getNumAmount(){ //break down to numerical amount //check for decimal if(containsSingleValue(rawValue, '.')){ decimal = rawValue.split('.'); if(decimal.length > 2){ decimalValue = decimal[1] + '.' + decimal[2]; } else if(decimal[1].length == 0){ decimalValue = '00'; } else if (decimal[1].length == 1){ decimalValue = decimal[1] + '0'; } else{ decimalValue = decimal[1]; } value = decimal[0]; } else{ value = rawValue; decimalValue = '00';

}

//check for sign if(containsSingleValue(value, currencySign)){

signValue = value.split(currencySign);

if(containsSingleValue(signValue[1], ',')){ element = signValue[1].split(','); amount = getAmount(element); } else{ amount = signValue[1]; } } else{ if(containsSingleValue(value, ',')){ element = value.split(','); amount = getAmount(element); } else{ amount = value; } } } function formatValidInput(){ //reconstruct with proper formatting var length = amount.length - 1; var count = 0; amount = removeLeadingZeros(amount); for(i=length; i >= 0; i--){ if(i==length){ formattedValue = amount.charAt(i); } else if(count%3 == 0 && i!= length){ formattedValue = amount.charAt(i) + ',' + formattedValue; } else{ formattedValue = amount.charAt(i) + formattedValue; } count++; }

outputString = currencySign + formattedValue + '.' + decimalValue;

//diaplsy output g_form.setValue(fieldName, outputString); } //contains function that takes in a string & a single value like '.' ',' '1' '2' '$' etc. function containsSingleValue(string, singleValue){ for(i=0;i<string.length;i++){ if(string[i]==singleValue){ return true; } } return false;

}

image

View original source

https://www.servicenow.com/community/developer-articles/service-catalog-currency-variable-currency-format-enforcer/ta-p/2322821