I have seen a couple of blogs where one can fetch the latest exchange rates from a service hosted on internet and then update the currency entity. However I have not seen any on what to do with latest exchange rate. I just wrote a workflow that runs in async mode and updates all foreign currency opportunities and related entities based on the latest exchange rates.
As part of this requirement users have the ability to start a dialog on the currency entity that in turn calls a custom workflow to update all open opportunities. As a result all the base currency fields are re-calculated to reflect the accurate base amount based on latest exchange rate. It is an asynchronous process as it can take hours to fetch all open opportunities, opportunity products and opportunity forecasts and update all of them based on latest exchange rates. It is envisaged that only System Administrators/trained users will initiate this process and that too only once in a year.
The solution is implemented using a combination of dialog and child workflow. As a child workflow, a custom workflow registered using the plug-in registration tool is initiated that fetches and updates all open opportunities, opportunity forecasts and opportunity products in an asynchronous manner. The dialog is configured to run on the Currency View page in the ‘Business Management’ section of the application. One small little glitch in this solution is that in order to instantiate the dialog one has to select at least one currency in the View. This does not make sense as it gives the impression that the dialog / operation is going to be performed on the selected currency however as already stated the workflow is designed to update all open currency on latest exchange rate of all currencies and not just one currency.Here is the explanation of the problem
As one can see in the figure above that none of the entity is selected so the dialog cannot be launched. In order to launch the dialog one has to select the currency
This is where the user might feel that the dialog would run only for Australian dollar however the dialog runs for all the open opportunities irrespective of the currency. We have added error messages to somewhat reduce the impact but this problem remains.
Here is complete overview of the implemented solution:
1. ‘UpdateOpportunityExchangeRate’ class has the logic to fetch opportunities and related entities like opportunity forecasts and opportunity products.
2. By default the fetch query in CRM has a limit on the total number of records that can be fetched in a query. In order to by pass this one has to use ‘fetchXml’ and pass in the cookie to keep track of records fetched. This solution uses the same approach to fetch all the opportunities keeping track of the records being fetched.
3. An important aspect of the ‘money’ fields is that whenever a record is saved and one of the money fields is updated all the ‘money’ fields in the record are updated by CRM system. This is important to understand as this implies that all the ‘base_amounts’ are recalculated once a particular field is updated on a record.
4. Workflow uses the principle described in Step-3 to maximum as in-order to update all ‘money’ base value fields we just fetch and update one ‘money’ field. Doing this reduces the information flow to a great extent. This results in significant impact on the optimization of the overall code because this allows updating all ‘money’ fields without having to fetch and update each one of them.
Register workflow to CRM
1. Compile the solution to generate the assembly for the workflow.
2. Use the plug-in registration tool to register the plug-in to CRM. The plug-in registration tool is available as part of the SDK.
3. Once the assembly is registered in the CRM it is available to be used as the Child Workflow.
4. Child workflow always runs in the asynchronous mode.
1. Out of the box dialog ‘Update all open opportunities with latest exchange rates’ is created in the CRM.
2. The dialog is configured on the Currency entity.
3. Dialog requires selection of at least one record for the execution of the dialog.
4. Dialogs ensure interaction with user to get approval to “update all open opportunities with all currencies”. This is required as confirmation must be obtained before all open opportunities are updated.
5. Once user approves the opportunity update process, workflow is instantiated in asynchronous mode.
6. Here is the detail of the workflow:
7. Workflow runs in the asynchronous mode in the background and performs the operations of updating the opportunities, opportunity forecasts and opportunity products.
Here are the deployment steps for this solution:
Deployment using the Solution in CRM
1. As part of the deployment the following two artifacts must be added to the Solution that will imported to the desired organization:
i. “Update all open opportunities with latest exchange rates”
If you have any question or queries, do not hesitate to reach out to us!