Omkar Mone
Mega Sage

Can you at copy paste the below script and check the logs of what all you can see in the system logs table - 

 

(function executeRule(current, previous /*null when async*/) {
gs.log('Before Insert Business Rule triggered for table: ' + current.getTableName());

const generateUniqueCode = () => {
return Array.from({ length: 2 }, () => "ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(Math.random() * 26 | 0)).join('') +
Array.from({ length: 8 }, () => Math.random() * 10 | 0).join('');
};

if (!current.u_unique_code) {
gs.log('u_unique_code is empty. Generating a new unique code.');

const gr = new GlideRecord('sys_user');
let uniqueCode, attempts = 0;

do {
gs.log('Attempt number: ' + (attempts + 1));

if (++attempts > 100) {
gs.addErrorMessage('Unable to generate a unique code. Please contact the administrator.');
current.setAbortAction(true);
gs.log('Error: Maximum attempts exceeded while generating unique code.');
return;
}

uniqueCode = generateUniqueCode();
gs.log('Generated unique code: ' + uniqueCode);

gr.initialize();
gr.addQuery('u_unique_code', uniqueCode);
gr.addQuery('sys_id', '!=', current.sys_id);
gr.query();

} while (gr.hasNext());

current.u_unique_code = uniqueCode;
gs.log('Assigned unique code: ' + uniqueCode);
} else {
gs.log('u_unique_code already exists: ' + current.u_unique_code);
}

gs.log('Before Insert Business Rule execution completed.');
})(current, previous);