Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

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

Asked by Dipika Agarwal May 1, 2023 2.9K views 2 answers
Share

About this question

 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)

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jul 16, 2024

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.








Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.