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.

Auto-populate percentage on a field

Vaishali 11
Tera Guru

Hi, 

I have a requirement where on the intake form there are 2 fields, total cost and % of funding. There is a 3rd field total funding cost which populates the percentage value of above fields. For ex, if % of funding-60, total cost- 100 then total funding cost should show 60.

How this can be achieved?

 

Thanks in advance!!

5 REPLIES 5

dbook
Mega Sage

You could do this with both Business Rules and Client scripts. Possibly both depending on your use case. 

 

Something like the below Business Rule could work for you.

// Table: your intake table (e.g., x_myapp_intake)
// When: Before Insert, Before Update
(function executeRule(current, previous /*null when async*/) {
  var cost = parseFloat(current.total_cost || 0);
  var pct  = parseFloat(current.funding_percent || 0);
  var funding = (cost * pct) / 100;

  // If the field is currency/decimal, set to a number
  current.total_funding_cost = funding.toFixed( 2 );
})();


If you want to display within a Catalog item you'll need a Catalog Client Script, something like this should work...

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

  // Recalculate whenever either variable changes
  function recalc() {
    var cost = parseFloat(g_form.getValue('total_cost')) || 0;          // currency/number variable name
    var pct  = parseFloat(g_form.getValue('funding_percent')) || 0;     // % variable name (0–100)
    var funding = (cost * pct) / 100;
    g_form.setValue('total_funding_cost', funding.toFixed( 2 ));          // currency variable name
  }
  recalc();
}

Brad Bowman
Kilo Patron
Kilo Patron

The implementation of this solution depends on when you need/want to see the percentage field populated.  If this must be done on the form when total cost and funding change, then you'll need two onChange client scripts, triggered when each variable changes.  If you don't need to see the percentage populated until after the intake form is submitted/updated, then you can use a business rule or flow to set the value.  

Mohammed8
Mega Sage

I agree with @Brad Bowman    , for this requirement if on form you will need two on-Change client scripts, because either of the two fields may change, and the third field must be recalculated whenever either input is updated.

You should also confirm the data types of the fields involved. If any of them are string fields, you will need to type convert the values to numbers before performing calculations.

Additionally, if the calculated percentage must stay within a specific range (for example 0–100), make sure to include validation to prevent the value from exceeding the allowed limits.

 

Thanks and Regards,

Mohammed Zakir

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Vaishali 11 

what did you start with and where are you stuck?

Unless you start from your side you won't learn.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader