Easy drag and drop – files [Document Management] with minimum X++ Coding [Dynamics AX]

Friends, Normally we attach a document using the Document handling icon on the tool bar and select appropriate options in attaching a file.

Well, I was going through smmDocuments form and found a drag drop ActiveX control which can make our life easier in quickly dragging and dropping the files [Yes, we need to write some code to add documents ,but its simple].

Please Note: I am assuming that you have setup document Management from Basic > Setup >> Document Management.

Lets see how we can do this to achieve the Drag-drop feature of files to attach documents directly to your document Management file system.
I am going to customize the custTable form as an example

Step 1: Go to AOT >> Forms >> CustTable
Step 2: Go to the designs node, Right click >. New Control >> ActiveX Control and select smmDrop2.smmDropWin activeX as shown below [Please note: If you are unable to see this class Id, it means you don’t have enough license keys ]

Once you drag drop the activeX control, Open the custTable form and you should see the control at the bottm left as shown below.

Step 3: Expand the activeX control and adda method onevent_DropFile event method
Step 4: Copy paste the below code to hit two tables DocuRef and docuValue to store the paths and the filename.

// AOSRunMode::Client
void onEvent_DropFile(COMVariant _strFilePath)
{
DocuRef tdocuRef;
DocuValue tdocuValue;
NumberSeq numSeq;
FileName _fileName;
FileName draggedFile = _strFilePath.bStr();
Num newFileNum;
DocuType docuType;
FileName fileName;
;

tdocuRef.RefRecId = custTable.RecId;
tdocuRef.RefTableId = custTable.TableId;
tdocuref.RefCompanyId = curext();

select docuType where docuType.ActionClassId == classnum(DocuActionArchive) &&
docuType.TypeId == ‘file’;

// Get next number from number sequence
numSeq = NumberSeq::newGetNum(DocuParameters::numRefDocuNumber(), true);
ttsbegin;
newFileNum = numSeq.num();
[tDocuValue.FileName, tDocuValue.FileType, tDocuValue.Path] = Docu::splitFilename(draggedFile);
_fileName = DocuParameters::find().ArchivePath;

if (!Docu::validateExtension(tDocuValue.FileType))
{
// Files with this file type cannot be attached.
throw error(“@SYS99219″);
}
filename = _fileName + newFileNum + (tDocuValue.FileType ? (‘.’ + tDocuValue.fileType) : ”);

// If file already exists , use next next number
while(WinAPI::fileExists(filename))
{
numSeq.used();
numSeq = NumberSeq::newGetNum(DocuParameters::numRefDocuNumber(), true);
newFileNum = numSeq.num();

filename = _fileName + newFileNum + (tDocuValue.fileType ? (‘.’ + tDocuValue.FileType) : ”);
}
tDocuValue.fileName = newFileNum;
tDocuValue.insert();

tDocuRef.ValueRecId = tdocuValue.RecId;
tDocuRef.TypeId = docuType.TypeId;
tDocuRef.write();

// Create the file reference
tdocuValue.insert();

if (numSeq)
{
// Mark the document numbersequence number as used
numSeq.used();
}

winapi::copyFile(draggedFile, fileName);
ttscommit;
info(strfmt(‘Document %1 attached successfully’, draggedFile));

}
If there is any other easy way of implementing the same above code, you can do that.
Step 5: There you go. We are done with the coding part. Compile the form. Now try dragging and dropping the files on the icon shown in the custTable form. [Below is the the reference screen shot]

You can even drag drop the files even if your form is inactive and is minimized[exactly how we drag and drop feature of outlook] as shown below

If you want to use this feature on any other form repeat the same steps mentioned above but only change 2 lines of code

tdocuRef.RefRecId = custTable.RecId; // Change the active record – vendTable.recId
tdocuRef.RefTableId = custTable.TableId; // Change the active recid – vendTable.TableId

If you are already aware of this feature. Please ignore.

Thanks!! Sreenath Reddy.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: