How to update a table's multiple record's field value in a iterating manner?

vishaljaiswal
Giga Expert

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?

@Ankur Bawiskar 

 

4 REPLIES 4

Mark Manders
Mega Patron

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

Hi @Mark Manders ,

 

thanks for the response. I tried your logic but it updates all record to 00010, it doesn't incrementing.

Ankur Bawiskar
Tera Patron
Tera Patron

@vishaljaiswal 

this doesn't sound like a valid business requirement?

 

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

Hi @Ankur Bawiskar 

This is actual business requirement, i just given a scenario instead of actual details to save the integrity of data.