The CreatorCon Call for Content is officially open! Get started here.

Adding sum of integer fields to another field on a form (Client Side)

Inactive_User15
Kilo Contributor

I'm trying to sum multiple integer fields into one on a form. I would like this to be done before the form is saved or submitted AND I need this calculation to trigger based on multiple fields. 

I have about 9 fields that if either one is NOT NULL then I want the calculation to run and populate the sum into a total field. Due to these conditions I'm trying to accomplish this in an UI policy rather than a client script. 

My current code is the following. I've tried using parseInt and adding '+' in front of the integer field. With the code below the field being populate with the total is stating NaN. 

 

function onCondition() {

var appDev = g_form.getValue('u_app_dev_eta_of_hours');
var applServ = g_form.getValue('u_appl_services_emr');
var sumHours = parseInt(appDev) + parseInt(applServ);
g_form.setValue('u_total_hrs_for_prj_rsrc_work',sumHours);
//g_form.setDisplay('u_total_hrs_for_prj_rsrc_work', +u_app_dev_eta_of_hours + 'u_appl_services_emr');


}

13 REPLIES 13

Abhijit4
Mega Sage

Hi David,

UI Policy is not as simple as it looks,

First thing to note, you have checked onLoad option in your UI Policy so it will run on load of page. If one of the value of 'u_app_dev_eta_of_hours' or 'u_appl_services_emr' field is null or empty, it will set value of 'u_total_hrs_for_prj_rsrc_work' to NaN

and even if you change value of variables, it will not trigger UI policy(as it already has executed onLoad of page). If you want to execute UI policy again you have to make your ui policy When to Apply condition to be false ( Make all fields empty so that When to Apply UI policy condition becomes false) then again when you write value to one of it's variable which will satisfies UI Policy condition to true and it will execute UI policy which will again set your result to NaN and then you will enter second field value but this time UI policy won't execute (It retains it's condition values to true and when you change your second field value it is still true so it will not execute UI policy)

 

Not sure but you may not understand what I am trying to explain but believe me I was facing similar kind of issue with UI policy, it took me 3 days to understand this concept of UI policy thoroughly.

If you want to test, remove When to Apply condition of your UI policy and change it to 'u_app_dev_eta_of_hours' is not empty AND 

'u_appl_services_emr' is not empty (here ANDing is important) , believe me this time you will get your result when you enter integer in your both field.

 

Solution for your problem is I would say go for OnSubmit client script, It will be the best solution for you. If you want it on change only then we have to find other way.

 

Let me know If you need more help.

 

Please mark it as correct or helpful if it really helps.

 

Thanks.

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

As for the on load, it states that it will run on load AND when the values change...so the latter is what I was banking on.

 

Ok, so I simplified my conditions to only the fields I'm currently testing and change the operator from OR to AND. 

This worked...sort of. So when I first enter a number into the fields then it calculates. If I change it, it does not update the SUM. 

 

I'm trying to get this to work so the user is able to see the total while working on the form rather than having to save/submit in order to see it. 

The whole reason I'm trying through a UI policy, as I stated, is due to the limitation of the client script to only trigger off one field. I had a crazy thought while trying to get this to work though....'

So what if I created a boolean field that was hidden. Create a client script against it that runs the calculation to populate the total hours and populate the total field. This boolean field gets set to 'true' by the UI policy that looks at each individual hour field. LOL it seems like there should be an easier way to accomplish something so simple, but that should work. 

Other thoughts?

That looks interesting idea but again question arises here, how your mentioned UI policy will trigger?

I think, again you are coming back to the same problem.

 

I think we have only two solution for this,

1) Either create individual onChange client script for each variable and add "total=total+g_form.getValue('variable name for which client script written') // Change name of field accordingly

2) Create UI policy for each variable(9 UI policies for 9 variables) and write When to Apply condition as variable name is not empty.

and as I said you can create onSubmit client script.

 

I don't think, there will be any other solution for this.

 

Mark it as correct or helpful if it really helps.

Thanks

 

 

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Thanks again for the help. I'm sticking with a business rule right now as it's not worth having that many separate scripts. Added a hint to show users that it will be calculated on save/submit/update.