How Do I Properly Increment a Field?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2016 11:22 AM
Hello All -
I have a table that has three fields: u_date, u_total and u_total_amount
u_total_amount is a calculated field based on u_total.
I am trying to write a Client Script that checks the table for an existing date. If the date is not in the table, a new record is inserted. I have this part working in the code below. Where I am running into difficulty is with a record for an existing date. If the date exists, I want to increment u_total by one.
Here is what I have so far:
function onSubmit() {
var dateNow = g_form.getValue('u_entry_date');
var dateCheck = new GlideRecord('u_daily_totals');
dateCheck.addQuery('u_date',dateNow);
dateCheck.query();
if (dataCheck.get(current.u_total)){
******************
This is where I am having trouble
*******************
} else {
var insertRec = new GlideRecord('u_daily_totals');
insertRec.initialize();
insertRec.u_date = dateNow;
insertRec.insert();
}
}
The starred section is where I'm having problems. I cannot figure out how to read in the u_total value of the current record, increment it by one and save it back to the u_total field.
Any help the community could provide would be appreciated!
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2016 11:27 AM
Hi Chris,
There is a error. It should be getValue and not get
var dateCheck = new GlideRecord('u_daily_totals');
dateCheck.addQuery('u_date',dateNow);
dateCheck.query();
if(dateCheck.next())
{
var total = dataCheck.current.u_total; //will give you the total value
handle your logic here
}
Also the best recommend/practice is to make a GlideAjax call and handle the logic there instead of making GlideRecord calls from client script. More info here.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2018 02:09 PM
Hey, so I am running into this same issue in my service portal server script. I could just call current to determine the previous value and + 1 to adjust the field in the table?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2016 11:38 AM
If all you want to do is increment the u_total field, why not
g_form.setValue('u_total', g_form.getValue('u_total') + 1);
in your starred section. If it thinks it's a string field (and you get a result like 11 instead of 2, then throw in a parseInt like this.
g_form.setValue('u_total', parseInt(g_form.getValue('u_total'), 10) + 1);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2016 11:55 AM
Thanks Guys for the replies. Pradeep's suggestion did not work and Chuck, I'm trying to update the field onSubmit. I don't think g_form helps with that does it?