Salesforce: Create Order
Registers new order information in Salesforce specifying the Client and order details. Stores Order ID and communication logs into String type Data Item.
2017 © Questetra, Inc. (MIT License)
Configs
  • A: Set OAuth2 Config Name (at [OAuth 2.0 Setting]) *
  • B: Set Sub-Domain (https://{subdomain}.salesforce.com/) *
  • C: Select STRING/SELECT DATA for an Account ID *
  • D: Select DATE DATA for Order Effective Date
  • E: Select STRING/SELECT DATA for Status
  • F: Set Price List ID *
  • G: Select SELECT DATA for Item *
  • H: Select NUMERIC DATA for Unit Price *
  • I: Select NUMERIC DATA for Quantity *
  • J: Select STRING DATA for Order ID (update)
  • X: Select STRING DATA for Access Log (update)
Script
// OAuth2 config
// Authorization Endpoint URL: https://login.salesforce.com/services/oauth2/authorize
// Token Endpoint URL: https://login.salesforce.com/services/oauth2/token
// Scope: 
// Consumer Key: (Get by Salesforce Connected App screen)
// Consumer Secret: (Get by Salesforce Connected App screen)

//// == Config Retrieving / 工程コンフィグの参照 ==
var oauth2 = configs.get( "conf_OAuth2" ) + "";
var subDomain = configs.get( "conf_SubDomain" ) + "";
var dataIdC = configs.get( "conf_DataIdC" ) + ""; //AccountId
var dataIdD = configs.get( "conf_DataIdD" ) + ""; //EffectiveDate
var dataIdE = configs.get( "conf_DataIdE" ) + ""; //Status
var priceBook = configs.get( "conf_DataIdF" ) + ""; //Pricebook2Id
var dataIdG = configs.get( "conf_DataIdG" ) + ""; //ItemId
var dataIdH = configs.get( "conf_DataIdH" ) + ""; //UnitPrice
var dataIdI = configs.get( "conf_DataIdI" ) + ""; //Quantity
var dataIdJ = configs.get( "conf_DataIdJ" ) + ""; //Order Id
var dataIdX = configs.get( "conf_DataIdX" ) + ""; //Access Log
// convet 'java.lang.String' to 'javascript string'

//// == Data Retrieving / ワークフローデータの参照 ==
var account = engine.findDataByNumber( dataIdC ) + ""; // SELECT_SINGLE or STRING_TEXTFIELD
if( engine.findDataDefinitionByNumber( dataIdC ).matchDataType( "SELECT_SINGLE" ) ){
    account = engine.findDataByNumber( dataIdC ).get(0).getValue() + "";
}else if( engine.findDataDefinitionByNumber( dataIdC ).matchDataType( "STRING_TEXTFIELD" ) ){
    account = engine.findDataByNumber( dataIdC ) + "";
} 
var effectiveDate = engine.findDataByNumber( dataIdD ) + "";
var status = engine.findDataByNumber( dataIdE ).get(0).getValue() + ""; //Status

var itemId = engine.findDataByNumber( dataIdG ) + ""; // SELECT_SINGLE or STRING_TEXTFIELD
if( engine.findDataDefinitionByNumber( dataIdG ).matchDataType( "SELECT_SINGLE" ) ){
    itemId = engine.findDataByNumber( dataIdG ).get(0).getValue() + "";
}else if( engine.findDataDefinitionByNumber( dataIdG ).matchDataType( "STRING_TEXTFIELD" ) ){
    itemId = engine.findDataByNumber( dataIdG ) + "";
} 
var unitPrice = engine.findDataByNumber( dataIdH) + "";
var quantity = engine.findDataByNumber( dataIdI ) + "";
// convet 'java.lang.String' to 'javascript string'


//// == Calculating / 演算 ==
// preparing for API Request
var apiRequest = httpClient.begin(); // HttpRequestWrapper
// com.questetra.bpms.core.event.scripttask.HttpClientWrapper

// preparing for API Request (OAuth2 Token, HTTP Basic Auth, etc)
var token = httpClient.getOAuth2Token( oauth2 );

// preparing for API Request (Path parameters)
var apiUri = "https://" + subDomain + ".salesforce.com/services/data/v40.0/commerce/sale/order/";

// preparing for API Request (Query parameters)
// (no set)

// preparing for API Request (JSON Body, Form Parameters)
var requestObj = {};
requestObj.order = [];
requestObj.order[0] = {};
requestObj.order[0].attributes = {"type": "Order"};
requestObj.order[0].EffectiveDate = effectiveDate;
requestObj.order[0].Status = status;
requestObj.order[0].Pricebook2Id = priceBook;
requestObj.order[0].accountId = account;
requestObj.order[0].OrderItems = {};
requestObj.order[0].OrderItems.records = [];
requestObj.order[0].OrderItems.records[0] = {};
requestObj.order[0].OrderItems.records[0].attributes = {"type": "OrderItem"};
requestObj.order[0].OrderItems.records[0].PricebookEntryId = itemId;
requestObj.order[0].OrderItems.records[0].quantity = quantity;
requestObj.order[0].OrderItems.records[0].UnitPrice = unitPrice;

var requestJson = JSON.stringify( requestObj );

// Request to the API (POST, GET, PUT, etc)
var response = apiRequest.bearer( token )
  .body( requestJson, "application/json")
  .post(apiUri);

var accessLog = "";
var responseJson = "";
accessLog += "---POST request--- " + response.getStatusCode() + "\n";
responseJson = response.getResponseAsString();

// Retrieve Properties from Response-JSON
if( response.getStatusCode() == 201 ){
  var jsonObj = JSON.parse( responseJson );
  var orderId = jsonObj.records[0].Id + "";
  engine.setDataByNumber( dataIdJ, orderId );
}

// Error Handling
// (no action)

//for Debug
accessLog += "---requestJson---\n" + requestJson + "\n";
accessLog += "---responseJson---\n" + responseJson + "\n";

//// == Data Updating / ワークフローデータへの代入 ==
if ( dataIdX !== "" ){ engine.setDataByNumber( dataIdX, accessLog ); }

Download

Capture

Notes

  • Please specify the item with the ID of the item registered in the specified price list
%d bloggers like this: