Oracle Apex - Populating a rich text editor item from the DB

 Requirement

You want to populate a rich text item from a value (Clob) stored in the DB.

Settings defaults and source doesn't do anything, this worked for me.

Solution

Assume my item is 

Add this application process. Mine is called FETCH_SIGNATURE and its processing point is Ajax Callback. Replace the SQL select at the beginning with your table/column/parameter.

DECLARE
  l_clob        CLOB;
  l_file        BLOB;
  l_dest_offset PLS_INTEGER := 1;
  l_src_offset  PLS_INTEGER := 1;
  l_lang_ctx    PLS_INTEGER := DBMS_LOB.DEFAULT_LANG_CTX;
  l_csid        PLS_INTEGER := NLS_CHARSET_ID('UTF8');
  l_blob_warn   PLS_INTEGER;
BEGIN
  BEGIN
    -- Get the CLOB
    SELECT NVL(clob_field, EMPTY_CLOB())
    INTO l_clob
    from table
    where id = :F_EMPLOYEE_ID;
  -- Exception handling when no data were found for the ID (you are creating new row
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      l_clob := EMPTY_CLOB();
      
  END;
  -- If we don't have a CLOB, just return empty content
  IF (DBMS_LOB.GETLENGTH(l_clob) = 0) THEN
    HTP.P('');
  ELSE
    -- Create BLOB from CLOB
    DBMS_LOB.CREATETEMPORARY(  lob_loc => l_file
                             , cache => true
                             , dur => DBMS_LOB.CALL);
    DBMS_LOB.CONVERTTOBLOB(l_file, l_clob, dbms_lob.lobmaxsize, l_dest_offset, l_src_offset, l_csid, l_lang_ctx, l_blob_warn);
    -- Download BLOB
    OWA_UTIL.MIME_HEADER('text/html', false);
    HTP.P('Content-Length: ' || dbms_lob.getlength(l_file));
    HTP.P('Content-Disposition: attachment; filename="content.html"');
    OWA_UTIL.HTTP_HEADER_CLOSE();
    WPG_DOCLOAD.DOWNLOAD_FILE(l_file);
  END IF;
END;

Now create a dynamic action on page load which executes this Javascript.

CKEDITOR.on("instanceReady", function(event) {
  // Run the Ajax Callback process
  var p = apex.server.process('FETCH_SIGNATURE', {
    pageItems:['F_EMPLOYEE_ID']
  }, {
    dataType: 'html'
  });
  // When the process is done, set the value to the page item
  p.done(function(data) {
    apex.item('P5_BODY_RICH').setValue(data);
  });
});

With this, I am able to default a large HTML value (with embedded image) into the editor.

Acknowledgement

CLOB in APEX Rich Text Editor – APEX Corner (stinolez.com)


Comments