What to do when I get this exception - ConnectApi.CommerceCatalog.getProduct on inactive product?
I am working on a B2B commerce project and I have issues with deactivated products (IsActive=false)
It returns an exception "ConnectApi.NotFoundException: Resource not found." which makes sense but it is used to get the products' images in the order history for instance, which is not working anymore once a product is deactivated.
Is there an other API or parameters to use for this specific use case or should I implement a workaround (SOQL query to get the medias for instance and fetch them with the CMS api)
We've had this issue before in our project, here is what we did.
ConnectApi Commerce class methods respects the structure of your storefront (I.E Entitlements, Pricebooks, Products)
So as a workaround:
we had to use ConnectApi.ManagedContentVersionCollection to retrieve medias attached to our community
we need to retrieve the media's attached to product using ProductMedia object
The implementation is something like this: (I cannot give you the full code due to NDA but hopefully you get the idea)
// Give me all product medias based on the product ids given List productMedia = new List([ SELECT ElectronicMediaId, ProductId FROM ProductMedia WHERE ProductId =: productIds ]); // Content Id to be Processed List contentIds = new List(); for (ProductMedia media : productMedia){ // Key takeaway here is you will use the ElectronicMediaId and match it against the result of the media source node when you use the ConnectAPI ManagedContentVersionCollection .... } // Get Community images from Salesforce CMS based on the targeted products ConnectApi.ManagedContentVersionCollection contentList = ConnectApi.ManagedContent.getManagedContentByIds('CommunityId', 'electronicMediaIds From Product Media Query', 0, 100, 'en_US', 'cms_image'); // using the Connect API ManagedContentVersionCollection for (ConnectApi.ManagedContentVersion versionItems : contentList.items) { // Add to ConnectApi ManagedContentMediaSourceNodeValue List mediaSourceNodes.add((ConnectApi.ManagedContentMediaSourceNodeValue) versionItems.contentNodes.get('source')); // Loop through this node to be able to get the content id and media url for (ConnectApi.ManagedContentMediaSourceNodeValue nodeValue : mediaSourceNodes) { // Assign values to a map for getting mappingOfMediaIdAndUrl.put(versionItems.managedcontentid, nodeValue.url); } } // At this point you have the respective product image URLs and electronic media ids available for processing. Even if the product is inactive this can be used in the order pages.