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

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

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 

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

Aman Kumar S
Kilo Patron

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

Best Regards
Aman Kumar