Tracking orders using Salesforce B2C Commerce Cloud
An example below provides a minimalistic module implementation using HTTPClient. The implementation relies on a driveCommerceOrderTrackingApiKey site prefers which is expected to contain a value with API having Runtime Order Tracking permissions.
JS
var HTTPClient = require('dw/net/HTTPClient');
var Logger = require('dw/system/Logger');
var Site = require('dw/system/Site');
/**
* Creates order-to-recipe associations using simple HTTP client.
* @param {Array} associations - Array of order-to-recipe association objects
* @returns {Object} - Result of the API call
*/
function createOrderRecipeAssociations(associations) {
var result = {
error: false,
errorMessage: null,
response: null
};
try {
if (!Array.isArray(associations) || associations.length === 0) {
return;
}
var apiKey = Site.getCurrent().getCustomPreferenceValue('driveCommerceOrderTrackingApiKey');
if (!apiKey) {
throw new Error('Drive Commerce API Key is not configured in site preferences');
}
// Create HTTP client
var httpClient = new HTTPClient();
var url = 'https://api.customizer.drivecommerce.com/api/v2/recipe/order?apikey=' + encodeURIComponent(apiKey);
httpClient.open('POST', url);
// Set timeout to 1 second.
httpClient.setTimeout(1000);
httpClient.setRequestHeader('Content-Type', 'application/json');
httpClient.send(JSON.stringify(associations));
// No meaningful request response to be processed.
} catch (e) {
result.error = true;
result.errorMessage = e.message;
Logger.error('Exception while calling Customizer order tracking API: {0}', e.message);
}
return result;
}
/**
* Example of usage:
*
* var associations = [
* {
* "recipeId": "DRIVE-001",
* "orderNumber": "1001",
* "orderId": "1111111111111",
* "itemId": "2222222222222",
* "itemQuantity": 2
* },
* {
* "recipeId": "DRIVE-002",
* "orderNumber": "1001",
* "orderId": "1111111111111",
* "itemId": "3333333333333",
* "itemQuantity": 1
* }
* ];
*
* var result = createOrderRecipeAssociations(associations);
*
* if (result.error) {
* // Handle error
* Logger.error('Failed to create order associations: ' + result.errorMessage);
* } else {
* // Handle success
* Logger.info('Created order associations successfully');
* }
*/
module.exports = {
createOrderRecipeAssociations: createOrderRecipeAssociations
};