- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2022 02:03 AM
Hi Team,
I am trying to update the existing record using workflow run script. It is querying the record and getting inside the loop, but not updating the values in the fields i am trying to update.
I have written the script like this:
var cred = 0;
var debt = 0;
var bal = 0;
var insjeli = new GlideRecordSecure('x_je_line_item_stagging_table');
insjeli.addQuery('number', current.sys_id);
insjeli.query();
while(insjeli.next()) {
var color = insjeli.getDisplayValue('color');
var amount = insjeli.getValue('amount');
if (color == 'Credit') {
cred = cred + parseInt(amount);
gs.info('Credit : ' + cred);
} else if (color == 'Debit') {
debt = debt + parseInt(amount);
gs.info('Debit : ' + debt);
}
}
bal = debt - cred;
gs.info('Balance : ' + bal);
var updateJE = new GlideRecordSecure('x_je_stagging_table');
updateJE.addQuery('sys_id', insjeli.getValue('number'));
updateJE.query();
if (updateJE.next()) {
gs.info('.........................'); //logging this message
updateJE.cred = cred;
updateJE.debt = debt;
updateJE.balance = bal;
updateJE.update(); // not updating the field values
gs.info(updateJE.getValue(cred)); // returning null, means value didn't get updated
}
I don't know where i have done wrong.
Any suggestions will help.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2022 03:51 AM
Hi,
Does the user whose session the workflow run script is running has write access to the table since you are using GlideRecordSecure; it would evaluate the ACLs
Did you check table level WRITE ACLS on cred, debt, balance is blocking?
Try to use GlideRecord
var updateJE = new GlideRecord('x_je_stagging_table');
updateJE.addQuery('sys_id', insjeli.getValue('number')); // should you pick number
updateJE.query();
if (updateJE.next()) {
gs.info('.........................'); //logging this message
updateJE.cred = cred;
updateJE.debt = debt;
updateJE.balance = bal;
updateJE.update(); // not updating the field values
gs.info(updateJE.getValue(cred)); // returning null, means value didn't get updated
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2022 02:12 AM
Hi,
are you giving correct field here to query?
Also try to update as this
var cred = 0;
var debt = 0;
var bal = 0;
var insjeli = new GlideRecordSecure('x_je_line_item_stagging_table');
insjeli.addQuery('number', current.sys_id);
insjeli.query();
while(insjeli.next()) {
var color = insjeli.getDisplayValue('color');
var amount = insjeli.getValue('amount');
if (color == 'Credit') {
cred = cred + parseInt(amount);
gs.info('Credit : ' + cred);
} else if (color == 'Debit') {
debt = debt + parseInt(amount);
gs.info('Debit : ' + debt);
}
bal = debt - cred;
gs.info('Balance : ' + bal);
var updateJE = new GlideRecordSecure('x_je_stagging_table');
updateJE.addQuery('sys_id', insjeli.getValue('number')); // should you pick number
updateJE.query();
if (updateJE.next()) {
gs.info('.........................'); //logging this message
updateJE.cred = cred;
updateJE.debt = debt;
updateJE.balance = bal;
updateJE.update(); // not updating the field values
gs.info(updateJE.getValue(cred)); // returning null, means value didn't get updated
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2022 02:18 AM
Hi Ankur,
Yeah, I have given the correct field in the query. The 'number' will return the sys_id. The query is fine. It is getting the record, but not updating the fields.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2022 03:51 AM
Hi,
Does the user whose session the workflow run script is running has write access to the table since you are using GlideRecordSecure; it would evaluate the ACLs
Did you check table level WRITE ACLS on cred, debt, balance is blocking?
Try to use GlideRecord
var updateJE = new GlideRecord('x_je_stagging_table');
updateJE.addQuery('sys_id', insjeli.getValue('number')); // should you pick number
updateJE.query();
if (updateJE.next()) {
gs.info('.........................'); //logging this message
updateJE.cred = cred;
updateJE.debt = debt;
updateJE.balance = bal;
updateJE.update(); // not updating the field values
gs.info(updateJE.getValue(cred)); // returning null, means value didn't get updated
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2022 02:29 AM
A/C to docs:
“when using GlideRecordSecure, non-writable fields are set to NULL when trying to write to the database. By default, canCreate() on the column is replaced with canWrite() on the column. If that returns false, the column value is set to NULL.”
https://docs.servicenow.com/bundle/sandiego-application-development/page/script/glide-server-apis/topic/p_GlideServerAPIs.html#concepthrq5bcgp
Aman Kumar