Help with OnLoad Client Script with Currency Field

jlaue
Kilo 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

Yousaf
Giga Sage

Hi,

try (g_form.value=='USD:0')

Client script to change the background color of a field

 

Mark Correct or Helpful if it helps.


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

-O-
Kilo Patron
Kilo Patron

How about

if (g_form.getDecimalValue('base_rent_per_month') == 0)

?

Unless the currency type is important.

Muhammad Khan
Mega Sage
Mega Sage

Hi jlaue,

 

Consider it as a workaround to fulfill your requirement. Create an onChange Client Script on your currency field, in this way it will work for changes as well.

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

   //Type appropriate comment here, and begin script below
	var display = g_form.getControl('<you_table_name>.<your_field_name>.display');       // Make sure to replace properly.
	var currency = g_form.getControl('<you_table_name>.<your_field_name>.currency');  //   Make sure to replace properly.
	
	if(newValue == 'USD;0.00'){
		display.style.background = 'red';
		currency.style.background = 'red';
	}
	else{
		display.style.background = 'white';
		currency.style.background = 'white';
	}
   
}

This "<you_table_name>.<your_field_name>.display" is basically the id of a part (where you type your currency in decimal) of your currency field element in the html,

while

"<you_table_name>.<your_field_name>.currency" is the id of other part (where you change currency like $, CHF, £ etc.) of you currency field element in the html.

 

You can do inspect of your currency field and observe these values, just make sure you provide these ids accurately.

 

Hopefully this will resolve your query.

That's a nice trick - selecting the display boxes of the composite control. But I would not do  == 'USD;0.00'. If someone selects a locale where the decimal separator is not the . it will fail. E.g. if someone selects German as language, the value returned will be 'USD;0,00', not 'USD;0.00'.