What is attachment in oracle application?
The attachments feature in oracle application enables users to link unstructured data, such as images, word-processing documents, spreadsheets, or text to their application data. For example, users can link images to items or video to operations as operation instructions.
Where to find an attachment?
There is an attachment icon in the oracle application toolbar that indicates whether the Attachments feature is enabled in a form block. When the button is dimmed, the Attachment feature is not available. When the Attachment feature is enabled in a form block, the icon becomes a solid paper clip. The icon switches to a paper clip holding a paper when the Attachment feature is enabled in a form lock and the current record has at least one attachment.
Attachment types:
An attached document can be:
1] Short Text
Text stored in the database containing less than 2000 characters.
2] Long Text
Text stored in the database containing 2000 characters or more.
3] Image
An image that Oracle Forms can display, including: bmp, cals, jfif, jpeg, gif, pcd, pcx, pict, ras, and tif.
4] OLE Object
An OLE Object that requires other OLE server applications to view, such as Microsoft Word or Microsoft Excel.
5] Web Page
A URL reference to a web page which you can view with your web browser.
Tables Involved:
For Importing Attachments in oracle application one has to populate following tables.
1. FND_DOCUMENTS
2. FND_ATTACHED_DOCUMENTS
3. FND_DOCUMENTS_TL
4. FND_DOCUMENT_DATATYPES.
5. FND_DOCUMENT_CATEGORIES
6. FND_DOCUMENTS_LONG_TEXT (Long text type attachment).
7. FND_DOCUMENTS_SHORT_TEXT (Short text type attachment).
8. FND_DOCUMENTS_LONG_RAW
9. FND_LOBS (File type attachments).
FND_DOCUMENTS:
FND_DOCUMENTS stores language-independent information about a document. For example, each row contains a document identifier, a category identifier, the method of security used for the document (SECURITY_TYPE, where 1=Organization,2=Set of Books, 3=Business unit,4=None), the period in which the document is active, and a flag to indicate whether or not the document can be shared outside of the security type (PUBLISH_FLAG).
Other specifications in this table include: datatype (DATATYPE_ID, where 1=short text,2=long text, 3=image, 4=OLE object), image type, and storage type (STORAGE_TYPE, where 1=stored in the database, 2=stored in the file system).
The document can be referenced by many application entities and changed only in the define document form (USAGE_TYPE=S); it can be used as a fill-in-the-blanks document, where each time you use a template, you make a copy of it (USAGE_TYPE=T); or it can be used only one time (USAGE_TYPE=O).Images and OLE Objects cannot be used as templates.
Queries:
1] To find all Long Text attachments:
SELECT
FAD.SEQ_NUM "Seq Number",
FDAT.USER_NAME "Data Type",
FDCT.USER_NAME "Category User Name",
FAD.ATTACHED_DOCUMENT_ID "Attached Document Id",
FDET.USER_ENTITY_NAME "User Entity",
FD.DOCUMENT_ID "Document Id",
FAD.ENTITY_NAME "Entity Name",
FD.MEDIA_ID "Media Id",
FD.URL "Url",
FDT.TITLE "Title",
FDLT.LONG_TEXT "Attachment Text"
FROM
FND_DOCUMENT_DATATYPES FDAT,
FND_DOCUMENT_ENTITIES_TL FDET,
FND_DOCUMENTS_TL FDT,
FND_DOCUMENTS FD,
FND_DOCUMENT_CATEGORIES_TL FDCT,
FND_ATTACHED_DOCUMENTS FAD,
FND_DOCUMENTS_LONG_TEXT FDLT
WHERE
FD.DOCUMENT_ID = FAD.DOCUMENT_ID
AND FDT.DOCUMENT_ID = FD.DOCUMENT_ID
AND FDCT.CATEGORY_ID = FD.CATEGORY_ID
AND FD.DATATYPE_ID = FDAT.DATATYPE_ID
AND FAD.ENTITY_NAME = FDET.DATA_OBJECT_CODE
AND FDLT.MEDIA_ID = FD.MEDIA_ID
AND FDAT.NAME = 'LONG_TEXT';
2] To find all Short Text attachments:
SELECT
FAD.SEQ_NUM "Seq Number",
FDAT.USER_NAME "Data Type",
FDCT.USER_NAME "Category User Name",
FAD.ATTACHED_DOCUMENT_ID "Attached Document Id",
FDET.USER_ENTITY_NAME "User Entity",
FD.DOCUMENT_ID "Document Id",
FAD.ENTITY_NAME "Entity Name",
FD.MEDIA_ID "Media Id",
FD.URL "Url",
FDT.TITLE "Title",
FDST.SHORT_TEXT "Attachment Text"
FROM
FND_DOCUMENT_DATATYPES FDAT,
FND_DOCUMENT_ENTITIES_TL FDET,
FND_DOCUMENTS_TL FDT,
FND_DOCUMENTS FD,
FND_DOCUMENT_CATEGORIES_TL FDCT,
FND_ATTACHED_DOCUMENTS FAD,
FND_DOCUMENTS_SHORT_TEXT FDST
WHERE
FD.DOCUMENT_ID = FAD.DOCUMENT_ID
AND FDT.DOCUMENT_ID = FD.DOCUMENT_ID
AND FDCT.CATEGORY_ID = FD.CATEGORY_ID
AND FD.DATATYPE_ID = FDAT.DATATYPE_ID
AND FAD.ENTITY_NAME = FDET.DATA_OBJECT_CODE
AND FDST.MEDIA_ID = FD.MEDIA_ID
AND FDAT.NAME = 'SHORT_TEXT';
---------------------------------------------------------------------------------------------------------------------------------------------
The following SQL gives the details of all attachments on PO headers. To get only attachments for a particular PO, add a line in the where clause, AND fad.pk1_value = <<PO_HEADER_ID>>
-- Get the attachment data from database.
SELECT fdl.description, fl.file_id, fdl.file_name, fad.pk1_value "Header ID", fl.upload_date "File added on", fl.file_format, fu.description created_by
FROM fnd_lobs fl, fnd_attached_documents fad, fnd_documents_tl fdl, fnd_user fu
WHERE 1 = 1 AND fdl.document_id = fad.document_id AND fdl.media_id = fl.file_id AND fad.entity_name = 'PO_HEADERS'
-- AND fdl.language=l_language
-- AND fl.file_name = l_filename
AND fu.user_id = fdl.created_by
-----------------------------------------------------------------------------------------------------------------------------------------------
Attachment upload through API:
Attachments can also be uploaded through an oracle provided API called FND_ATTACHED_DOCUMENTS_PKG.
It consist of three procedures
1) Insert Row
2) Update Row
3) Lock Row
Names of these procedures are self explanatory. insert row is used to insert a new row for attachment data, update row is used to update existing row for a particular row and Lock Row is used to lock a existing row for further modification.
Attachments API - FND_DOCUMENTS_PKG, FND_ATTACHED_DOCUMENTS_PKG
Attachments API - FND_DOCUMENTS_PKG, FND_ATTACHED_DOCUMENTS_PKG
Working on attachments need some knowledge of how an attachment functions (Definitions, Entity, Document Category, Entities and Blocks, Primary Keys in attachments (If exists)) in Oracle Apps. To understand the below API and what it actually does its advisible to read the Attachments Chapter in Developers Guide.
Primary Key information that uniquely identifies the product (such as the product_ID).
Below API takes single file from a shared drive and inserts into fnd_lobs. Then its been attached to a particular Id (like order_id, invoice_id..) using the combination of pk1_value, entity name and category. In this example i have used PDF as attachment. Quering the table fnd_document_datatypes will give different attachment types.
This sample shows how an attachment API works. You can use it to customize/enhance to requirement.
DECLARE
l_rowid ROWID;
l_attached_document_id NUMBER;
l_document_id NUMBER;
l_media_id NUMBER;
l_category_id NUMBER;
l_pk1_value fnd_attached_documents.pk1_value%TYPE:=<Primary Key information that uniquely identifies the product (such as the product_ID)>;
l_description fnd_documents_tl.description%TYPE:='Test Attachment';
l_filename VARCHAR2(240) := '<File Name>';
l_seq_num NUMBER;
l_blob_data BLOB;
l_blob BLOB;
l_bfile BFILE;
l_byte NUMBER;
l_fnd_user_id NUMBER;
l_short_datatype_id NUMBER;
x_blob BLOB;
fils BFILE;
blob_length INTEGER;
l_entity_name VARCHAR2(100) := <entity_name>;
l_category_name VARCHAR2(100) := <category_name>;
BEGIN
fnd_global.apps_initialize (<userid>, <applid>,<appluserid>);
SELECT fnd_documents_s.NEXTVAL
INTO l_document_id
FROM DUAL;
SELECT fnd_attached_documents_s.NEXTVAL
INTO l_attached_document_id
FROM DUAL;
SELECT NVL (MAX (seq_num), 0) + 10
INTO l_seq_num
FROM fnd_attached_documents
WHERE pk1_value = l_pk1_value AND entity_name = l_entity_name;
-- Select User_id
SELECT user_id
INTO l_fnd_user_id
FROM apps.fnd_user
WHERE user_name = <user_name>;
-- Get Data type id for Short Text types of attachments
SELECT datatype_id
INTO l_short_datatype_id
FROM apps.fnd_document_datatypes
WHERE NAME = 'FILE';
-- Select Category id for Attachments
SELECT category_id
INTO l_category_id
FROM apps.fnd_document_categories_vl
WHERE USER_NAME = l_category_name;
-- Select nexvalues of document id, attached document id and
-- l_media_id
SELECT apps.fnd_documents_s.NEXTVAL,
apps.fnd_attached_documents_s.NEXTVAL
--apps.fnd_documents_long_text_s.NEXTVAL
INTO l_document_id,
l_attached_document_id
--l_media_id
FROM DUAL;
SELECT MAX (file_id) + 1
INTO l_media_id
FROM fnd_lobs;
fils := BFILENAME (<FLIE PATH>, l_filename);
-- Obtain the size of the blob file
DBMS_LOB.fileopen (fils, DBMS_LOB.file_readonly);
blob_length := DBMS_LOB.getlength (fils);
DBMS_LOB.fileclose (fils);
-- Insert a new record into the table containing the
-- filename you have specified and a LOB LOCATOR.
-- Return the LOB LOCATOR and assign it to x_blob.
INSERT INTO fnd_lobs
(file_id, file_name, file_content_type, upload_date,
expiration_date, program_name, program_tag, file_data,
LANGUAGE, oracle_charset, file_format
)
VALUES (l_media_id, l_filename, 'application/pdf',--'text/plain',--application/pdf
SYSDATE,
NULL, 'FNDATTCH', NULL, EMPTY_BLOB (), --l_blob_data,
'US', 'UTF8', 'binary'
)
RETURNING file_data
INTO x_blob;
-- Load the file into the database as a BLOB
DBMS_LOB.OPEN (fils, DBMS_LOB.lob_readonly);
DBMS_LOB.OPEN (x_blob, DBMS_LOB.lob_readwrite);
DBMS_LOB.loadfromfile (x_blob, fils, blob_length);
-- Close handles to blob and file
DBMS_LOB.CLOSE (x_blob);
DBMS_LOB.CLOSE (fils);
DBMS_OUTPUT.put_line ('FND_LOBS File Id Created is ' || l_media_id);
COMMIT;
-- This package allows user to share file across multiple orgs or restrict to single org
fnd_documents_pkg.insert_row
(x_rowid => l_rowid,
x_document_id => l_document_id,
x_creation_date => SYSDATE,
x_created_by => l_fnd_user_id,-- fnd_profile.value('USER_ID')
x_last_update_date => SYSDATE,
x_last_updated_by => l_fnd_user_id,-- fnd_profile.value('USER_ID')
x_last_update_login => fnd_profile.VALUE('LOGIN_ID'),
x_datatype_id => l_short_datatype_id, -- FILE
X_security_id => <security ID defined in your Attchments, Usaully SOB ID/ORG_ID..>,
x_publish_flag => 'N', --This flag allow the file to share across multiple organization
x_category_id => l_category_id,
x_security_type => 1,
x_usage_type => 'S',
x_language => 'US',
x_description => l_filename,--l_description,
x_file_name => l_filename,
x_media_id => l_media_id
);
commit;
fnd_documents_pkg.insert_tl_row
(x_document_id => l_document_id,
x_creation_date => SYSDATE,
x_created_by => l_fnd_user_id,--fnd_profile.VALUE('USER_ID'),
x_last_update_date => SYSDATE,
x_last_updated_by => l_fnd_user_id,--fnd_profile.VALUE('USER_ID'),
x_last_update_login => fnd_profile.VALUE('LOGIN_ID'),
x_language => 'US',
x_description => l_filename--l_description
);
COMMIT;
fnd_attached_documents_pkg.insert_row
(x_rowid => l_rowid,
x_attached_document_id => l_attached_document_id,
x_document_id => l_document_id,
x_creation_date => SYSDATE,
x_created_by => l_fnd_user_id,--fnd_profile.VALUE('USER_ID'),
x_last_update_date => SYSDATE,
x_last_updated_by => l_fnd_user_id,--fnd_profile.VALUE('USER_ID'),
x_last_update_login => fnd_profile.VALUE('LOGIN_ID'),
x_seq_num => l_seq_num,
x_entity_name => l_entity_name,
x_column1 => NULL,
x_pk1_value => l_pk1_value,
x_pk2_value => NULL,
x_pk3_value => NULL,
x_pk4_value => NULL,
x_pk5_value => NULL,
x_automatically_added_flag => 'N',
x_datatype_id => 6,
x_category_id => l_category_id,
x_security_type => 1,
X_security_id => <security ID defined in your Attchments, Usaully SOB ID/ORG_ID..>,
x_publish_flag => 'Y',
x_language => 'US',
x_description => l_filename,--l_description,
x_file_name => l_filename,
x_media_id => l_media_id
);
COMMIT;
DBMS_OUTPUT.put_line ('MEDIA ID CREATED IS ' || l_media_id);
END;
/
FND_ATTACHED_DOCUMENTS:
FND_ATTACHED_DOCUMENTS stores information relating a document to an application entity. For example, a record may link a document to a sales order or an item. Each row contains foreign keys to FND_DOCUMENTS and FND_DOCUMENT_ENTITIES. There is also a flag to indicate whether or not an attachment was created automatically.
FND_DOCUMENTS_TL:
FND_DOCUMENTS_TL stores translated information about the documents in FND_DOCUMENTS. Each row includes the document identifier, the language the row is translated to, the description of the document, the file in which the image is stored, and an identifier (MEDIA_ID) of the sub-table in which the document is saved (FND_DOCUMENTS_SHORT_TEXT, FND_DOCUMENTS_LONG_TEXT, or FND_DOCUMENTS_LONG_RAW).
FND_DOCUMENT_DATATYPES:
FND_DOCUMENT_DATATYPES stores the document datatypes that are supported. Initial values are: short text, long text, image, and OLE Object (DATATYPE_ID=1, 2, 3, or 4). Customers can add datatypes to handle documents stored outside of Oracle and use non-native Forms applications to view/edit their documents. The table uses a “duplicate record” model for handling multi-lingual needs. That is, for each category there will be one record with the same CATEGORY_ID and CATEGORY_NAME for each language.
FND_DOCUMENT_CATEGORIES:
FND_DOCUMENT_CATEGORIES stores information about the categories in which documents are classified. For example, documents may be considered “Bill of Material Comments”, “WIP Job Comments”, etc. Document categories are used to provide a measure of security on documents. Each form that enables the attachment feature lists which categories of documents can be viewed in the form. This table uses a “duplicate record” model for handling multi-lingual needs.
FND_DOCUMENTS_LONG_TEXT:
FND_DOCUMENTS_LONG_TEXT stores information about long text documents.
FND_DOCUMENTS_SHORT_TEXT:
FND_DOCUMENTS_SHORT_TEXT stores information about short text documents.
FND_DOCUMENTS_LONG_RAW:
FND_DOCUMENTS_LONG_RAW stores images and OLE Objects, such as Word Documents and Excel spreadsheets, in the database.
FND_DOCUMENT_ENTITIES:
FND_DOCUMENT_ENTITIES lists each entity to which attachments can be linked. For example, attachments can be linked to Items, Sales Orders, etc. Since the table uses a “duplicate record” model for handling multi-lingual needs, for each document entity there will be one record with the same DOCUMENT_ENTITY_ID and DATA_OBJECT_CODE for each language.
------------------------------------------------------------------------------------------------------------
To get long text of attachment at customer level
-------------------------------------
SELECT FDLT.LONG_TEXT into v_useritemtext
FROM FND_DOCUMENTS_TL FDL,
FND_DOCUMENTS_LONG_TEXT FDLT,
fnd_attached_documents fad
WHERE FDL.MEDIA_ID=FDLT.MEDIA_ID
and fad.document_id=fdl.document_id
and fad.entity_name = 'AR_CUSTOMERS'
AND LANGUAGE ='US'
AND upper(FDL.DESCRIPTION) =upper('XXDESC')
and fad.pk1_value =:BILL_TO_CUSTOMER_ID;