diff --git a/Server-Side Components/Business Rules/Use_case_Base64-Encode-Before-Save-And-Decode-on-Display/Before_BR_script.js b/Server-Side Components/Business Rules/Use_case_Base64-Encode-Before-Save-And-Decode-on-Display/Before_BR_script.js new file mode 100644 index 0000000000..77908f5b65 --- /dev/null +++ b/Server-Side Components/Business Rules/Use_case_Base64-Encode-Before-Save-And-Decode-on-Display/Before_BR_script.js @@ -0,0 +1,17 @@ + + +//Encode Before Saving Trigger: Before Insert/Update + +(function executeRule(current, previous) { + if (current.fieldName.changes()) { + var plainText = current.fieldName + ''; + current.fieldName = GlideStringUtil.base64Encode(plainText); + } +})(current, previous); +//If the field is new or updated, encodes its value to Base64. Saves the encoded string to the database. + +// For Example User enters Hello World in fieldName. +//Before saving : System encodes and stores SGVsbG8gV29ybGQ= in the database. +//When the record is viewed : Display rule decodes it back to Hello World for the user. + + diff --git a/Server-Side Components/Business Rules/Use_case_Base64-Encode-Before-Save-And-Decode-on-Display/Display_BR_script.js b/Server-Side Components/Business Rules/Use_case_Base64-Encode-Before-Save-And-Decode-on-Display/Display_BR_script.js new file mode 100644 index 0000000000..488c9a7d64 --- /dev/null +++ b/Server-Side Components/Business Rules/Use_case_Base64-Encode-Before-Save-And-Decode-on-Display/Display_BR_script.js @@ -0,0 +1,19 @@ +// Decode on Display Trigger: Before Display +////Decodes fieldName from Base64. Shows decoded value on the form. + +(function executeRule(current) { + if (current.fieldName) { // field name can be anything + try { + var decoded = GlideStringUtil.base64Decode(current.fieldName); + current.setDisplayValue('fieldName', decoded); + } catch (ex) { + current.setDisplayValue('fieldName', '[Invalid Base64]'); + } + } +})(current); + +//If the field is new or updated, encodes its value to Base64. Saves the encoded string to the database. + +// For Example User enters Hello World in fieldName. +//Before saving : System encodes and stores SGVsbG8gV29ybGQ= in the database. +//When the record is viewed : Display rule decodes it back to Hello World for the user. diff --git a/Server-Side Components/Business Rules/Use_case_Base64-Encode-Before-Save-And-Decode-on-Display/Readme.md b/Server-Side Components/Business Rules/Use_case_Base64-Encode-Before-Save-And-Decode-on-Display/Readme.md new file mode 100644 index 0000000000..c6f18138a6 --- /dev/null +++ b/Server-Side Components/Business Rules/Use_case_Base64-Encode-Before-Save-And-Decode-on-Display/Readme.md @@ -0,0 +1,10 @@ +Base64 Encode/Decode Business Rule +Overview + +This setup demonstrates how to store field data securely in Base64 format in the database, while still displaying it as human-readable text to end users in ServiceNow. + +It uses two Business Rules: + +Before Insert/Update Rule : Encodes plain text into Base64 before saving to the database. + +Display Rule : Decodes the Base64 value back into readable text when loading the record form.