Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to partially mask the field value

Vasu ch
Kilo Sage

Hi,

I've a field "PAN Card Number" in user table. Whenever the user fills this field, I want to mask the value like BCXXXXXX1D. How can I do this? And how can I retrieve its value when needed?

 

Thank you.

1 ACCEPTED SOLUTION

Harsh_Deep
Giga Sage
Giga Sage

Hello @Vasu ch 

 

Write Before BR on the same table and use condition pannumber isnot empty

And use below script-

 

 

 

(function executeRule(current, previous /*, g*/) {
   
    var panField = 'pan_number'; // Change this to match your field name
    var visibleChars = 2; // Number of characters to leave visible (e.g., the first two and the last character)
    var pan = current.panField;
    if (pan && pan.length >= (visibleChars + 2)) {
        // Create a masked PAN
        var prefix = pan.substring(0, visibleChars); // First two characters
        var maskedChars = 'X'.repeat(pan.length - (visibleChars + 2)); // Masked characters
        var suffix = pan.slice(-1); // Last character
        var maskedPAN = prefix + maskedChars + suffix;
        current.panField = maskedPAN;
    }
})(current, previous);

 Please accept my solution and mark as helpful.

Thanks

View solution in original post

3 REPLIES 3

Harsh_Deep
Giga Sage
Giga Sage

Hello @Vasu ch 

 

Write Before BR on the same table and use condition pannumber isnot empty

And use below script-

 

 

 

(function executeRule(current, previous /*, g*/) {
   
    var panField = 'pan_number'; // Change this to match your field name
    var visibleChars = 2; // Number of characters to leave visible (e.g., the first two and the last character)
    var pan = current.panField;
    if (pan && pan.length >= (visibleChars + 2)) {
        // Create a masked PAN
        var prefix = pan.substring(0, visibleChars); // First two characters
        var maskedChars = 'X'.repeat(pan.length - (visibleChars + 2)); // Masked characters
        var suffix = pan.slice(-1); // Last character
        var maskedPAN = prefix + maskedChars + suffix;
        current.panField = maskedPAN;
    }
})(current, previous);

 Please accept my solution and mark as helpful.

Thanks

But how can we fetch the actual PAN value when required?

Anand Kumar P
Giga Patron

Hi @Vasu ch ,
Try below script in before BR when pan not empty

(function executeRule(current, previous /*null when async*/) {
 var panCard = current.u_pan_number;
    if (panCard) {
        var maskedPan = panCard.substring(0, 2) + "XXXXXX" + panCard.substring(8, 9);
        current.u_pan_number = maskedPan;
    }

})(current, previous);

AnandKumarP_0-1698691429976.png


Please mark it as helpful and solution proposed if it serves your purpose.

Thanks,

Anand