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.

Client Script Code query

ST9
Tera Contributor

Hi All,

 

I have a requirement to calculate the Liability Violation Hours where Liability Violation Hours = Violation hours * Multiplier.
I have written a onChange Client script for above cal.
Everything is working fine except i cannot make the Liability Violation Hours(decimal type field) field as empty when the violation hours as empty.
Please help to correct my code

 

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

   var vh = g_form.getValue("violation_hour");
   var mul = g_form.getValue("multi");
	var lvh;
	if((mul!='' && vh != 0)){
		lvh= vh*mul;
		 g_form.setValue("liability_violation_hours", lvh);
	}
	else if((mul==''|| mul!='')&& vh===0){
		var ll=vh*mul*0;
		 g_form.setValue("liability_violation_hours", ll);
	}
	else if((mul==''|| mul!='')&& vh===''){
		g_form.setValue("liability_violation_hours", '');
	}
	
}

 

 

1 ACCEPTED SOLUTION

Hi @ST9 

You can try the replace method below.

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

    if (newValue === '') {
        g_form.clearValue("liability_violation_hours");
        return;
    }

    var violationHours = g_form.getIntValue('violation_hour') * g_form.getIntValue('multi');
    g_form.setValue("liability_violation_hours", violationHours.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));

}

Screenshot 2023-11-21 at 20.42.53.png

 

Cheers,

Tai Vu 

View solution in original post

5 REPLIES 5

praveenhirekudi
Tera Contributor

@ST9 

This can be controlled using Attributes on the field. You need to create a format attribute and set it to none as shown below. Open the fields dictionary entry and click on the Attributes Related list and add it there. Screenshot 2023-11-14 214949.png

 

Please Accept the solution if it works for you