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
merthenmerthen 

decode base64 in apex class

I am trying to pull the body of a "Document" into my apex class from which I would like to extract name/value pairs (like reading a properties file).
I find that the body is base64 encoded.
How can I decode that in my apex class?
Best Answer chosen by Admin (Salesforce Developers) 
merthenmerthen

Works for me!

Thanks!

I actually didn't even have to use the "EncodingUtil" class...just did a blob.toString()

Here is my now working code.  Its not very elegant but it works! (poor man's properties file)

Document doc = [Select d.Body From Document d where d.Name='Solution Areas Mappings'];

Blob bodyBlob = doc.Body;

String bodyStr = bodyBlob.toString();

List<String> parts= new List<String>();

parts = bodyStr.split('\n',-2);

System.debug('---------'+parts[0].substring(parts[0].indexOf('=')+1,parts[0].length())+'-------');

Thanks so much

All Answers

JimRaeJimRae
Use the EncodingUtil class to convert your Base64 encoded string in to a blob object, then you can use the Blob toString method to get the value pairs out of the decoded object.  I also used the string split method to break my value pairs out into "records" for processing.
merthenmerthen

Works for me!

Thanks!

I actually didn't even have to use the "EncodingUtil" class...just did a blob.toString()

Here is my now working code.  Its not very elegant but it works! (poor man's properties file)

Document doc = [Select d.Body From Document d where d.Name='Solution Areas Mappings'];

Blob bodyBlob = doc.Body;

String bodyStr = bodyBlob.toString();

List<String> parts= new List<String>();

parts = bodyStr.split('\n',-2);

System.debug('---------'+parts[0].substring(parts[0].indexOf('=')+1,parts[0].length())+'-------');

Thanks so much
This was selected as the best answer
RobertoDRobertoD

Hi,

 

Although this is old maybe you can give me an answer:

 

I'm trying to get some attachments are base64 with apex I always jump when opening decryption errors or open encrypted.

My code is:

 

CREATE OR REPLACE /* Formatted on 2012/09/18 10:10 (Formatter Plus v4.8.8) */
FUNCTION SYSTEM.blob_to_blob_base64 (p_data IN BLOB)
   RETURN BLOB
IS
   l_bufsize   INTEGER     := 16386;
   l_buffer    RAW (16386);
   l_offset    INTEGER     DEFAULT 1;
   l_result    BLOB;
BEGIN
   DBMS_LOB.createtemporary (l_result, FALSE, DBMS_LOB.CALL);

   LOOP
      BEGIN
         DBMS_LOB.READ (p_data, l_bufsize, l_offset, l_buffer);
      EXCEPTION
         WHEN NO_DATA_FOUND
         THEN
            EXIT;
      END;

      l_offset := l_offset + l_bufsize;
      DBMS_LOB.append
         (l_result,
          to_blob
             (UTL_RAW.cast_to_raw
                 (UTL_RAW.cast_to_varchar2 (UTL_ENCODE.base64_encode (l_buffer)
                                           )
                 )
             )
         );
   END LOOP;

   RETURN l_result;
END;
/


CREATE OR REPLACE /* Formatted on 2012/09/18 10:10 (Formatter Plus v4.8.8) */
FUNCTION SYSTEM.blob_to_clob_base64 (p_data IN BLOB)
   RETURN CLOB
IS
   l_bufsize   INTEGER     := 16386;
   l_buffer    RAW (16386);
   l_offset    INTEGER     DEFAULT 1;
   l_result    CLOB;
BEGIN
   DBMS_LOB.createtemporary (l_result, FALSE, DBMS_LOB.CALL);

   LOOP
      BEGIN
         DBMS_LOB.READ (p_data, l_bufsize, l_offset, l_buffer);
      EXCEPTION
         WHEN NO_DATA_FOUND
         THEN
            EXIT;
      END;

      l_offset := l_offset + l_bufsize;
      DBMS_LOB.append
         (l_result,
          TO_CLOB
                 (UTL_RAW.cast_to_varchar2 (UTL_ENCODE.base64_encode (l_buffer)
                                           )
                 )
         );
   END LOOP;

   RETURN l_result;
END;
/


GRANT EXECUTE ON SYSTEM.BLOB_TO_BLOB_BASE64 TO GESORA;

GRANT EXECUTE ON SYSTEM.BLOB_TO_CLOB_BASE64 TO GESORA;

 

Thanks