Auto generate number field with masked number

Rajkumar Bommal
Tera Contributor

Hi,

am working on a record producer , i want to Auto-generated running number field in table (2000 appended with masked NRIC, leaving last 4 characters visible) 2000-*****123F, when record is inserted in the table.

1 ACCEPTED SOLUTION

@Rajkumar Bommal 

is this field "nric" mapped with some variable?

How it's getting populated?

Did you print what came in that field in your BR?

try this and share what came in logs

(function executeRule(current, previous /*null when async*/) {
    gs.info('Business Rule started for record: ' + current.sys_id);

    // Convert NRIC to string to ensure proper slicing
    var nric = current.nric + '';
    gs.info('NRIC value: ' + nric);

    // Check if NRIC length is at least 4 characters
    if (nric.length >= 4) {
        // Get the last four characters of the NRIC
        var lastFour = nric.slice(-4);
        gs.info('Last four characters of NRIC: ' + lastFour);

        // Set the identification number with the masked NRIC
        current.u_identification_number = '2000-*****' + lastFour;
        gs.info('Generated identification number: ' + current.u_identification_number);
    } else {
        // Handle cases where NRIC is less than 4 characters
        current.u_identification_number = '2000-*****ERR';
        gs.info('NRIC is less than 4 characters, setting identification number to: ' + current.u_identification_number);
    }

    gs.info('Business Rule completed for record: ' + current.sys_id);
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

7 REPLIES 7

@Ankur Bawiskar 
No user won't enter any number in record Producer all he enter is details of the employee and his managing person details 
i have used a script in business rule but it is not working
Script:

(function executeRule(current, previous /*null when async*/ ) {
    var nric = current.nric + '';
    if (nric.length >= 4) {
        var lastFour = nric.slice(-4);
        current.u_identification_number = '2000-*****' + lastFour;
    } else {
        current.u_identification_number = '2000-*****ERR';
    }

})(current, previous);

@Rajkumar Bommal 

is this field "nric" mapped with some variable?

How it's getting populated?

Did you print what came in that field in your BR?

try this and share what came in logs

(function executeRule(current, previous /*null when async*/) {
    gs.info('Business Rule started for record: ' + current.sys_id);

    // Convert NRIC to string to ensure proper slicing
    var nric = current.nric + '';
    gs.info('NRIC value: ' + nric);

    // Check if NRIC length is at least 4 characters
    if (nric.length >= 4) {
        // Get the last four characters of the NRIC
        var lastFour = nric.slice(-4);
        gs.info('Last four characters of NRIC: ' + lastFour);

        // Set the identification number with the masked NRIC
        current.u_identification_number = '2000-*****' + lastFour;
        gs.info('Generated identification number: ' + current.u_identification_number);
    } else {
        // Handle cases where NRIC is less than 4 characters
        current.u_identification_number = '2000-*****ERR';
        gs.info('NRIC is less than 4 characters, setting identification number to: ' + current.u_identification_number);
    }

    gs.info('Business Rule completed for record: ' + current.sys_id);
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Rajkumar Bommal 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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