How to Access Elements in Form's Embedded List with JavaScript?

lmcmanamon
Giga Contributor

Hello All!

I'm new to using ServiceNow and trying to brush up on my JavaScript. I'm encountering a small issue with not knowing how to access certain elements on my form. Basically, as you can see from the screencap, I have a variety of fields that are of data type "Decimal" in my Product Table Embedded List. I'd like to have it so that the summation of all those fields is put into the "Total Dealer Cost" field. Below is a screencap of both my form and my current Client Script for just when the Admin field is edited. For the script, it alerts that onCellEdit has been entered and that findTotalDealerCost has been entered, so fails when I try to access the Admin field. Could someone point me in the right direction?

Rep Support Form:

Rep Support Form.PNG

Client Script:

Client Script.PNG

11 REPLIES 11

Ashok Katam
Mega Guru

Hi Lauren



Can you please try to use getDecimalValue instead of getValue in your script and try to run.. and please remove parseDouble too ...   Thanks !


It's not throwing an error anymore, but now there's another issue: no matter what I put in, it gathers every field to be 0, even the one I'm currently editing, no matter what number I have. Thoughts? Keep in mind, I want the values to change pre submit. Also, maybe I have to include something to syntactically access the correct/current row?  


onCellEdit type scrpyt runs on a list view rather than a form, i think what you need here is onChange script.


Or even better


Make the 'total' field as calculated and in the calculation you can provide the sum of the fields it is being calculated from, read more here


Calculated

[Advanced view]


Determines whether the value of the field is calculated from other values. If selected, use the Calculation field to define how the calculation is performed. When sorting or grouping by a calculated field, the sort order is based on the field value from the last time the field was updated, not the last time the field was displayed. Note: In relation to business rules, calculated fields are populated first before any business rule, even a before business rule, is run. Calculated fields are then populated again if necessary after any before business rules run.
Calculation

[Advanced view]


When the Calculated field is selected, allows you to enter a script for calculating the value of the field. You can use the current object in this script. Just as with access control rules, the script can:
  • Evaluate to true or false.
  • Return an answer variable set to true or false.
  • Set a field value directly, such as: current.display_name="name".

Note: You can override the calculation for extended tables.


-Anurag

Where/ how would I set up "Total Dealer Cost" to be a calculated value? In the table definition, because I didn't seem to see anything related there?