I want to update records in expedite table

keval3
Tera Contributor

Hi All,

please help me with below requirement

I have one expendably table in witch I have one field called serial no. with integer type, this field's value should increase when new record is created so for that I have written 1 BR and it is working fine. but now I want that previously added record's value should also sequence wise display in that record. please let me know how to resolved this.

I thinks I need write fix script for that but let me know the script. below After Insert BR I have written.

(function executeRule(current, previous /*null when async*/ ) {
 
    // Add your code here
    var ab = new GlideRecord('x_upl2_mx_digitize_sample_and_analysis');
    ab.query();
    var arrSN = [];
    while (ab.next()) {
        arrSN.push(parseInt(ab.u_serial_number));
    }
    var lrg=0;
    gs.info("test");
    for (var i = 0; i < arrSN.length; i++) {
        if (lrg < arrSN[i]) {
lrg = arrSN[i];
        }
 
    }
    current.u_serial_number = ++lrg;
    current.update();
 
 
})(current, previous);

   

2 REPLIES 2

Gurpreet07
Mega Sage

1. First you need to update existing data using a fix script:

 

 

// Initialize a starting serial number (1 in this case)
var serNum = 1;

// Create a GlideRecord object for the target table
var ab = new GlideRecord('x_upl2_mx_digitize_sample_and_analysis');

// Order the records by creation date for efficient iteration
ab.orderBy('sys_created_on');

// Execute the query to retrieve all records
ab.query();

// Loop through each record
while (ab.next()) {

// Update the record's "u_serial_number" field with the current serial number
ab.u_serial_number = serNum;

// Update the record in the database
ab.update();

// Increment the serial number for the next record
serNum++;
}

 
2. Business rule should be before insert Or you can use default value as well
 

// Retrieve the latest record (ordered by descending creation date)
var ab = new GlideRecord('x_upl2_mx_digitize_sample_and_analysis');
ab.orderByDesc('sys_created_on');
ab.setLimit(1); // Only fetch the most recent record
ab.query();

// Check if a record was found
if (ab.next()) {

// Extract the latest serial number and convert it to an integer
var latestNum = parseInt(ab.u_serial_number, 10);

// Set the current record's "u_serial_number" to the latest number + 1
current.u_serial_number = latestNum++;

}

 

Hi Gurpreet,

 

I have written fixscript given by you but it's not working properly. my expendably table is under x_upl2_mx_digitize_experiment table. so I am not getting serial no. in proper way so is it possible to autopupulate the seriol no. basis on 

Experiment ID I am keeping screen snap below for your reference.
keval3_0-1702545969079.png