orderExtensionUtils Service Component Methods

To access the retail order data, you can use a few out-of-the-box APIs that are exposed in the orderExtensionUtils service component. Example: Accesses order data when the getorderdata API is invoked from the custom LWC component.

Required Editions

Available in: Lightning Experience

Available in: Enterprise and Unlimited Editions that have Consumer Goods Cloud enabled

Here are the methods available in the orderExtensionUtils service component:

Note
Note The API version for all methods is 59.
Method Description Syntax Import Command Sample Code
getOrderData Returns data from order fields. getOrderData(recordId) import { getOrderData } from 'cgcloud/orderExtensionUtils'; {  @api recordId;  initialOrderData = {};  connectedCallback() {    getOrderData(this.recordId).then((data) => {      this.initialOrderData = data;    });  }}
updateOrderData Updates order fields. updateOrderData(recordId, srcThisRef, fieldApiName, value) import { getOrderData, updateOrderData } from 'cgcloud/orderExtensionUtils'; {  @api recordId;  initialOrderData = {};  connectedCallback() {    getOrderData(this.recordId).then((data) => {      this.initialOrderData = data;    });  }  // You can invoke this method with the click of a button  updateOrderFieldValue() {    const fieldName = 'cgcloud__Delivery_Note__c';    const newValue = 'test-note';    updateOrderData(this.recordId, this, fieldName, newValue);  }}
getOrderItemData Returns order items data. getOrderItemData(recordId) import { getOrderItemData } from 'cgcloud/orderExtensionUtils'; {  @api recordId;  initialOrderItemsData = [];  connectedCallback() {    getOrderItemData(this.recordId).then((data) => {      this.initialOrderItemsData = data;    });  }}
updateOrderItemData Updates order items data. updateOrderItemData(recordId, srcThisRef, itemId, fieldApiName, value) import { getOrderItemData, updateOrderItemData } from 'cgcloud/orderExtensionUtils'; {  @api recordId;  initialOrderItemsData = [];  connectedCallback() {    getOrderItemData(this.recordId).then((data) => {      this.initialOrderItemsData = data;    });  }  updateOrderItemFieldData() {    const orderItemId = this.initialOrderItemsData[0].Id;    const fieldName = 'cgcloud__Discount__c';    const value = 9;    updateOrderItemData(this.recordId, this, orderItemId, fieldName, value);  }}
setCustomState Sets up additional data to be stored along with the order data. For example, new record creation in a custom object. The additional data setup that uses the setCustomState method can be sent to the custom APEX class handling save customizations. setCustomState(recordId, customState) import { setCustomState } from 'cgcloud/orderExtensionUtils'; {  @api recordId;  connectedCallback() {}  setOrderCustomState() {    setCustomState(this.recordId, {      customProp: 'Test'    });  }}
getIsOrderInEditMode Returns whether the order is in edit mode during the initial page load. You can use this information to show custom LWC components only in read-only mode during the initial page load. getIsOrderInEditMode() import { getIsOrderInEditMode } from 'cgcloud/orderExtensionUtils'; {  @api recordId;  @track isOrderInEditMode = false;  connectedCallback() {    this.isOrderInEditMode = getIsOrderInEditMode();  }}
registerListenerForOrderDataUpdates Registers a callback method invoked when any order field changes. registerListenerForOrderDataUpdates(recordId, thisRef, callback) import {  getOrderData,  registerListenerForOrderDataUpdates} from 'cgcloud/orderExtensionUtils'; {  @api recordId;  initialOrderData = {};  orderDataUpdates = {};  connectedCallback() {    getOrderData(this.recordId).then((data) => {      this.initialOrderData = data;    });    registerListenerForOrderDataUpdates(      this.recordId,      this,      this.handleOrderDataUpdates    );  }  handleOrderDataUpdates(fieldApiName, value) {    this.orderDataUpdates[fieldApiName] = value;  }}
registerOrderDataInlineValidator Registers a callback method invoked when an order field changes and performs a validation on the order field. registerOrderDataInlineValidator(recordId, thisRef, callback) import {  getOrderData,  registerOrderDataInlineValidator} from 'cgcloud/orderExtensionUtils'; {  @api recordId;  initialOrderData = {};  connectedCallback() {    getOrderData(this.recordId).then((data) => {      this.initialOrderData = data;    });    registerOrderDataInlineValidator(      this.recordId,      this,      this.validateOrderDataChanges    );  }  validateOrderDataChanges(fieldName, value) {    switch (fieldName) {      case 'cgcloud__Invoice_Note__c':      case 'cgcloud__Delivery_Note__c':        return this.validateNoteFields(value);      default:        return '';    }  }  validateNoteFields(value) {    if (value && value.length > 100) {      return 'This text should be less than 100 characters';    }  }}
registerListenerForOrderItemDataUpdates Registers a callback method invoked when any order item field changes. registerListenerForOrderItemDataUpdates(recordId, thisRef, callback) import {  getOrderItemData,  registerListenerForOrderItemDataUpdates} from 'cgcloud/orderExtensionUtils'; {  @api recordId;  initialOrderItemsData = [];  connectedCallback() {    getOrderItemData(this.recordId).then((data) => {      this.initialOrderItemsData = data;    });    registerListenerForOrderItemDataUpdates(      this.recordId,      this,      this.handleOrderItemDataUpdates    );  }  handleOrderItemDataUpdates(itemId, fieldApiName, value) {    const orderItem = this.initialOrderItemsData.find(      (_orderItem) => _orderItem.Id === itemId    );    orderItem[fieldApiName] = value;  }}
registerOrderItemDataInlineValidator Registers a callback method invoked when an order item field changes and performs a validation on the order item field. registerOrderItemDataInlineValidator(recordId, thisRef, callback) import {  getOrderItemData,  registerOrderItemDataInlineValidator} from 'cgcloud/orderExtensionUtils'; {  @api recordId;  initialOrderItemsData = [];  connectedCallback() {    getOrderItemData(this.recordId).then((data) => {      this.initialOrderItemsData = data;    });    registerOrderItemDataInlineValidator(      this.recordId,      this,      this.validateOrderItemDataChanges    );  }  validateOrderItemDataChanges(itemId, fieldName, value) {    switch (fieldName) {      case 'Discount__c':        return this.validateDiscount(value);      default:        return '';    }  }  validateDiscount(value) {    if (value > 10) {      return 'Item discount should not be more than 10%';    }    return '';  }}
registerBeforeAddItemActionHandler Registers a callback method before adding items in an order. registerBeforeAddItemActionHandler(recordId, thisRef, callback) import {  getOrderData,  registerListenerForOrderDataUpdates,  registerBeforeAddItemActionHandler} from 'cgcloud/orderExtensionUtils'; {  @api recordId;  initialOrderData = {};  orderDataUpdates = {};  connectedCallback() {    getOrderData(this.recordId).then((data) => {      this.initialOrderData = data;    });    registerListenerForOrderDataUpdates(      this.recordId,      this,      this.handleOrderDataUpdates    );    registerBeforeAddItemActionHandler(      this.recordId,      this,      this.handleBeforeAddItemCB    );  }  handleOrderDataUpdates(fieldApiName, value) {    this.orderDataUpdates[fieldApiName] = value;  }  handleBeforeAddItemCB(newOrderItems) {    const orderData = { ...this.initialOrderData, ...this.orderDataUpdates };    newOrderItems = newOrderItems.map((orderItem) => {      if (        orderData.cgcloud__Order_Account__r.Name === '*NTO Store #201' &&        orderItem['Product__r.Description_1__c'] === 'BBQ'      ) {        throw new Error("You can't add BBQ product");      }      orderItem.Quantity__c = 2;      orderItem.Number__c = 10;      return orderItem;    });  }}
registerBeforeSaveActionHandler Registers a callback method before saving an order. registerBeforeSaveActionHandler(recordId, thisRef, callback) import {  getOrderData,  registerBeforeSaveActionHandler} from 'cgcloud/orderExtensionUtils'; {  @api recordId;  initialOrderData = {};  connectedCallback() {    getOrderData(this.recordId).then((data) => {      this.initialOrderData = data;    });    registerBeforeSaveActionHandler(      this.recordId,      this,      this.handleBeforeSaveCB    );  }  handleBeforeSaveCB(savePayload) {    const {      orderItemsToBeUpserted: orderItems,      updatedOrder: orderData    } = savePayload;    for (let i = 0; i < orderItems.length; i++) {      const orderItem = orderItems[i];      if (orderItem.cgcloud__Quantity__c > 90) {        throw new Error("Order Item Quantity shouldn't be more than 90");      }    }    let headerDiscount = orderData.cgcloud__Header_Discount_Percentage__c;    headerDiscount = headerDiscount && Number(headerDiscount);    if (headerDiscount && headerDiscount > 15) {      throw new Error("Header discount can't be greater than 15");    }    let deliveryDate = orderData.cgcloud__Delivery_Date__c;    if (deliveryDate) {      deliveryDate = new Date(deliveryDate);      deliveryDate.setDate(deliveryDate.getDate() + 1);      orderData.cgcloud__Delivery_Date__c = deliveryDate;    }  }}
registerListenerForEnablingOrDisablingEditMode Returns whether the order is in edit mode during the order taking process. You can use this information to show custom LWC components only in read-only mode. registerListenerForEnablingOrDisablingEditMode(recordId, thisRef, callback) import {  getOrderData,  registerListenerForEnablingOrDisablingEditMode} from 'cgcloud/orderExtensionUtils'; {  @api recordId;  initialOrderData = {};  connectedCallback() {    getOrderData(this.recordId).then((data) => {      this.initialOrderData = data;    });    registerBeforeSaveActionHandler(      this.recordId,      this,      this.handleBeforeSaveCB    );  }  handleBeforeSaveCB(savePayload) {    const {      orderItemsToBeUpserted: orderItems,      updatedOrder: orderData    } = savePayload;    for (let i = 0; i < orderItems.length; i++) {      const orderItem = orderItems[i];      if (orderItem.cgcloud__Quantity__c > 90) {        throw new Error("Order Item Quantity shouldn't be more than 90");      }    }    let headerDiscount = orderData.cgcloud__Header_Discount_Percentage__c;    headerDiscount = headerDiscount && Number(headerDiscount);    if (headerDiscount && headerDiscount > 15) {      throw new Error("Header discount can't be greater than 15");    }    let deliveryDate = orderData.cgcloud__Delivery_Date__c;    if (deliveryDate) {      deliveryDate = new Date(deliveryDate);      deliveryDate.setDate(deliveryDate.getDate() + 1);      orderData.cgcloud__Delivery_Date__c = deliveryDate;    }  }}