関数のThinklet

機能Thinkletは、JavaScriptを使用してカスタムデータ変換と計算を可能にします。 ビルトインのThinkletはほとんどのタスクを処理しますが、関数Thinkletは複雑なデータ操作やワークフローのロジックに柔軟性を提供します。

仕組み

データを変換し、インサイトを生成し、ワークフローの意思決定を自動化するために、関数ThinkletにJavaScriptコードを追加します。

使用ケース

関数Thinkletを使用するのに適したケース:

  • データ変換 – 値を変換し、データをフォーマットし、新しい指標を計算します。

  • 時間計算 – 時間の差を決定し、期間を追跡します。

  • 意思決定の自動化 – 計算された出力を使用して条件付きワークフローロジックを進めます。

JavaScriptに精通していない場合は、入力パラメータ、望ましい出力と必要な変換をChatGPTのようなAIアシスタントに説明するか、他のLLMツールを使用してください。 それにより、必要なコードが生成され、プロセスが迅速かつ簡単になります。 以下の「JavaScriptコード構造」のセクションを参照し、見本として使用してください。

関数Thinkletの設定

生データを意味のあるフォーマットに変換することで、第三者の統合を簡素化し、例えば、2つの日付の間の日数を計算することができます。作業回避策や外部ツールを使用せずに対応可能です。

  • 名前: 関数Thinkletのためのユニークな名前を入力します。

  • ID: この名前に基づいて、システムがIDを自動生成します。

  • 説明 (オプション): Thinkletの目的とその機能について説明します。 この情報は、ワークフローの他のユーザーにとって役立つでしょう。

  • パラメーター: パラメーターを追加を選択して、Thinkletのパラメーターを設定し、関数ロジックの入力パラメーターとします。

    • ID: 入力パラメーターのIDを定義します。 JavaScriptでサポートされない文字を入力することはできません。

    • :対応するパラメーター値を設定します。データベース値、他のThinkletの出力、グローバルパラメータ、またはカスタム値があります。

  • 出力:5つまでの関数の出力を設定するために、出力を追加を選択します。

    • 名前: 望ましい出力のためのユニークな名前を入力します。

    • ID:この名前に基づいてシステムがIDを自動生成します。

  • JavaScript: 入力パラメータに基づいて関数ロジックを定義するためのJavaScriptコードを記述します。 スクリプトは、定義された出力のIDを持つ出力も定義すべきです。

JavaScriptコードの構造

When you author a Function thinklet in Nexthink Flow, you should follow these syntax rules to ensure your script works correctly and remains debuggable:

  • Access input parameters defined in the configuration using inputs.parameter_ID

  • Assign output values with the syntax outputs.output_ID

  • Include useful information in the Workflow execution timeline for troubleshooting by using nxLogger.log()

Access input parameters
const param1 = inputs.parameter_ID_1;
const param2 = inputs.parameter_ID_2;
...
const paramN = inputs.parameter_ID_5; // Up to 5 parameters
Logic for transformations
outputs.output_1 = /* your logic here */;
outputs.output_2 = /* your logic here */;
...
outputs.output_5 = /* your logic here */;
Output any results for troubleshooting
nxLogger.log("Name the output" + output_1);

JavaScriptコードの例

このコード例は、関数Thinklet内でパスワードの期限切れチェッカーを実装する方法を示しています。

Access input parameter
const lastUpdate = inputs.last_update_date;

Define password validity period (90 days)
const validityDays = 90;
const expirationDate = new Date(lastUpdate);
expirationDate.setDate(expirationDate.getDate() + validityDays);

Get current date
const now = new Date();

Calculate days left
const timeDiff = expirationDate - now;
const daysLeft = Math.ceil(timeDiff / (1000 * 60 * 60 * 24));

Set outputs 
outputs.days_left = daysLeft; // Days_to_change_password
outputs.if_expired = daysLeft <= 0; // Already_expired

Output intermediate results for troubleshooting
nxLogger.log("Days left: " + daysLeft);
nxLogger.log("Need to notify: " + outputs.output_2);

Last updated

Was this helpful?