What to do when I get this exception - ConnectApi.CommerceCatalog.getProduct on inactive product?

868    Asked by DipikaAgarwal in Salesforce , Asked on May 1, 2023

 I am working on a B2B commerce project and I have issues with deactivated products (IsActive=false)

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_ConnectAPI_CommerceCatalog_static_methods.htm#apex_ConnectAPI_CommerceCatalog_getProduct_2

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)

Answered by Ella Clarkson

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.


Your Answer

Answer (1)

The ConnectApi.CommerceCatalog.GetProduct exception occurs when attempting to access product information for a product that is inactive in Salesforce. This typically means that the product you're trying to query is not available or has been deactivated.

Here are some steps you can take to troubleshoot and resolve this issue:

Steps to Resolve the Issue

Check Product Status:

Ensure that the product you are trying to access is active. You can check this in the Salesforce UI:

Go to the Products tab.

Search for the product by its name or ID.

Verify that the product is marked as active.

Handle Exceptions Gracefully:

Implement error handling in your Apex code to gracefully handle cases where the product is inactive. You can catch the exception and take appropriate action, such as notifying the user or logging the error.

  try {    ConnectApi.CommerceCatalog.Product product = ConnectApi.CommerceCatalog.getProduct(productId);    // Process the product} catch (ConnectApi.ConnectApiException e) {    if (e.getMessage().contains('inactive')) {        // Handle the case where the product is inactive        System.debug('The product is inactive: ' + productId);        // You can notify the user or take other actions as needed    } else {        // Handle other exceptions        throw e;    }}

Check Product ID:

Verify that the product ID you are using is correct. Ensure that it matches the ID of an active product in your Salesforce org.

Query Active Products Only:

When querying for products, ensure that you are only retrieving active products. You can add a filter to your SOQL query to only return products where IsActive is true.

  List activeProducts = [SELECT Id, Name FROM Product2 WHERE IsActive = true];

Update Product Status:

If you have control over the product data, consider updating the status of inactive products to active if they should be available. Be cautious with this approach as it may have implications for your business processes.

  Product2 product = [SELECT Id, IsActive FROM Product2 WHERE Id = roductId LIMIT 1];if (!product.IsActive) {    product.IsActive = true;    update product;}

Review Product Catalog Setup:

Ensure that your product catalog setup in Salesforce is correct and that there are no issues with how products are managed or published.

Example Code with Exception Handling

Here’s a more complete example demonstrating how to handle the ConnectApi.CommerceCatalog.getProduct exception:

  public class ProductService {    public static ConnectApi.CommerceCatalog.Product getProduct(String productId) {        try {            ConnectApi.CommerceCatalog.Product product = ConnectApi.CommerceCatalog.getProduct(productId);            return product;        } catch (ConnectApi.ConnectApiException e) {            if (e.getMessage().contains('inactive')) {                System.debug('The product is inactive: ' + productId);                // Additional handling, such as notifying the user or logging the issue            } else {                throw e; // Re-throw other exceptions            }        }        return null; // Return null if the product is inactive or not found    }}

Summary

The ConnectApi.CommerceCatalog.GetProduct exception related to inactive products can be addressed by verifying product status, handling exceptions gracefully, ensuring the correct product ID, querying only active products, and reviewing your product catalog setup. Implementing these steps will help ensure that your code handles inactive products appropriately and provides a better user experience.








4 Months

Interviews

Parent Categories