Order Management拡張でのREST APIの使用
拡張機能がREST APIをコールするときに、次のガイドラインを適用します。
コネクタの作成
-
「ホーム」>「自分の企業」>「設定および保守」>「外部インタフェースWebサービス詳細の管理」に移動します。
このタスクを使用する必要があります。
-
次の値を設定します。
属性
値
コネクタ名
QUERYPARAM
コネクタURL
https://host:port/fscmRestApi/resources/latest/itemsV2
ユーザー名
REST APIへのアクセスに使用するユーザー名を入力します。
パスワード
REST APIへのアクセスに使用するパスワードを入力します。
起動モード
同期サービス
添付の送信 いいえ レスポンス処理オプション デフォルト値を使用します。 詳細は、「オーダー管理と履行システム間のコネクタ詳細の管理」を参照してください。
-
拡張機能でinvokeRestServiceメソッドを使用して、REST APIをコールします。
詳細と例については、「Oracle Supply Chain Management CloudのREST API」に移動し、Order Managementを展開して、「オーダー・ハブの販売オーダー」をクリックします。
invokeRestServiceを使用したREST APIのコール
Order Managementでは、拡張で送信する引数に従って、正しいバージョンが自動的に選択されます。
JSONオブジェクト
JSONオブジェクトをスクリプト化する場合は、次の形式を使用します。
invokeRestService(String integrationName,
String restResourcePath,
JSONObject jsonObj,
String method,
Map<String, String> headers,
Map<String, Object> queryParams,
Map<String, Object> pathParams);
たとえば:
def response = context.invokeRestService("ItemsV2", "", null, "GET", headers, queryParams, pathParams);
def response = context.invokeRestService("InventoryReservations", "", jsonObject, "POST", headers, queryParams, pathParams);
JSON配列
この形式は、JSON配列をスクリプト化する場合に使用します。
invokeRestService(String integrationName,
String restResourcePath,
JSONArray jsonArray,
String method,
Map<String, String> headers,
Map<String, Object> queryParams,
Map<String, Object> pathParams) ;
たとえば:
def response = context.invokeRestService("RestPatchConnector", "", jsonArrayObject, "PATCH", headers, queryParams, pathParams);
RESTリクエスト
invokeRestServiceのリクエストでは、次のパラメータを使用できます。
| ポジション | パラメータ | タイプ | 説明 |
|---|---|---|---|
| 1 | ConnectorName | 文字列 | コネクタの名前(QUERYPARAMなど)。 |
| 2 | restResourcePath | 文字列 | プレースホルダ。 空の文字列を使用してください: "。 コネクタにこの値を含める必要がありますが、拡張のロジックは無視します。 |
| 3 | JsonObj | JSONオブジェクト |
リクエストにJSONのオブジェクト・ペイロードを含めます。 ペイロードが必要ない場合は、空のままにします。 たとえば、GETリクエストの場合です。 |
| 3 | JsonArray | JSON配列 | リクエストにJSONの配列ペイロードを含めます。 |
| 4 | 操作 | 文字列 |
次のいずれかのHTTPメソッドを指定します。
|
| 5 | ヘッダー | マップ string、 string | HTTPヘッダーの値ペアを含めます。 |
| 6 | QueryParams | マップ string、 object | URLに追加するURL問合せパラメータを指定します。 たとえば、status=ACTIVEです。 |
| 7 | PathParams | マップ string、 object |
URLパスの一部を置換するために使用する変数を指定します。 たとえば:
|
RESTレスポンス
invokeRestServiceのレスポンスで次のメソッドを使用して、データを抽出し、ロジックを実装します。
| 方法 | タイプ | 説明 |
|---|---|---|
| getStatus() | 文字列 | コールの結果。 SUCCESSまたはERRORが含まれます。 |
| getResponseStatusCode() | 整数 |
標準HTTPステータス・コード。 たとえば、OKの場合は200、Createdの場合は201、Bad Requestの場合は400です。 |
| getPayload() | JSONオブジェクト | RESTリクエストで送信したJSONオブジェクトが含まれます。 |
| getPayloadArray() | JSON配列 | RESTリクエストで送信したJSON配列が含まれます。 |
| getResponse() | 文字列 | ステータスがSUCCESSの場合、レスポンス・ペイロードが含まれます。 |
| getErrorMessage() | 文字列 | ステータスがERRORの場合、レスポンス・ペイロードが含まれます。 |
例の取得
この例では、ItemsV2という名前のコネクタを使用して品目詳細を取得し、その応答を出荷指示属性に書き込みます。 次のコネクタURLを使用します。
https://host:port/fscmRestApi/resources/latest/itemsV2
拡張スクリプトはinvokeRestServiceで7つのパラメータを使用し、GETペイロードでnull値を使用します。
実行時に次のURLが作成されます。
https://host:port/fscmRestApi/resources/latest/itemsV2? q=ItemNumber=AS54888;OrganizationCode=V1&onlyData=true&fields=ItemNumber,ItemDescription
拡張機能のスクリプトは次のとおりです。
import oracle.apps.scm.doo.common.extensions.ValidationException;
import oracle.apps.scm.doo.common.extensions.Message;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException
import org.json.simple.JSONObject;
// 1. Filter the condition:
def poNumber = header.getAttribute("CustomerPONumber");
if (poNumber == null || poNumber != "ItemsV2") return;
String connectorName = "ItemsV2";
// 2. Specify the query parameters:
// Matches: ?q=ItemNumber=AS54888;OrganizationCode=V1&onlyData=true&fields=ItemNumber,ItemDescription
Map<String, Object> queryParams = new HashMap<>();
queryParams.put("q", "ItemNumber=AS54888;OrganizationCode=V1");
queryParams.put("onlyData", "true");
queryParams.put("fields", "ItemNumber,ItemDescription");
//Map string, object pathParams = new HashMap<>();
//pathParams.put("itemsV2UniqID", "00040000000EACED0005770800005B0A5D2711C80000000EACED000577");
//Assumes you want to use this connector url: https://host:port/fscmRestApi/resources/latest/itemsV2/{itemsV2UniqID}/child/ItemDFF
// 3. Use a GET request. Parameter 3 is empty for a GET:
def response = context.invokeRestService(connectorName, "", null, "GET", null, queryParams, null);
// 4. Process the result:
def status = response.getStatus();
def data = "Response Status : " + status + ", Response Status Code : " + response.getResponseStatusCode() + ". ";
if ("SUCCESS".equals(status)) {
def resp = response.getResponse(); // Access the response payload
// See whether the "items" in the array contain data:
if (resp.get("items") != null && resp.get("items").length > 0) {
// Extract data from the first item in the array
def itemRow = resp.get("items")[0];
def itemInfo = itemRow.get("ItemNumber") + "-" + itemRow.get("ItemDescription");
data = data + itemInfo;
} else {
data = data + "Could not fetch item information using connector " + connectorName + " with query parameters " + queryParams;
}
} else { // ERROR
// Capture error details:
data = data + " ErrorMessage : " + response.getErrorMessage() + " Response : " + response.getResponse();
}
// 5. Update Order Header
header.setAttribute("ShippingInstructions", data);
POSTの例
この例では、InventoryReservationsという名前のコネクタを使用して、インベントリ予約を作成します。 次のコネクタURLを使用します。
https://host:port/fscmRestApi/resources/latest/inventoryReservations/
拡張スクリプトはinvokeRestServiceで7つのパラメータを使用し、POSTリクエストにJSONペイロード値を含めます。
実行時に次のURLが作成されます。
https://host:port/fscmRestApi/resources/latest/itemsV2? q=ItemNumber=AS54888;OrganizationCode=V1&onlyData=true&fields=ItemNumber,ItemDescription
拡張機能のスクリプトは次のとおりです。
import oracle.apps.scm.doo.common.extensions.ValidationException;
import oracle.apps.scm.doo.common.extensions.Message;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
// 1. Filter the condition:
def poNumber = header.getAttribute("CustomerPONumber");
if (poNumber != "InventoryReservations") return;
//Map string, object pathParams = new HashMap<>();
//Map string, object queryParams = new HashMap<>();
Map<String, String> headers = new HashMap<>();
headers.put("REST-Framework-Version", "4");
// 2. Create the JSON Object's request payload:
JSONObject jObj = new JSONObject();
jObj.put("OrganizationId", 204);
jObj.put("ItemNumber", "AS54888");
jObj.put("ReservationQuantity", 10000000);
jObj.put("ReservationUOMCode", "Ea");
jObj.put("SupplySourceType", "On Hand");
jObj.put("DemandSourceType", "User Defined");
jObj.put("DemandSourceName", "27210");
// 3. Send a POST request:
def response = context.invokeRestService("InventoryReservations", "", jObj, "POST", headers, null, null);
// As an option, you can read all the attributes from the response:
List<Message> msgs = new ArrayList<Message>();
msgs.add(new Message( Message.MessageType.WARNING, "Input Payload : " + response.getPayload() ));
msgs.add(new Message( Message.MessageType.WARNING, "Input Payload Array : " + response.getPayloadArray() ));
msgs.add(new Message( Message.MessageType.WARNING, "Response Status : " + response.getStatus() ));
msgs.add(new Message( Message.MessageType.WARNING, "Response Status Code : " + response.getResponseStatusCode() ));
msgs.add(new Message( Message.MessageType.WARNING, "Error Message : " + response.getErrorMessage() ));
msgs.add(new Message( Message.MessageType.WARNING, "Success Message : " + response.getResponse() ));
// 4. Evaluate the response:
def statusCode = response.getResponseStatusCode();
if (statusCode == 200 || statusCode == 201) {
// Success. Get the specific ID from the response:
def successMessage = response.getResponse();
msgs.add(new Message(Message.MessageType.WARNING, "Reservation done successfully with ReservationID : " + successMessage.get("ReservationId")));
} else {
// Error. Parse the error details from the service:
def errorMessage = response.getErrorMessage();
msgs.add(new Message(Message.MessageType.ERROR, "Error Message - " + errorMessage));
if ( errorMessage != null && errorMessage.trim().startsWith("{")) {
JSONParser parser = new JSONParser();
JSONObject root = (JSONObject) parser.parse(errorMessage);
// Use errorDetails to navigate through the error structure.
// The structure and the node names depend on what's in the REST response:
JSONArray errorDetails = (JSONArray) root.get("o:errorDetails");
if (errorDetails != null && !errorDetails.isEmpty()) {
JSONObject errorInfo = (JSONObject) errorDetails.get(0);
def errorCode = errorInfo.get("o:errorCode");
def detail = errorInfo.get("detail");
msgs.add(new Message(Message.MessageType.ERROR, "ErrorCode - " + errorCode + ". ErrorDetail - " + detail));
}
}
}
// 5. Send the collected messages to the caller:
if(!msgs.isEmpty()) throw new ValidationException(msgs);