Tuesday, June 5, 2012

X++ code for document attachement in AX 2009

In Dynamics AX 2009, we had a requirement to attach document with Purchase order table using x++ code while performing few options with PO.

For that purpose, we have to use following x++ AOT objects
  • DocuRef table
  • DocuActionArchive class
I have written following generic method that will attach record to any table in AX based on parameters passed to it

void attachDoc(RefTableId _refTableId, RefRecId _refRecId, selectableDataArea _refCompanyId, FileName _name)
{
    DocuRef docuRef;
    DocuActionArchive archive;
    ;


    docuRef.clear();
    docuRef.RefRecId = _refRecId;
    docuRef.RefTableId = _refTableId;
    docuRef.RefCompanyId = _refCompanyId;
    docuRef.Name = _name;
    docuRef.TypeId = 'File';


    docuRef.insert();


    archive = new DocuActionArchive();
    archive.add(docuRef, _name);
}

To call this method simply write following line and your document will be attached.

this.attachDoc(tableNum(PurchTable), purchTable.RecId, purchTable.dataAreaId, filepathname);

2 comments:

  1. this code get error because not declaration docuvalue
    this code work fine
    //attached file
    docuvalue docuvalue;
    recid docuvaluerecid;
    DocuRef docuRef;
    DocuActionArchive archive;
    ttsbegin;
    docuRef.clear();
    docuRef.RefRecId = _refRecId;
    docuRef.RefTableId = _refTableId;
    docuRef.RefCompanyId = _refCompanyId;
    docuRef.Name = _name;
    docuRef.TypeId = 'File';
    docuRef.insert();
    docuvalue.insert(); docuref.ValueRecId = docuvalue.RecId;
    docuref.update();
    docuvaluerecid = docuvalue.RecId;
    select forupdate docuvalue where docuvalue.recid == docuvaluerecid;
    docuvalue = docuvalue::writeDocuValue(docuref,_name+".xlsx");
    ttscommit;

    ReplyDelete