Tuesday 23 February 2016

Item Attachment Details - SQL and code

We might be faced with a situation when we have attachment text uploaded for an Item and we wish to see that information . In such a scenario we have the option of opening the item and see the attachment from EBS screen , or use the below SQL to fetch the attachment text for 1 or more items

select long_text,att1.media_id,msib.segment1,text1.rowid row_id,mp.organization_code
  from fnd_documents_long_text text1,
  FND_ATTACHED_DOCS_FORM_VL att1,
  mtl_system_items_b msib,
  mtl_parameters mp
  where text1.media_id = att1.media_id
  and att1.entity_name = 'MTL_SYSTEM_ITEMS'
  and att1.PK1_VALUE = msib.organization_id
  and att1.PK2_VALUE = msib.inventory_item_id
  and mp.organization_id = msib.organization_id
  --and att1.creation_date like sysdate ;

Long Text is the attachment description on the Item.

In another scenario the business knew what is the Item Attachment description and wants to know the Set of items where this description exists, Unfortunately the above SQL cannot be used in this case since the Attachment description is a long text column with datatype LONG  which cannot be used in a SQL WHERE clause , However the requirement can be met by using the script in anonymous block as below : Additional condition and checks can be added as need be.

/* The code will search across item description
     and find the item with the given description
     current code will check all the items where item has attachment description
     text as 'test' , and will display the item details
  */
declare

l_chr_desc varchar2(32000);
l_chr_desc_substr varchar2(100);
l_num_media_id number;

cursor c1 is
select long_text,att1.media_id,msib.segment1,text1.rowid row_id,mp.organization_code
  from fnd_documents_long_text text1,
  FND_ATTACHED_DOCS_FORM_VL att1,
  mtl_system_items_b msib,
  mtl_parameters mp
  where text1.media_id = att1.media_id
  and att1.entity_name = 'MTL_SYSTEM_ITEMS'
  and att1.PK1_VALUE = msib.organization_id
  and att1.PK2_VALUE = msib.inventory_item_id
  and mp.organization_id = msib.organization_id
  --and att1.creation_date like sysdate
  ;

begin

  for c1_rec in C1
  LOOP
     select long_text,media_id
       into l_chr_desc,l_num_media_id
       from apps.fnd_documents_long_text
       where rowid = c1_rec.row_id;

     l_chr_desc_substr := substr(l_chr_desc,1,25);

     If l_chr_desc_substr = 'test'
     then
        dbms_output.put_line('l_num_media_id '||l_num_media_id);
        dbms_output.put_line('Item  '||c1_rec.segment1);
        dbms_output.put_line('IWarehouse  '||c1_rec.organization_code);
     end if;

   
  end loop;
 
exception
when others
then
dbms_output.put_line('error  '||SQLERRM);
end;

Any feedback on Improvements is appreciated .