Script to remove/redact PHI/PII gets error even when elevated and in sandbox
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2024 03:26 PM
I am trying to setup a script to remove and redact PHI and PII in my instance. When running in sandbox, i get an error even when i am running with elevated privelage and as admin:
(function() {
// Define the table to scan and the fields to check
var tableName = 'incident';
var fieldsToCheck = ['description', 'short_description', 'comments'];
// Define regex patterns for PII
var piiPatterns = [
// Date of Birth (DOB) - various formats
/\b(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/\d{4}\b/, // MM/DD/YYYY
/\b(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/\d{4}\b/, // DD/MM/YYYY
/\b\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])\b/, // YYYY-MM-DD
// Other PHI (add more patterns as needed)
// /\b...your_phi_pattern_here...\b/
// Social Security Numbers (SSNs)
/\b\d{3}-\d{2}-\d{4}\b/,
// Credit Card Numbers (Visa, MasterCard, Amex)
/\b(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13})\b/
];
var gr = new GlideRecord(tableName);
gr.query();
while (gr.next()) {
var recordUpdated = false;
fieldsToCheck.forEach(function(field) { // Traditional function declaration
if (gr[field]) {
var fieldValue = gr[field].toString();
piiPatterns.forEach(function(pattern) { // Traditional function declaration
if (pattern.test(fieldValue)) {
fieldValue = fieldValue.replace(pattern, '[REDACTED]');
recordUpdated = true;
}
});
gr[field] = fieldValue;
}
});
if (recordUpdated) {
gr.update();
}
}
gs.log('PHI/PII removal script completed for table: ' + tableName);
})();
the error i recieve is below:
-------------------------------------------------
Not sure how to move forward with this any help is welcome.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2024 03:58 PM
Hi @RBlor - Where are you running the script? Minimally, gs.log() and gr.update() are restricted in sandbox...I'm not even sure you can run that while loop. See: Configuring Script sandbox property
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2024 04:23 PM
i see i was just trying to test the script in a sandbox subprod enviroment. what is the best way approach the script ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2024 04:42 PM
Understanding you’re not in production, you could comment the gr.update() and replace it with gs.info() to log the INC number—and optionally which field(s) would be updated—then run it as a background script.
You might also consider simply running the script and using rollback contexts to roll back the updates.