A question came up on the GPUG forums (Bringing Attachments from Legacy System into GP) asking about attaching files programmatically in GP. My GP Batch Attach works solely with the PM module as my previous employer’s needs really only surrounding around attaching vendor invoices as that was the bulk of documents they had.
In this post I will walk through my code and show how to easily expand it to any window in GP.
The document attach process in GP is fairly simple and is as follows:
- Create a note (paperclip icon) on the document window you wish to attach a file
- Attach the document in GP referencing that note’s index
When utilizing the attach function in GP, you need only 3 things:
- Note number/index (Including path. See below)
- File path for upload
- File name
Once you have that, all you have to call is the following function:
Microsoft.Dexterity.Applications.Dynamics.Forms.CoAttachManagement.Functions.CreateAttachment.Invoke(noteNumber, filePath, fileName, false, 2)
I’ll be honest, I do not know what the last two parameters are for. I ran the debug logger while performing the attach feature in GP and noticed the parameters provided were always false and 2 so I have always left it as this. I have not been able to find any documentation as to what these two parameters do.
You can tell from this that is it extremely easy to call the attach function from c#. The most “complicated” part of automating this is navigating to the correct window that you wish to attach the file.
Code to Attach Document to Payables Invoice/Document
The following works whether a note has already been created for the document or not.
Link to code snippet: GP Batch Attach Code Snippet
1. Navigate to the PM Transaction Window for an existing document
Microsoft.Dexterity.Applications.Dynamics.Procedures.PmZoomToEntryForm.Invoke(docType, voucherNum, trxSource, tableFlag, “”, 2, 7814);
docType <short> = Payables Document Type (i.e. 1 for invoice, 5 for credit memo, 6 for payment)
voucherNum<string> = Voucher Number of the document
trxSource<string> = Transaction Source for the document
tableFlag<short> = 3 for history table, 2 for open table, 1 for temp/work table where the document exists
Similar to the CreateAttachment procedure, I could not find sufficient documentation on the last 3 parameters. Again, I ran GP with the debug logger on and made note of the parameters provided and they were always blank, 2 and 7814.
2. Open the note window (same as clicking the paperclip icon on the window)
3. Open the document attach window (same as clicking the Attach button)
4.Once the document attach window has been opened, we can retrieve the note number and note origin/location within the GP file structure:
string localNumber = Microsoft.Dexterity.Applications.Dynamics.Forms.CoAttachManagement.CoAttachManagement.LocalNumber.Value;
string localOrigin = Microsoft.Dexterity.Applications.Dynamics.Forms.CoAttachManagement.CoAttachManagement.LocalOrigin.Value;
string noteNumber = @”0\System\” + localOrigin + @”\” + localNumber;
5. Finally, issue the procedure to attach the file (from above):
Microsoft.Dexterity.Applications.Dynamics.Forms.CoAttachManagement.Functions.CreateAttachment.Invoke(noteNumber, filePath, fileName, false, 2);
6. Lastly close any tables or forms you opened:
People may notice, my GPBatchAttach only requires a vendor ID and document number to identify the unique document however the PM Transaction Zoom procedure requires the transaction source number and document type as well. I built in additional queries to fetch this information using the vendor ID and document number within my code. This in itself is also tricky and I will keep it out of this post as it is kind of an aside as you can technically fetch this information numerous other ways.
Disclaimer: I am very much an amateur programmer. I have never taken an official Microsoft course. I cannot guarantee any code/program that I provide as I only have the ability to test my work on a small sample set. Users accept all the risk of using anything I provide and are encouraged to thoroughly test in a dedicated test environment before moving to production. Files I provide are free for use and are not to be resold. By using my files you agree to not hold me liable for any damages caused by said files.