Existing record is not getting updated using workflow run script

Yesh
Tera Contributor

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

1 ACCEPTED SOLUTION

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

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

View solution in original post

5 REPLIES 5

John Dahl
Tera Guru

Your query is comparing a number value to a sys_id. The resulting updateJE GlideRecord is mode likely empty and would not result in any updates.

updateJE.addQuery('sys_id', insjeli.getValue('number'));