forcedotcom / b2b-commerce-on-lightning-quickstart

Creative Commons Zero v1.0 Universal
175 stars 208 forks source link

Unable to use B2BSearchControllerSample.productSearch with effectiveAccountId and Named Principal Named Credential so unable to display prices #11

Open peterTrifecta opened 3 years ago

peterTrifecta commented 3 years ago

We’ve run into an issue with the reference component that uses the REST Connect API for searching that we need to use for a custom Category / Search Results.

B2BSearchControllerSample.productSearch: https://github.com/forcedotcom/b2b-commerce-on-lightning-quickstart/blob/b8a34565d394a2b4460cee0bc587b2c720e87842/examples/lwc/force-app/main/default/classes/B2BSearchControllerSample.cls#L24

That calls the product-search endpoint.

httpRequest.setEndpoint( 'callout:CallMeBack/services/data/v' + apiVersionApexClass + '/commerce/webstores/' + webstoreId + '/search/product-search' );

The reference implementation ignores the effectiveAccountId (note it is a parameter to the Apex method, but never passed in the REST call), which means that pricing is not displayed. When we add it as specified in the documentation https://developer.salesforce.com/docs/atlas.en-us.chatterapi.meta/chatterapi/connect_resources_commerce_webstore_product_search.htm

?effectiveAccountId=

We get an error:

[{​​​​​​​​"errorCode":"INVALID_API_INPUT","message":"Only a buyer user can access this account."}​​​​​​​​]

We followed the setup steps here for the Named Credential:

https://github.com/forcedotcom/b2b-commerce-on-lightning-quickstart/blob/master/examples/lwc/docs/NamedCredentials.md

The problem is that our Named Principal doesn’t have the Buyer User system permission. So, we created a permission set to assign it to our Named Principal user, but it is not possible. We get an error that the Buyer User permission is not applicable for the license type of our Named Principal, which is Salesforce (also doesn’t work for Platform). However, there is no way to create a named principal for the Named Credential that is a Community Login User and we cannot do it on a Per User basis either, because that would require the store users to go through the webflow for oauth which we obviously cannot do.

We are stuck on not being able to use this AND display pricing.

Ideally, the reference implementation should be updated to not ignore the effectiveAccountId parameter since it is more likely that prices would be desired in the search results than not. At least, if it is not possible, that should be documented.

patrykzak commented 2 years ago

@peterTrifecta are you still facing this issue? I have not encountered your error, I am able to display prices for logged in users since I am able to pass accountId (made sure that provided account is a buyer group member in the buyer group assigned to webstore and that this account is enabled as buyer) into effectiveAccountId parameter, however my problem is when I pass the Id of guestBuyerProfile, then I always get null for prices, therefore I am unable to show prices for guest users. I have set name credentials like in the provided guide with all Application Permissions on the connected app.

KaranK-Valtech commented 1 year ago

Hi All,

I was also having issues in retrieving the pricing, below is the sample code for everyone's reference

HttpRequest httpRequest = new HttpRequest();

httpRequest.setMethod('POST');

httpRequest.setHeader(

            'Content-Type',

            'application/json;  charset=utf-8'

        );

httpRequest.setEndpoint(

            'callout:CallMeBack/services/data/v' +

            apiVersionApexClass +

            '/commerce/webstores/' +

            webstoreId +

            '/search/product-search?effectiveAccountId='+effectiveAccID

        );

httpRequest.setBody(searchQuery);

Http http = new Http();

HttpResponse httpResponse = http.send(httpRequest); 

Additional things to note:

Thanks @patrykzak @peterTrifecta