Newer Version Available

This content describes an older version of this product. View Latest

Retail Order Save Customization Use Cases

Here are the different Apex customization use cases for the retail order save process.

Change a managed package sObject field value

Here’s a sample Apex class that modifies the custom field text of an order.

1global class RetailOrderSampleSaveCustomization implements System.Callable {
2  public Object call(String m, Map<String, Object> params) {
3    // Get the RE_Order instance
4    cgcloud.RE_Order orderWrapper = (cgcloud.RE_Order) params.get('order');
5
6    // Update the Order__c SObject field
7    cgcloud__Order__c order = (cgcloud__Order__c) orderWrapper.getOrder()
8      .getRecord();
9    // Customer_Order_Id__c field is part of the cgcloud managed package.
10    order.cgcloud__Customer_Order_Id__c = 'customer-order-id';
11
12    // Get the First Order_Item__c and update its Custom__c field value
13    cgcloud__Order_Item__c orderItem = (cgcloud__Order_Item__c) orderWrapper.getOrderItems()[0]
14      .getRecord();
15    // Custom__c is a custom field created by the customer.
16    orderItem.Custom__c = 'My New custom text';
17
18    return null;
19  }
20}

Add a custom sObject

Here’s a sample Apex class that adds an additional sObject record to the order that is saved.

1global class RetailOrderSaveCustomization implements System.Callable {
2  public Object call(String m, Map<String, Object> params) {
3    // Get the RE_Order instance
4    cgcloud.RE_Order orderWrapper = (cgcloud.RE_Order) params.get('order');
5
6    // Get the Order__c SObject
7    cgcloud__Order__c order = (cgcloud__Order__c) orderWrapper.getOrder()
8      .getRecord();
9
10    /**
11     * MyCustomSObject__c is a custom object and it has a Custom__c field.
12     * Use append method so that this new SObject will be created as part of the order save process.
13     */
14    MyCustomSObject__c mySObject = new MyCustomSObject__c();
15    mySObject.Custom__c = order.cgcloud__Customer_Order_Id__c;
16    cgcloud.RE_Order.Record myRecordWrapper = orderWrapper.append(mySObject);
17
18    return null;
19  }
20}

The RE_Order class ensures that all the order-related sObjects are committed in an all-or-none fashion while encapsulating all operations in a transaction.

Note

Add a custom sObject and define a parent-child relationship

Here’s a sample Apex class that adds a custom sObject and relates it to the parent sObject.

1global class RetailOrderSaveCustomization implements System.Callable {
2  public Object call(String m, Map<String, Object> params) {
3    cgcloud.RE_Order orderWrapper = (cgcloud.RE_Order) params.get('order');
4    cgcloud__Order__c order = (cgcloud__Order__c) orderWrapper.getOrder().getRecord();
5
6    // Append a new SObject to be saved
7    MyCustomSObject__c mySObject = new MyCustomSObject__c();
8    cgcloud.RE_Order.Record myRecordWrapper = orderWrapper.append(mySObject);
9
10    // Add relationship to the Order__c record
11    orderWrapper.addRelationship(
12      myRecordWrapper,
13      MyCustomSObject__c.Order__c,
14      orderWrapper.getOrder()
15    );
16
17    // Append an additional Child SObject
18    MyChildSObject__c myChildSObject = new MyChildSObject__c();
19    cgcloud.RE_Order.Record myChildRecordWrapper = orderWrapper.append(
20      myChildSObject
21    );
22
23    // Relate the child object to the custom object
24    orderWrapper.addRelationship(
25      myChildRecordWrapper,
26      MyChildSObject__c.MyCustomSObject__c,
27      myRecordWrapper
28    );
29
30    return null;
31  }
32}

Save custom data in the retail UI

Here’s a sample Apex class that saves the custom data along with the order data. In your callable class, you can access the custom state that was set as part of the order state.

1global class RetailOrderSaveCustomization implements System.Callable {
2  public Object call(String m, Map<String, Object> params) {
3    // Get the RE_Order instance
4    cgcloud.RE_Order orderWrapper = (cgcloud.RE_Order) params.get('order');
5
6    // Get the custom data serialized data
7    String payloadString = (String) params.get('customState');
8
9    if (payloadString != null && payloadString != '') {
10      Map<String, Object> customPayload = (Map<String, Object>) JSON.deserializeUntyped(
11        payloadString
12      );
13
14      // Append a new SObject to be saved
15      MyCustomSObject__c mySObject = new MyCustomSObject__c();
16      mySObject.Custom__c = (String) customPayload.get('Custom__c');
17      mySObject.Duration__c = (Integer) customPayload.get('Duration__c');
18
19      if (mySObject.Custom__c != null && mySObject.Duration__c != null) {
20        cgcloud.RE_Order.Record myRecordWrapper = orderWrapper.append(
21          mySObject
22        );
23
24        // Add relationship to the Order__c record
25        orderWrapper.addRelationship(
26          myRecordWrapper,
27          MyCustomSObject__c.Order__c,
28          orderWrapper.getOrder()
29        );
30      }
31    }
32
33    return null;
34  }
35}