function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shubham Sinha 43Shubham Sinha 43 

Getting an error "void valueOf(Schema.SObjectField) from the type Blob".

I am trying to encrypt  an 'auto number'  custom field but i am getiing the errro "void valueOf(Schema.SObjectField) from the type Blob".
Public static string getToken(){
    
        String userIdKey = ('U' + UserInfo.getUserId()).left(16);
                    Blob salt = Blob.valueOf(userIdKey);
                    Blob key = Crypto.generateAesKey(128);
                    Blob data = Blob.valueOf(BIIB_Remote_Consent_Initiator__c.Name);// I am getting error here.
                    Blob encrypted = Crypto.encrypt('AES128', key, salt, data);
                    String encryptedItemCode = EncodingUtil.urlEncode(EncodingUtil.base64Encode(encrypted), 'UTF-8');
                    String encryptedPublicKey = EncodingUtil.urlEncode(EncodingUtil.base64Encode(key), 'UTF-8'); 

 

                    String reportid = EncodingUtil.urlEncode(encryptedItemCode + '+' + encryptedPublicKey, 'UTF-8');
                    system.debug('reportID ' +reportid);

                   reportid = EncodingUtil.urlDecode(reportid, 'UTF-8');
        
            String parameterValue = reportid;
            String encodedItemCode = parameterValue.split('\\+')[0];
            String encodedKey = parameterValue.split('\\+')[1];
                        
             userIdKey = ('U' + UserInfo.getUserId()).left(16);
             salt = Blob.valueOf(userIdKey);
             key = EncodingUtil.base64Decode(EncodingUtil.urlDecode(encodedKey, 'UTF-8'));
             encrypted = EncodingUtil.base64Decode(EncodingUtil.urlDecode(encodedItemCode, 'UTF-8'));
            Blob decrypted = Crypto.decrypt('AES128', key, salt, encrypted);
            String itemCode = decrypted.toString();

system.debug('itemcode ' +itemCode);
return reportid;
    
    }

 
VinayVinay (Salesforce Developers) 
Hi Shubham,

You need to do is to pass string value to the valueOf function Blob.valueOf('Sample Test');

String myString = 'StringToBlob'; Blob myBlob = Blob.valueof(myString);

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_blob.htm

Review below links which has examples.

https://www.mstsolutions.com/technical/encrypt-and-decrypt-data-using-apex-crypto-class/
https://salesforce.stackexchange.com/questions/108763/why-is-string-valueofblob-different-that-blob-tostring/108766#108766

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar