How to create an OrderSummary record using Apex【B2B Commerce Cloud】

Preface

I wrote this article because the instructions provided in the B2B Commerce Cloud developer document “Create Unmanaged Order Summaries" is too brief to actually generate an Order Summary record.

Step1:Create an order and its associated records

First of all, we need to create the following three records.

  1. Order
  2. OrderDeliveryGroupOrder
  3. OrderItem
Order order = new Order(
    AccountId = cart.AccountId,
    OwnerId = cart.OwnerId,
    SalesStoreId = cart.WebStoreId,
    PoNumber = cart.PoNumber,
    BillingStreet = cart.BillingStreet,
    BillingCity = cart.BillingCity,
    BillingState = cart.BillingState,
    BillingPostalCode = cart.BillingPostalCode,
    BillingCountry = cart.BillingCountry,
    BillingLatitude = cart.BillingLatitude,
    BillingLongitude = cart.BillingLongitude,
    EffectiveDate = now,
    OrderedDate = now,
    Status = 'Draft'
);
insert(order);

OrderDeliveryGroup orderDeliveryGroup = new OrderDeliveryGroup(
    Description = cartDeliveryGroup.ShippingRequests__c,
    DesiredDeliveryDate = desiredDeliveryDate,
    DeliverToName = cartDeliveryGroup.DeliverToName,
    DeliveryInstructions = cartDeliveryGroup.ShippingInstructions,
    DeliverToStreet = cartDeliveryGroup.DeliverToStreet,
    DeliverToCity = cartDeliveryGroup.DeliverToCity,
    DeliverToState = cartDeliveryGroup.DeliverToState,
    DeliverToPostalCode = cartDeliveryGroup.DeliverToPostalCode,
    DeliverToCountry = cartDeliveryGroup.DeliverToCountry,
    DeliverToLatitude = cartDeliveryGroup.DeliverToLatitude,
    DeliverToLongitude = cartDeliveryGroup.DeliverToLongitude,
    OrderDeliveryMethodId = cartDeliveryGroup.DeliveryMethodId,
    OrderId = orderId
);
insert(orderDeliveryGroup);

OrderItem orderItem = new OrderItem(
    Product2Id = cartItem.Product2Id,
    Type = orderItemType,
    Quantity = cartItem.Quantity,
    ListPrice = cartItem.ListPrice,
    UnitPrice = unitPrice,
    OrderId = orderId,
    OrderDeliveryGroupId = orderDeliveryGroupId,
    TotalLineAmount = cartItem.TotalLineAmount
);
insert(orderItem);

Step2:Update the status of the order to 'Activated’

Second, change the status of the order to 'Activated’, just like we do in Checkout.

order.Status = 'Activated';
update order;

Step3: Create an OrderSummary from an Order using the createOrderSummary method.

Finally, we create an OrderSummary from the Order.

At this time, OrderDeliveryGroupSummary and OrderItemSummary records are also automatically generated.

As with checkout, the values of custom fields are automatically copied if the API reference name is the same.

ConnectApi.OrderSummaryInputRepresentation osir = new
ConnectApi.OrderSummaryInputRepresentation();
osir.orderId=order.Id; //replace this recordId with an appropriate value.
osir.orderLifeCycleType='UNMANAGED';
ConnectApi.OrderSummaryOutputRepresentation osor =
ConnectApi.OrderSummaryCreation.createOrderSummary(osir);

Salesforce

Posted by regardie