- 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
‎03-31-2022 11:28 PM
Hi
to be honest, your code makes no sense, as I can only see that you decrypt the value at field "u_password_string" and then set that decrypted value again on the same field.
But in case your field "u_password_string" is a password2 field, you have to set the decrypted value via
current.setDisplayValue('u_password_string', decrString);
Kind regards
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2022 11:46 PM
Thanks for the reply.
I don't quite understand, "decrString = encr.decrypt(encrString);" You mean you don't need to do anything after this?
By the way, if I write "current.setDisplayValue('u_password_string', decrString);", it shows "undefined" and without description, it shows "I+tYsVN4xvz5915A7/EDfg==".
Is decryption not working?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2022 12:52 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2022 01:04 AM
Thank you very much.
I am aware that GlideEncrypter is only available in Global scope.
That is why we are using Global in the verification we are doing now.
Encryption with GlideEncrypter works fine.
However, only the decryption does not work as described above.
Is there something I am missing?