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.

Help with OnLoad Client Script with Currency Field

jlaue
Mega Sage

Hello - 

I am working on an OnLoad client script that checks a currency field, and if it is the default of '0.00' I want to set the background color of the field on the form to red.  I have checked around community and found some examples and based on that, I have this script, however it is not working.  I was wondering if anyone could provide some guidance on this.  I realize that I can use a Field Style, but I only want the background to be red if there hasn't been a value entered in there, so anything other than 0.00 in the currency field.

function onLoad() {
    
    var element = g_form.getElement('base_rent_per_month');
    var value = g_form.getValue('base_rent_per_month');
    if (value == 'USD;0.00') {
        element.style.backgroundColor = "red";
    }
    
}

I have also tried different value combinations like:  '0'   '0.00'   ''   'USD;0'  

Thanks!

 

 

1 ACCEPTED SOLUTION

Appreciated that you have pointed towards one of the possible cons of using hardcoded value which might change for different languages. 

I hope so that g_form.getDecimalValue() pointed by you in your answer, will also work for values like you have specified "USD;0,00", or for any other possible value which does not contain dot(.) to distinguish decimal part of a number.

@jlaue  consider using this better version of above script.

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

	var display = g_form.getControl('<your_table_name>.<your_field_name>.display');
	var currency= g_form.getControl('<your_table_name>.<your_field_name>.currency');

	if(g_form.getDecimalValue('<your_field_name>') == 0){
		display.style.background = 'red';
		currency.style.background = 'red';
	}
	else{
		display.style.background = 'white';
		currency.style.background = 'white';
	}
   
}

View solution in original post

7 REPLIES 7

Appreciated that you have pointed towards one of the possible cons of using hardcoded value which might change for different languages. 

I hope so that g_form.getDecimalValue() pointed by you in your answer, will also work for values like you have specified "USD;0,00", or for any other possible value which does not contain dot(.) to distinguish decimal part of a number.

@jlaue  consider using this better version of above script.

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

	var display = g_form.getControl('<your_table_name>.<your_field_name>.display');
	var currency= g_form.getControl('<your_table_name>.<your_field_name>.currency');

	if(g_form.getDecimalValue('<your_field_name>') == 0){
		display.style.background = 'red';
		currency.style.background = 'red';
	}
	else{
		display.style.background = 'white';
		currency.style.background = 'white';
	}
   
}

Thank you all for the responses and input.  The OnChange client script provided by @Muhammad Khan  worked perfectly and is an even better solution than what I was looking at.  The ServiceNow community is truly the greatest !!

I hope so that g_form.getDecimalValue() pointed by you in your answer, will also work for values like you have specified "USD;0,00", or for any other possible value which does not contain dot(.)

It does, that is why I suggested it 🙂