Programmatically Adding Products to an Opportunity Product Bundle in Dynamics 365

Jordan Hohepa, 25 May 2018

In Microsoft Dynamics 365, Users have the option to create different types of products. These products can be created as standalone products, product families and product bundles. Product bundles are a good way to group related products and sell them together. The screenshot below shows a test bundle I have added to an opportunity, which groups together three standalone products.

image

One of the limitations of product bundles is they aren’t easily customizable once they have been added to an opportunity. Using the bundle above as an example, if I needed to add an extra standalone product to this bundle, there currently is not a way to do this. Even updating the bundle product itself is a difficult task.

There is a way around this programmatically, which would be done by adding an opportunity product and use background logic to set the ‘parentbundleid’ and ‘producttypecode’ fields. The screenshot below shows the fields in advanced find, with the parentbundleid being the id of ‘Test Product Bundle 2 – No Required’ and the Product Type being ‘Optional Bundle Product’.

image

The code below shows an example of an opportunity product being created in a plugin, where sdk is an instance of IOrganizationService and the Id fields (productid, opportunityId, uomId, bundleId) are retrieved earlier based on a set of conditions.

image

Earlier I mentioned that the producttypecode needed to be set to ‘Optional Bundle Product’, but while configuring this I found that if I attempted to set this on create it would automatically be overwritten. To remedy this, I decided to use a separate pre-sync plugin to update the Opportunity Products producttypecode after it had been created. The screenshot below shows this logic which is triggered by the custom two option set field ‘mag_converttobundleproduct’ being set to True. ‘mag_converttobundleproduct’ is being set to True in the previous screenshot.

image

Once the plugins run, with both the parentbundleid and the producttypecode being set, the newly created opportunity product will appear under the bundle in the subgrid and will act as a product of the bundle.