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

yaswanthi2
Giga Sage

Hi @ST9 

If the field violation hours or multiplier is newValue then in above script in line 2 remove newValue==''

Tai Vu
Kilo Patron
Kilo Patron

Hi @ST9 

"Everything is working fine except i cannot make the Liability Violation Hours(decimal type field) field as empty when the violation hours as empty."

It's because of this condition in your script.

   if (isLoading || newValue === '') {
      return;
   }

 

Let's try the below adjustment.

#OnChange Client Script (Violation hours)

 

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

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

    var ll = newValue * g_form.getValue("multi");
    g_form.setValue("liability_violation_hours", ll);

}

 

 

You can do the same for the Multiplier variable.

 

Cheers,

Tai Vu

ST9
Tera Contributor

@Tai Vu : Hi, I have tried your code and it worked. Could you help me with adding the commas to correct places,
For example - if Multiplier is 1 and Violation hours = 111,111,111 then Liability Violation Hours = 111,111,111

but for me it shous without comma .. 111111111 

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