Create a document and attach it to a Customer Record using X++

Saturday, March 28, 2020

 

Document handling in Dynamics 365 for Finance and Operations is a feature that allows you to add notes, links, documents, images, files, and other related information to almost any record in the system. For example, we can track all the correspondence sent out to our customers by attaching the documents to their records in Dynamics 365 for Finance and Operations. Document handling on most of the forms can be accessed either from the Action pane by clicking on the Attachments button and selecting Document handling from the Command menu under File or selecting the Document handling icon from the status bar.Lets create an attachment using X++ and attach a note to a customer record. Lets move over to visual studio.

Step 1.

If you haven't created a project. Create a new Visual Studio Project, the type should be Unified Operations. In the new D365 Visual Studio project lets add a new class for managing documents. Select the project name in the solution and right Click.

Step 5 image

Step 2.In the dialog which pops up we have a number of choices to add to the project. In the left hand pane select Code. This will narrow our choices to code items. In the items remaining in the list select the typed a runnable class. Give it a Name

Step 6 image

I've given the class the name. RunnableClassCustomerDocument. In the code pane past the following code. 

Step 7 image

8 Let's go over to the Customer Listing pages in Dynamics 365. Lets select a customer in this case I'm selecting Account Number 004007 and Name Owen Tolley. In the Customer menu select the attachments option to see what documents exist

Step 8 image

9 In the attachment page , there are no attachments. So lets go back to visual studio and run our code

Step 9 image

10 Don't forget to copy the customer number for our code 004007 

Step 10 image

11 In the code page lets paste 004007  as the customer we are going to add the attachment for. In the code we are going to search for the customer 004007. We then return the customer object we are going to attach the document for. Next we search for the

Step 11 image

12 document Type "Note". The next lines we add fields like Name of the attachment and the notes. We also set the type of the document we are adding. Before we run the code, right click the new class name and set to StartUp Object 

Step 12 image

13 Click Start Without Debugging. i tend to use this option for speed purposes. If you want to trace the code line by line choose the start debugging option

Step 13 image

14 Once the code has been compiled and built the following window is display. Review the message at the top of the screen is matches the message box from our code. Document note has been added successfully. Lets review our customer record.

Step 14 image

15 Navigate to the customers list in Dynamics. Find the customer we reviewed earlier in the article and click the attachments menu option as we did previously.

Step 15 image

16 In the attachment screen we now see a Note entry. We can also see that the note comments and when the note was added.  

Step 16 image

17 This is really useful piece of functionality. There are lots of scenario's where you will need to store documents against records in Dynamics . Hope it was useful. Thats the ends of the tutorial. 

Step 17 image

X++ Code

class RunnableClassCustomerDocument
{
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
public static void main(Args _args)
{
CustTable custTable;
DocuType docuType;
DocuRef docuRef;
custTable = CustTable::find('004007');
docuType = DocuType::find('Note');
if (!docuType || docuType.TypeGroup != DocuTypeGroup::Note)
{
throw error("Invalid document type");
}
docuRef.RefCompanyId = custTable.dataAreaId;
docuRef.RefTableId = custTable.TableId;
docuRef.RefRecId = custTable.RecId;
docuRef.TypeId = docuType.TypeId;
docuRef.Name = 'Automatic note';
docuRef.Notes = 'Added from X++';
docuRef.insert();
info("Document note has been added successfully");
}

}