GlideRecordSecure updating the sys_created_on field empty

Anshu_Anand_
Kilo Sage
Kilo Sage

Hi,

 

When using GlideRecordSecure in updating any table in servicenow, sys_created_on field is being empty

It should not happen as its OOTB field.

When using GlideRecord it never happen

 

var table = 'x_mile';
var query = "sys_id=215fd925f703b154a2ffe9b84851e035";
var data = {"state":"open","milestone":"cd9bdbcaf7a755105ef5eeb74851e0e8"};
var secure = true;
 
grUpdateRecords = new GlideRecordSecure(table);
grUpdateRecords.addEncodedQuery(query);
grUpdateRecords.query();
while (grUpdateRecords.next()) {
for (var element in data) {
 
gs.info("data[element]  = "+data[element] );
grUpdateRecords.setValue(element, data[element]);
grUpdateRecords.update();
}
}

 

 

Regards,
Anshu
2 REPLIES 2

Iraj Shaikh
Mega Sage
Mega Sage

Hi @Anshu_Anand_ 

The issue you're describing seems to be related to the sys_created_on field being cleared when updating records using GlideRecordSecure. Normally, the sys_created_on field is a read-only field that gets set when a record is created and should not be changed.

In the code snippet you provided, there is a loop that iterates over the data object and updates each field in the current GlideRecordSecure instance. If the sys_created_on field is somehow included in the data object, it could be inadvertently cleared or modified.

However, based on the data object you've shown, there are only two fields being updated: state and milestone. The sys_created_on field is not mentioned, so it should not be affected by this code.

Here are a few things to check or consider:

  1. Ensure that the sys_created_on field is not being included in the data object elsewhere in your code.
  2. Verify that no business rules, client scripts, or other scripts are interfering and causing the sys_created_on field to be cleared.
  3. Check if there are any customizations on the instance that might affect system fields like sys_created_on.
  4. Make sure that the sys_created_on field is not set to be writable in the dictionary settings, as this could potentially allow the field to be modified.

Additionally, there is a potential issue in your code where the update() method is called inside the loop for each field being updated. This means that if there are multiple fields in the data object, the record will be updated multiple times, which is not efficient. Instead, you should set all the values first and then call update() once after the loop:

 

var table = 'x_mile';
var query = "sys_id=215fd925f703b154a2ffe9b84851e035";
var data = {"state":"open","milestone":"cd9bdbcaf7a755105ef5eeb74851e0e8"};
var secure = true;

var grUpdateRecords = new GlideRecordSecure(table);
grUpdateRecords.addEncodedQuery(query);
grUpdateRecords.query();
if (grUpdateRecords.next()) {
    for (var element in data) {
        gs.info("data[element]  = "+data[element]);
        grUpdateRecords.setValue(element, data[element]);
    }
    grUpdateRecords.update();
}

 

 

If the sys_created_on field is still being cleared after ensuring the above points, it might be necessary to contact ServiceNow support for further assistance, as this behavior is not expected and could be a platform issue.

 

Please mark this response as correct or helpful if it assisted you with your question.

 

Rajdeep Ganguly
Mega Guru


The issue you're experiencing might be due to the fact that you're updating the record inside the loop for each element in the data object. This could potentially cause the sys_created_on field to be reset. Here's a revised version of your code:

javascript
var table = 'x_mile';
var query = "sys_id=215fd925f703b154a2ffe9b84851e035";
var data = {"state":"open","milestone":"cd9bdbcaf7a755105ef5eeb74851e0e8"};
var secure = true;

var grUpdateRecords = new GlideRecordSecure(table);
grUpdateRecords.addEncodedQuery(query);
grUpdateRecords.query();

while (grUpdateRecords.next()) {
for (var element in data) {
gs.info("data[element] = "+data[element] );
grUpdateRecords.setValue(element, data[element]);
}
grUpdateRecords.update();
}


In this revised version, the update() method is called only once per record, after all the fields in the data object have been set. This should prevent the sys_created_on field from being reset.

Here are the key points:

- The sys_created_on field is a system field that is automatically set when a record is created and should not be manually updated.
- The issue might be due to the update() method being called inside the loop for each element in the data object.
- The revised code calls the update() method only once per record, after all the fields in the data object have been set.
- This should prevent the sys_created_on field from being reset.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER