In this blog we will be highlighting the steps to send sales invoices from Dynamics CRM to Dynamics 365 for Financials.
Use Plugin in CRM
Here are the steps:
- Download the CRM SDK from Microsoft.
- In SDK package, add the “CRMSDKTemplates” into Visual Studio (find the matched one with your VS version).
- Create a new CRM Plug-in Library in Visual Studio.
- Create early bound entity classes with the code generation tool (CrmSvcUtil.exe, run it in Command Prompt under this tool’s directory).
- Use the early bound entity classes to retrieve the Sales Invoice entity from the LocalPluginContext.
Create and use Web API in D365 for Financials
Now you have your Sales Invoice entity from CRM and want to map the related fields in Financials and send those field values to Financials.
- Create a Web Service for Sales Invoices in Financials. Make a copy of the Web Service URL and you will need it later. https://yourcompanyname.financials.dynamics.com:7048/MS/ODataV4/Company(‘ yourcompanyname’)/SalesInvoice
- Find out the metadata (data structure formatted in JSON) of the Web Service you just created. What I did is creating a temporary Console Application and adding the service reference. Then I could check the data type of the sales invoice in Visual Studio.
- Decide and map the related fields. Create your own class with the same fields in the Plugin code (the class name can be any, but the type and name of those fields you need to map should be exactly the same as what is in the service reference).
- Use DataContractJsonSerializer to serialize your created class for sending it to the web service.
- Create a web request to the URL of the Web Service using HttpClient class (username is your account user name in Financials and the password is your web service key).
- Send the request to the Web Service using POST method.
- Then you maybe want to send the Sales Invoice line items. You should repeat Steps 2 to 6 for the line items: creating a line item class, mapping the fields, serializing it and posting it. The only tricky point is the line item URL is based on the header you previously sent. It looks like: https://yourcompanyname.financials.dynamics.com:7048/MS/ODataV4/Company(‘yourcompanyname’)/SalesInvoice(Document_Type=’Invoice’,No=’SampleInvoiceNo1′)/SalesInvoiceSalesLines
What’s more tricky is that sometimes the header passed into Financials successfully but the line or lines do not. Then you have to log into Financials and delete the header manually.
To learn more about Microsoft Dynamics 365 for Financials, contact us at email@example.com.