I am working on simple encryption using the crypto class to encrypt some data fields in salesforce. The encryption class works fine for text data with max size 255. however i need to reduce the encrypted data size once encrypted.

This is sample encryption login i am using to encrypt the data.

for(Account a : scope)
Blob stringencry =Blob.valueOf(a.phone);
Blob encrypted = Crypto.encryptWithManagedIV('AES256', seckey, stringencry);       
String b64=EncodingUtil.base64Encode(encrypted);

The above logic works fine for text data type variable however the same could not be implemented for other data types like phone,url or any others .While saving the records i am receiving the "STRING_TOO_LONG" error from salesforce.

So post encryption there has to be a way in restricting the encrypted size of the data.Any light on this is great help to me.

1 Answer 1


Some of the standard Salesforce fields like phone, URL, email etc. have enforced character limit which is set by the platform. Phone I believe is 40 characters. Your output (encrypted and encoded) is most likely to be greater than 40 characters hence it won't fit in that field.

  • yes, thats the exact issue i am facing. encryption works only if the field size if 255 and mostly the encrypted data is less than 255 characters. there got to be a way in reducing the data size before or after encryption while saving.
    – chops
    May 5, 2015 at 7:09
  • The data might be only 1 character, though as soon as you encrypt it, it generates a 40-64 characters hash (depending which algorithm you use). Once you base64 encode that hash, it becomes even longer. That being said, there is no way you can store the encrypted/encoded data into a field that has lower limit of characters than the actual result. You might as well create a custom field in order to store the hashed value. May 5, 2015 at 10:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .