@@ -6,8 +6,8 @@ global with sharing class B2BSyncCheckInventory {
66 // Validate the input
77 if (cartIds == null || cartIds .size () != 1 ) {
88 String errorMessage = ' A cart id must be included to B2BSyncCheckInventory' ;
9- saveCartValidationOutputError ( errorMessage , ' ' );
10- throw new CalloutException (errorMessage );
9+ // Sync non-user errors skip saveCartValidationOutputError
10+ throw new IllegalArgumentException (errorMessage );
1111 }
1212
1313 // Extract cart id and start processing
@@ -16,57 +16,45 @@ global with sharing class B2BSyncCheckInventory {
1616 }
1717
1818 private static void startCartProcessSync (ID cartId ) {
19- try {
20- // Get all SKUs and their quantities from cart items.
21- Map <String , Decimal > quantitiesFromSalesforce = new Map <String , Decimal >();
22- for (CartItem cartItem : [SELECT Sku , Quantity FROM CartItem WHERE CartId = : cartId AND Type = ' Product' WITH SECURITY_ENFORCED ]) {
23- if (String .isBlank (cartItem .Sku )) {
24- String errorMessage = ' The SKUs for all products in your cart must be defined.' ;
25- saveCartValidationOutputError (errorMessage , cartId );
26- throw new CalloutException (errorMessage );
27- }
28- quantitiesFromSalesforce .put (cartItem .Sku , cartItem .Quantity );
19+ // Get all SKUs and their quantities from cart items.
20+ Map <String , Decimal > quantitiesFromSalesforce = new Map <String , Decimal >();
21+ for (CartItem cartItem : [SELECT Sku , Quantity FROM CartItem WHERE CartId = : cartId AND Type = ' Product' WITH SECURITY_ENFORCED ]) {
22+ if (String .isBlank (cartItem .Sku )) {
23+ String errorMessage = ' The SKUs for all products in your cart must be defined.' ;
24+ saveCartValidationOutputError (errorMessage , cartId );
25+ throw new CalloutException (errorMessage );
2926 }
27+ quantitiesFromSalesforce .put (cartItem .Sku , cartItem .Quantity );
28+ }
3029
31- // Stop checkout if there are no items in the cart
32- if (quantitiesFromSalesforce .isEmpty ()) {
33- String errorMessage = ' Looks like your cart is empty.' ;
30+ // Stop checkout if there are no items in the cart
31+ if (quantitiesFromSalesforce .isEmpty ()) {
32+ String errorMessage = ' Looks like your cart is empty.' ;
33+ saveCartValidationOutputError (errorMessage , cartId );
34+ throw new CalloutException (errorMessage );
35+ }
36+
37+ // Get all available quantities for products in the cart (cart items) from an external service.
38+ Map <String , Object > quantitiesFromExternalService = getQuantitiesFromExternalService (cartId , quantitiesFromSalesforce .keySet ());
39+
40+ // For each cart item SKU, check that the quantity from the external service
41+ // is greater or equal to the quantity in the cart.
42+ // If that is not true, set the integration status to "Failed".
43+ for (String sku : quantitiesFromSalesforce .keySet ()) {
44+ Decimal quantityFromSalesforce = quantitiesFromSalesforce .get (sku );
45+ Decimal quantityFromExternalService = (Decimal )quantitiesFromExternalService .get (sku );
46+ if (quantityFromExternalService == null ){
47+ String errorMessage = ' The product with sku ' + sku + ' could not be found in the external system' ;
3448 saveCartValidationOutputError (errorMessage , cartId );
35- throw new CalloutException (errorMessage );
36- }
37-
38- // Get all available quantities for products in the cart (cart items) from an external service.
39- Map <String , Object > quantitiesFromExternalService = getQuantitiesFromExternalService (cartId , quantitiesFromSalesforce .keySet ());
40-
41- // For each cart item SKU, check that the quantity from the external service
42- // is greater or equal to the quantity in the cart.
43- // If that is not true, set the integration status to "Failed".
44- for (String sku : quantitiesFromSalesforce .keySet ()) {
45- Decimal quantityFromSalesforce = quantitiesFromSalesforce .get (sku );
46- Decimal quantityFromExternalService = (Decimal )quantitiesFromExternalService .get (sku );
47- if (quantityFromExternalService == null ){
48- String errorMessage = ' The product with sku ' + sku + ' could not be found in the external system' ;
49- saveCartValidationOutputError (errorMessage , cartId );
50- throw new CalloutException (errorMessage );
51- }
52- else if (quantityFromExternalService < quantityFromSalesforce ){
53- String errorMessage = ' Insufficient quantity for the product with sku ' + sku + ' : '
54- + quantityFromSalesforce + ' needed, but only '
55- + quantityFromExternalService + ' available.' ;
56- saveCartValidationOutputError (errorMessage , cartId );
57- throw new CalloutException (errorMessage );
58- }
49+ throw new CalloutException (errorMessage );
50+ }
51+ else if (quantityFromExternalService < quantityFromSalesforce ){
52+ String errorMessage = ' Insufficient quantity for the product with sku ' + sku + ' : '
53+ + quantityFromSalesforce + ' needed, but only '
54+ + quantityFromExternalService + ' available.' ;
55+ saveCartValidationOutputError (errorMessage , cartId );
56+ throw new CalloutException (errorMessage );
5957 }
60- } catch (CalloutException e ) {
61- throw e ;
62- } catch (Exception e ) {
63- // For testing purposes, this example treats exceptions as user errors, which means they are displayed to the buyer user.
64- // In production you probably want this to be an admin-type error. In that case, throw the exception here
65- // and make sure that a notification system is in place to let the admin know that the error occurred.
66- // See the readme section about error handling for details about how to create that notification.
67- String errorMessage = ' An exception of type ' + e .getTypeName () + ' has occurred: ' + e .getMessage ();
68- saveCartValidationOutputError (errorMessage , cartId );
69- throw new CalloutException (errorMessage );
7058 }
7159 }
7260
@@ -98,7 +86,7 @@ global with sharing class B2BSyncCheckInventory {
9886 }
9987 else {
10088 String errorMessage = ' There was a problem with the request. Error: ' + response .getStatusCode ();
101- saveCartValidationOutputError ( errorMessage , cartId );
89+ // Sync non-user errors skip saveCartValidationOutputError
10290 throw new CalloutException (errorMessage );
10391 }
10492 }
@@ -109,7 +97,7 @@ global with sharing class B2BSyncCheckInventory {
10997 // CartId: Foreign key to the WebCart that this validation line is for
11098 // Level (required): One of the following - Info, Error, or Warning
11199 // Message (optional): Message displyed to the user
112- // Name (required): The name of this CartValidationOutput record. For example CartId:BackgroundOperationId
100+ // Name (required): The name of this CartValidationOutput record. For example CartId
113101 // RelatedEntityId (required): Foreign key to WebCart, CartItem, CartDeliveryGroup
114102 // Type (required): One of the following - SystemError, Inventory, Taxes, Pricing, Shipping, Entitlement, Other
115103 CartValidationOutput cartValidationError = new CartValidationOutput (
@@ -123,4 +111,4 @@ global with sharing class B2BSyncCheckInventory {
123111
124112 insert (cartValidationError );
125113 }
126- }
114+ }
0 commit comments