NetSuite RESTlet のコールの例

この例では次の方法を示します。

  • NetSuite インスタンスにデプロイされた NetSuite RESTlets をコールして顧客レコードを作成、取得、削除するスクリプトを作成する

  • RESTlet スクリプトをデプロイする

  • NetSuite に接続してアプリケーションをデプロイするようにアプリケーションのプロパティを設定する

始める前に

この例を実行するには、以下が必要です。

  • Java 8、11、または 17

  • Anypoint Studio 7.0.x

  • Mule 4.2.1 以降

  • DataWeave 2.0

  • NetSuite インスタンスへのアクセス権と ​mule-app.properties​ ファイル内のログイン情報

スクリプトを作成して RESTlet としてデプロイする

顧客レコードを作成、取得、削除するスクリプトを作成してデプロイします。

  1. サンプル JavaScript スクリプトを作成します。

    // Get a standard NetSuite record
    function getRecord(datain)
    {
        return nlapiLoadRecord(datain.recordtype, datain.id);
        // for example, recordtype="customer", id="769"
    }
    
    // Create a standard NetSuite record
    
    function createRecord(datain)
    {
        var err = new Object();
    
        // Validate if mandatory record type is set in the request
        if (!datain.recordtype)
        {
            err.status = "failed";
            err.message= "missing recordtype";
            return err;
        }
    
        var record = nlapiCreateRecord(datain.recordtype);
    
        for (var fieldname in datain)
        {
          if (datain.hasOwnProperty(fieldname))
          {
            if (fieldname != 'recordtype' && fieldname != 'id')
            {
              var value = datain[fieldname];
                if (value && typeof value != 'object')
                // ignore other type of parameters
                {
                  record.setFieldValue(fieldname, value);
                }
            }
          }
        }
        var recordId = nlapiSubmitRecord(record);
        nlapiLogExecution('DEBUG','id='+recordId);
        var nlobj = nlapiLoadRecord(datain.recordtype,recordId);
        return nlobj;
    }
    
    // Delete a standard NetSuite record
    function deleteRecord(datain)
    {
      nlapiDeleteRecord(datain.recordtype, datain.id);
      // for example: recordtype="customer", id="769"
    
    }
  2. アカウントの SuiteScript と NetSuite Web サービスを有効にします。

    1. NetSuite にログインします。

    2. [Set Up (セットアップ)] > [Company (会社)] > [Enable Features (機能の有効化)] > [SuiteCloud]​ をクリックします:

      SuiteScript の有効化ページ
  3. 以前に作成した​ファイルをアップロードします。

    1. [Customization (カスタマイズ)] > [Scripting (スクリプト)] > [Scripts (スクリプト)] > [New (新規)]​ に移動します。

    2. スクリプトファイルを選択し、​[Create Script Record (スクリプトレコードの作成)]​ をクリックし、​[RESTlet]​ を選択します。

      スクリプト種別の選択ページ
  4. アップロードしたスクリプトの内容を使用してフォームに入力し、スクリプトをデプロイします。

    スクリプトの設定ページ
  5. 次のページを表示するオーディエンスを選択します。

    スクリプトのデプロイページ
  6. 外部 URL に表示されているスクリプト番号とデプロイ番号を書き留めておきます。これらは、後で RESTlet をコールするために必要です。

Mule プロジェクトを作成する

Studio で新しい Mule プロジェクトを作成し、NetSuite に接続するためのログイン情報、デプロイしたスクリプト、デプロイ ID を設定します。

  1. Studio で、​[File (ファイル)] > [New (新規)] > [Mule Project (Mule プロジェクト)]​ を選択します。

  2. Mule プロジェクトの名前を入力して、​[Finish (完了)]​ をクリックします。

  3. Package Explorer​ で、作成した Studio プロジェクトに配置されている ​src/main/resources/mule-app.properties​ ファイルを開きます。

  4. 次の値を設定します。

    netsuite.email=
    netsuite.password=
    netsuite.account=
    netsuite.roleId=
    netsuite.applicationId=
    netsuite.subsidiary=
    netsuite.script=
    netsuite.deploy=

    netsuite.script​ と ​netsuite.deploy​ の値は、​「スクリプトを作成して RESTlet としてデプロイする」​の最後のステップで書き留めた値です。

コネクタを Mule プロジェクトに追加する

NetSuite Connector を Mule プロジェクトに追加して、XML コードにコネクタの名前空間およびスキーマの場所を自動的に入力し、プロジェクトの ​pom.xml​ ファイルに必須の連動関係を追加します。

  1. [Mule Palette (Mule パレット)]​ ビューで、​[(X) Search in Exchange ((X) Exchange 内を検索)]​ をクリックします。

  2. [Add Modules to Project (モジュールをプロジェクトに追加)]​ で、検索項目に「​netsuite​」と入力します。

  3. [Available modules (使用可能なモジュール)]​ で、そのコネクタ名をクリックします。

  4. [Add (追加)]​ をクリックします。

  5. [Finish (完了)]​ をクリックします。

