Salesforce Order Create

Registers new order information in Salesforce specifying the Client and order details. Stores Order ID and communication logs into String type Data Item.
Salesforce Order Create
salesforce-order-create-20171020-addonxml
(md5:6b907af8193fbd414b2b5579c3e3114f)

    • C’: Client ID stored in the selected Data Item C
    • D’: Order Effective date stored in the selected Data Item D
    • E’: Status of Order stored in the selected Data Item E
    • F’: Price list ID stored in the selected Data Item F
    • G’: Item ID stored in the selected Data Item G
    • H’: Unit price stored in the selected Data Item H
    • I’: Quantity stored in the selected Data Item I
    • Order information will be registered in Salesforce based on information of the client, product etc.
    • The order ID will be stored in the data item selected at J
    • The communication logs will be stored in the data item selected at X
    • Please specify the item with the ID of the item registered in the specified price list

    A. Set OAuth2 Config Name (at [OAuth 2.0 Setting])Single-line inputRequired
    B. Set Sub-Domain (https://{subdomain}.salesforce.com/)Single-line inputRequired
    C. Select STRING/SELECT DATA for an Account IDData selectString(single line)orSelect(radio/select/search)Required
    D. Select DATE DATA for Order Effective DateData selectDate
    E. Select STRING/SELECT DATA for StatusData selectString(single line)orSelect(radio/select/search)
    F. Set Price List IDSingle-line inputRequired
    G. Select SELECT DATA for ItemData selectSelect(radio/select/search)Required
    H. Select NUMERIC DATA for Unit PriceData selectNumericRequired
    I. Select NUMERIC DATA for QuantityData selectNumericRequired
    J. Select STRING DATA for Order ID (update)Data selectString(single line)
    X. Select STRING DATA for Access Log (update)Data selectString(multiple line)
    -variable namevariable label
    Aconf_OAuth2Set OAuth2 Config Name (at [OAuth 2.0 Setting])
    Bconf_SubDomainSet Sub-Domain (https://{subdomain}.salesforce.com/)
    Cconf_DataIdCSelect STRING/SELECT DATA for an Account ID
    Dconf_DataIdDSelect DATE DATA for Order Effective Date
    Econf_DataIdESelect STRING/SELECT DATA for Status
    Fconf_DataIdFSet Price List ID
    Gconf_DataIdGSelect SELECT DATA for Item
    Hconf_DataIdHSelect NUMERIC DATA for Unit Price
    Iconf_DataIdISelect NUMERIC DATA for Quantity
    Jconf_DataIdJSelect STRING DATA for Order ID (update)
    Xconf_DataIdXSelect STRING DATA for Access Log (update)
    // Salesforce Order Create (ver. 20171020)
    // (c) 2017, Questetra, Inc. (the MIT License)
    
    // 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 ); }