How to update a table's multiple record's field value in a iterating manner?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2024 05:52 AM
Scenario: As per the business requirement, let's say i have a table 'xyz' and putting adquery like 'status is new'. After that if 10 records are fetched then i have to update all 10 records field 'Cost Center' as 00010...00020...00030...00040 and so on.
This number should start from <00010> and increases on the interval of 10 as above sequentially.
any idea to solve this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2024 06:07 AM
Something like this?
// Define the initial value for the Cost Center for the first batch
var batchCostCenterValue = 10;
// Query the 'xyz' table for records where the status is 'new'
var xyzRecord = new GlideRecord('xyz');
xyzRecord.addQuery('status', 'new');
xyzRecord.orderBy('sys_created_on'); // Assuming you want to order by creation date or choose another field if necessary
xyzRecord.query();
var count = 0; // Counter to keep track of the number of records processed in the current batch
while (xyzRecord.next()) {
// Check if the count reaches 10, if so, increment the batchCostCenterValue by 10 and reset the counter
if (count >= 10) {
batchCostCenterValue += 10;
count = 0; // Reset the counter for the new batch
}
// Format the Cost Center value for the current batch
var formattedCostCenter = ('0000' + batchCostCenterValue).slice(-5);
// Update the 'Cost Center' field with the formatted value for the current batch
xyzRecord.setValue('cost_center', formattedCostCenter);
xyzRecord.update();
// Increment the counter since a record has been processed
count++;
}
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2024 06:26 AM
Hi @Mark Manders ,
thanks for the response. I tried your logic but it updates all record to 00010, it doesn't incrementing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2024 06:54 AM
this doesn't sound like a valid business requirement?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2024 07:13 AM
This is actual business requirement, i just given a scenario instead of actual details to save the integrity of data.