0

I have a pricebook set up with several products in it. I would like to query these products and display them on my React app.

I am a little confused on how to go about this as I am a newbie to salesforce and only know how to query an order so far

Any leads would be appreciated

2

Welcome to SF Stack Exchange!

Since you are new I would like to point out two things about the query you've asked about.

  1. This might appear confusing to you but the API Name for Product is Product2(link) and the one for a pricebook is Pricebook2(link).

  2. The connection between the two is established via a junction object called PricebookEntry.

With these 2 in mind your query would be:

[SELECT Id, Product2Id, Product2.Name FROM PricebookEntry WHERE Pricebook2Id='12345']

Where 12345 is your Pricebook's Id.

Essentially that will retrieve a List of PriceBookEntry objects.

Now you can iterate over those objects to extract the Product objects you are interested in.

List<PriceBookEntry> pEntries = [SELECT Id, Product2Id, Product2.Name, Product2.IsActive FROM PricebookEntry WHERE Pricebook2Id=:pbookId];

for (PriceBookEntry pEntry: pEntries) {
    system.debug(pEntry.Product2Id);
    system.debug(pEntry.Product2.Name);
    system.debug(pEntry.Product2.IsActive);
}

I imagine that once you retrieve the pEntries from APEX you can iterate over them in React and pull the Product data that you need.

If you want to return a list of products from APEX instead you can change the above code to the following:

public static List<Product2> getProducts(Id pbookId) {
    List<PriceBookEntry> pEntries = [SELECT Id, Product2Id, Product2.Name, Product2.IsActive FROM PricebookEntry WHERE Pricebook2Id=:pbookId];

    List<Product2> products = new List<Product2>();
    for (PriceBookEntry pEntry: pEntries) {
        Product2 aProduct = new Product2();
        aProduct.Id = pEntry.Product2Id;
        aProduct.Name = pEntry.Product2.Name;
        aProduct.IsActive = pEntry.Product2.IsActive;
        // add any additional fields from your query.
        products.add(aProduct);
    }
    return products;
}
8
  • This is very helpful mate :) Could you however provide some insight as to how exactly I would use [SELECT Id, Product2Id, Product2.Name FROM PricebookEntry WHERE Pricebook2Id='12345'] when making a get request to the salesforce API? Right now I use the below to get the info about the price book – Kisho Jul 1 '20 at 7:58
  • const result = await axios({ method: 'get', url: 'tahina-test2-dev-ed.my.salesforce.com/services/data/v20.0/…', data: { fields: 'Name' }, headers: { Authorization: 'Bearer 00D2X000001esWS!ASAAjbhvhvhvA4rn1' } }); – Kisho Jul 1 '20 at 7:59
  • @Kisho How is your request payload allowing you to identify the pricebook you need? All you are sending in is fields: 'Name'. – Arthlete Jul 1 '20 at 14:22
  • So I should request for Product2Id and Product2Id.Name? – Kisho Jul 1 '20 at 14:39
  • I am now running into a cors issue :/ I did not have this issue when I whitelisted localhost:8000 last night. Do you know a workaround for this? @Arthlete – Kisho Jul 1 '20 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.