- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2022 11:18 PM
Hi,
I need to decrypt the value using the Business Rule shown below.
(function executeRule(current, previous /*null when async*/) {
if (gs.hasRole('x_540069_sn21.passwdCanRead_2')) {
var encrString = current.u_password_string;
var decrString = '';
var encr = new GlideEncrypter();
decrString = encr.decrypt(encrString);
current.u_password_string = decrString;
current.u_passwdstring = '';
}
})(current, previous);
I have checked the contents of "encrString" using "Script Debugger" as follows
encrString: I+tYsVN4xvz5915A7/EDfg==
I would like to decrypt this with the above Business Rule, but the executed value shows "undefined".
What is wrong with my Business Rule?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
-
Team Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2022 01:08 AM
Script include in Global with "Accessbile from" set to "All application scopes" and "Client callable" unchecked. There's already OOTB script include names"EncryptUtil" so named it "EncryptUtil2".
var EncryptUtil2 = Class.create();
EncryptUtil2.prototype = {
initialize: function() {
},
encryptText: function(text) {
var encr = new GlideEncrypter();
return encr.encrypt(text);
},
decryptText: function(text) {
var encr = new GlideEncrypter();
return encr.decrypt(text);
},
type: 'EncryptUtil2'
};
As an example, created 3 fields name "Clear text" (u_clear_text), "Encrypted String" (u_encrypted), "Decrypted String" (u_decrypted).
Created Client script to encrypt value of "Clear text" and set it in "Encrypted String".
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ajax = new GlideAjax('EncryptText');
ajax.addParam('sysparm_name', 'encryptText');
ajax.addParam('sysparm_text', newValue);
ajax.getXMLAnswer(function(answer) {
if (answer.length > 0) {
g_form.setValue('u_encrypted', answer);
}
});
}
Script Include named "EncryptText" that's client callable.
var EncryptText = Class.create();
EncryptText.prototype = Object.extendsObject(AbstractAjaxProcessor, {
encryptText: function() {
var text = this.getParameter('sysparm_text');
var encr = new GlideEncrypter();
return encr.encrypt(text);
},
decryptText: function() {
var text = this.getParameter('sysparm_text');
var encr = new GlideEncrypter();
return encr.decrypt(text);
},
type: 'EncryptText'
});
Business rule:
(function executeRule(current, previous /*null when async*/ ) {
try {
var encr = new global.EncryptUtil2();
var encrypted = current.u_encrypted;
var decrypted = encr.decryptText(encrypted);
current.u_decrypted = decrypted;
} catch (e) {
current.u_decrypted = e.message;
}
})(current, previous);
Execution:
Step 1: Enter value into Clear text. Encrypted String is set.
Step 2. Save form. Decrypted text is set in Decrypted String

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2022 01:08 AM
Script include in Global with "Accessbile from" set to "All application scopes" and "Client callable" unchecked. There's already OOTB script include names"EncryptUtil" so named it "EncryptUtil2".
var EncryptUtil2 = Class.create();
EncryptUtil2.prototype = {
initialize: function() {
},
encryptText: function(text) {
var encr = new GlideEncrypter();
return encr.encrypt(text);
},
decryptText: function(text) {
var encr = new GlideEncrypter();
return encr.decrypt(text);
},
type: 'EncryptUtil2'
};
As an example, created 3 fields name "Clear text" (u_clear_text), "Encrypted String" (u_encrypted), "Decrypted String" (u_decrypted).
Created Client script to encrypt value of "Clear text" and set it in "Encrypted String".
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ajax = new GlideAjax('EncryptText');
ajax.addParam('sysparm_name', 'encryptText');
ajax.addParam('sysparm_text', newValue);
ajax.getXMLAnswer(function(answer) {
if (answer.length > 0) {
g_form.setValue('u_encrypted', answer);
}
});
}
Script Include named "EncryptText" that's client callable.
var EncryptText = Class.create();
EncryptText.prototype = Object.extendsObject(AbstractAjaxProcessor, {
encryptText: function() {
var text = this.getParameter('sysparm_text');
var encr = new GlideEncrypter();
return encr.encrypt(text);
},
decryptText: function() {
var text = this.getParameter('sysparm_text');
var encr = new GlideEncrypter();
return encr.decrypt(text);
},
type: 'EncryptText'
});
Business rule:
(function executeRule(current, previous /*null when async*/ ) {
try {
var encr = new global.EncryptUtil2();
var encrypted = current.u_encrypted;
var decrypted = encr.decryptText(encrypted);
current.u_decrypted = decrypted;
} catch (e) {
current.u_decrypted = e.message;
}
})(current, previous);
Execution:
Step 1: Enter value into Clear text. Encrypted String is set.
Step 2. Save form. Decrypted text is set in Decrypted String

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2022 01:12 AM
BTW, I've found that decrypting a long text in Business Rule is returning encrypted text instead of decrypted text. Try entering shorter text to test to see that works.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2022 03:12 AM
Thank you very much.
I was able to confirm the operation by following the procedure you taught me.
In conclusion, is it that the decryption could not be done well because ScriptInclude was not used?
As you pointed out, even if I tried to decrypt with a short text, it returned the text as it was encrypted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2022 11:58 PM
Yes. Should use Script Include to decrypt text from scoped application.