アプリケーションフローを作成する

  1. Studio キャンバスで ​Configuration XML (設定 XML)​ をクリックします。

  2. <?xml version="1.0" encoding="UTF-8"?>​ 行の後のすべての内容を削除します。

  3. 次の XML をコピーして、​<?xml version="1.0" encoding="UTF-8"?>​ 行の後に貼り付けます。

    <mule xmlns:netsuite-restlet="http://www.mulesoft.org/schema/mule/netsuite-restlet"
          xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
          xmlns:http="http://www.mulesoft.org/schema/mule/http"
          xmlns="http://www.mulesoft.org/schema/mule/core"
          xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
    http://www.mulesoft.org/schema/mule/netsuite-restlet http://www.mulesoft.org/schema/mule/netsuite-restlet/current/mule-netsuite-restlet.xsd">
    	<configuration-properties file="mule-app.properties" doc:name="Configuration properties"/>
    	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
    		<http:listener-connection host="0.0.0.0" port="8081" />
    	</http:listener-config>
    	<netsuite-restlet:rest-config name="NetSuite_Rest_config" doc:name="NetSuite Rest config" >
    		<netsuite-restlet:restlet-token-connection
    				consumerKey="${netsuite.consumerKey}"
    				consumerSecret="${netsuite.consumerSecret}"
    				tokenId="${netsuite.tokenId}"
    				tokenSecret="${netsuite.tokenSecret}"
    				account="${netsuite.account}"
    				readTimeout="${netsuite.readTimeout}"
    				connectionTimeout="${netsuite.connectTimeout}"/>
    	</netsuite-restlet:rest-config>
    	<flow name="html-form-flow">
            <http:listener config-ref="HTTP_Listener_config" path="/" doc:name="/"/>
            <parse-template location="form.html" doc:name="Parse Template"/>
        </flow>
        <flow name="restletGet">
            <http:listener config-ref="HTTP_Listener_config" path="/get" doc:name="/get"/>
            <ee:transform doc:name="Transform GET Input" >
    			<ee:message >
    				<ee:set-payload ><![CDATA[%dw 2.0
    output application/java
    ---
    {
        "id": attributes.queryParams.id,
        "recordtype": attributes.queryParams.recordtype
    }]]></ee:set-payload>
    			</ee:message>
    		</ee:transform>
            <netsuite-restlet:call-restlet-get config-ref="NetSuite_Rest_config" script="${netsuite.script}" deploy="${netsuite.deploy}" doc:name="Call RESTlet (GET)" />
            <ee:transform doc:name="to JSON" >
    			<ee:message >
    				<ee:set-payload ><![CDATA[%dw 2.0
    output application/json
    ---
    payload]]></ee:set-payload>
    			</ee:message>
    		</ee:transform>
            <logger level="INFO" doc:name="Logger"/>
        </flow>
        <flow name="restletPost">
            <http:listener config-ref="HTTP_Listener_config" path="/post" doc:name="/post"/>
            <ee:transform doc:name="Transform POST Input" >
    			<ee:message >
    				<ee:set-payload ><![CDATA[%dw 2.0
    output application/java
    ---
    payload]]></ee:set-payload>
    			</ee:message>
    		</ee:transform>
            <netsuite-restlet:call-restlet-post config-ref="NetSuite_Rest_config" deploy="${netsuite.deploy}" script="${netsuite.script}" doc:name="NetSuite RESTlet (POST)"/>
            <ee:transform doc:name="to JSON" >
    			<ee:message >
    				<ee:set-payload ><![CDATA[%dw 2.0
    output application/json
    ---
    payload]]></ee:set-payload>
    			</ee:message>
    		</ee:transform>
            <logger level="INFO" doc:name="Logger"/>
        </flow>
        <flow name="restletDelete">
            <http:listener config-ref="HTTP_Listener_config" path="/delete" doc:name="/delete"/>
            <ee:transform doc:name="Transform DELETE Input" >
    			<ee:message >
    				<ee:set-payload ><![CDATA[%dw 2.0
    output application/java
    ---
    {
        "id": attributes.queryParams.id,
        "recordtype": attributes.queryParams.'recordtype'
    }]]></ee:set-payload>
    			</ee:message>
    		</ee:transform>
            <netsuite-restlet:call-restlet-delete config-ref="NetSuite_Rest_config" deploy="${netsuite.deploy}" script="${netsuite.script}" doc:name="NetSuite RESTlet (DELETE)"/>
            <set-payload value="Record deleted successfully" doc:name="Set Payload"/>
            <logger  level="INFO" doc:name="Logger"/>
        </flow>
    	</mule>
  4. プロジェクトを保存します。

フローについて

  1. html-form​ フローでは、​parseTemplate​ コンポーネントを使用して HTML フォームを表示します。

    HMTL フォーム Studio フロー
  2. restletGet​ フローでは、RESTlet の GET 関数をコールします。

    Restlet Get Studio フロー
  3. restletPost​ フローでは、RESTlet の POST 関数をコールします。

    Restlet Post Studio フロー
  4. restletDelete​ フローでは、RESTlet の DELETE 関数をコールします。

    Restlet Delete Studio フロー

アプリケーションを実行する

アプリケーションを実行、デプロイ、確認します。
  1. プロジェクトのキャンバスの下部にある ​[Global Elements (グローバル要素)]​ をクリックします。

  2. [Global Configuration Elements (グローバル設定要素)]​ で、​[NetSuite Rest config (NetSuite Rest 設定)]​ を選択して ​[Edit (編集)]​ をクリックします。

  3. [Test Connection (接続をテスト)]​ をクリックして、Sandbox と接続されていることを確認します。

    成功メッセージが表示されます。

    [Test connection successful (テスト接続成功)] メッセージ
  4. Package Explorer​ でプロジェクト名をクリックし、​[Run (実行)] > [Run As (別のユーザーとして実行)] > [Mule Application (Mule アプリケーション)]​ をクリックします。
    コンソールで、​Mule is up and kicking​ のメッセージを探して、アプリケーションが正常に開始されたことを確認します。

  5. ブラウザーを開き、URL ​http://localhost:8081​ にアクセスします。
    アプリケーションがデプロイされたことを確認できます。

    デモページ