Wednesday, September 16, 2020

Sitecore Experience Commerce - Steps to update custom values through - GetEntityView and DoUxAction APIs.

There are a few options to extend the Sitecore commerce entities,  OOTB Sitecore XC provided below entities.

  1.  Sitecore.Commerce.Plugin.Catalog.SellableItem
  2.  Sitecore.Commerce.Plugin.Catalog.Catalog
  3.  Sitecore.Commerce.Plugin.Catalog.Category
  4.  Sitecore.Commerce.Plugin.Promotions.PromotionBook
  5.  Sitecore.Commerce.Plugin.Promotions.Promotion
  6.  Sitecore.Commerce.Plugin.Pricing.PriceBook
  7.  Sitecore.Commerce.Plugin.Pricing.PriceCard
  8.  Sitecore.Commerce.Plugin.Customers.Customer
  9.  Sitecore.Commerce.Plugin.Orders.Order

If you are working in the commerce world, you will see a requirement to extend these entities, the requirement can be an example below.

1,  Add invoice link to the order - Add a new field on order entities to show the invoice link or status from the backend system.

2. Add a new field on customer entities to show the preferred time for the call, Membership type like gold, silver etc.

3. Extend the sellable item (The most common one) to add more fields like custom properties for Color, notes etc.

For updating the order usually, we should use the minion jobs to read the current status and update from the backend system but sometimes you need to update those details from the outer side. Here are a few easy tips.

To update any field values of entities like Order, Customer you can use the GetEntityVIew to get the Entry and 

URL will be like this  

https://commerceauthoring.XX.dev.com/api/GetEntityView()- h

Request input-

{  
  "entityId": "Entity-Customer-f26e9063ba38461bafcf97fe4a115830",  
  "viewName": "EditView",  
  "forAction": "EditView",  
  "itemId": "Composer-82bd424ddb274945912dca6fb71cea4b"  
 }  
We need to pass the Customer or Entity and name of the Composer template-

The response will be an complete entity, We can keep the response in an object and update the value and after that can call the

 https://commerceauthoring.XXX.com.dev.com/api/DoUxAction()

and post the updated view, that will update the value in the entiry.

 {  
  "@odata.context": "https://commerceauthoring.XXX.com.dev.com/Api/$metadata#Sitecore.Commerce.EntityViews.EntityView",  
  "@odata.type": "#Sitecore.Commerce.EntityViews.EntityView",  
  "Name": "EditView",  
  "Policies": [  
  ],  
  "DisplayName": "Edit View",  
  "EntityId": "Entity-SellableItem-demohandtowel1",  
  "EntityVersion": 4,  
  "Action": "EditView",  
  "ItemId": "Composer-82bd424ddb274945912dca6fb71cea4b",  
  "VersionedItemId": "Composer-82bd424ddb274945912dca6fb71cea4b-4",  
  "SelectedChildView": {  
   "Name": "",  
   "Policies": [  
   ]  
  },  
  "DisplayRank": 500,  
  "UiHint": "Flat",  
  "Icon": "chart_column_stacked",  
  "Properties": [  
   {  
    "Name": "Version",  
    "Policies": [  
    ],  
    "DisplayName": "Version",  
    "Value": "1",  
    "IsHidden": true,  
    "OriginalType": "System.Int32",  
    "IsReadOnly": true,  
    "UiType": "",  
    "IsRequired": true  
   },  
   {  
    "Name": "LaunchDate",  
    "Policies": [  
    ],  
    "DisplayName": "Launch Date",  
    "Value": "2020-09-16T14:26:00.5386742+00:00",  
    "IsHidden": false,  
    "OriginalType": "System.DateTimeOffset",  
    "IsReadOnly": false,  
    "UiType": "",  
    "IsRequired": true  
   },  
   {  
    "Name": "DealerName",  
    "Policies": [  
    ],  
    "DisplayName": "Dealer Name",  
    "Value": "",  
    "IsHidden": false,  
    "OriginalType": "System.String",  
    "IsReadOnly": false,  
    "UiType": "",  
    "IsRequired": true  
   }  
  ],  
  "ChildViews": [  
  ]  
 }  
the get GetEntityView will always create a new version in case of edit, so there would be any issue for the concurrency, the same request response can be used to update the entity with the latest values.

Note:- Although this is not a recommended option. 
Ideally, all these changes should go inside the commerce engine.

This information is just an example if there is any need to update data from the external API or need for front end extension.




No comments:

Post a Comment