機会テーブルにクエリーを実行するスクリプトの例

  • リリースバージョン: Zurich
  • 更新日 2025年07月31日
  • 所要時間:5分
  • このサンプルスクリプトは、すべての機会を取得アカウント ID の機会を取得機会の詳細を取得カスタムアクションを使用して、機会テーブルにクエリーを実行します。

    このサンプルスクリプトは、次の 3 つの部分で構成されています。
    1. 最初の部分では、適切なカスタムアクションを選択し、それに対する入力を準備します。
    2. 2 番目の部分は、アクションへの呼び出しを実行します。
    3. 3 番目の部品はアクションの出力を処理します。

    スポークアクションの選択と入力の準備

    スクリプトのこのセクションで、Salesforce アプリケーションから機会を取得するために準備されている 3 つのカスタムアクションのいずれかを選択します。
    • すべての機会を取得
    • アカウント ID の機会を取得
    • 機会の詳細を取得
    v_query関数の引数に含まれるパラメーターに基づいて、呼び出すアクションを決定できます。
    /****** Choose action and prepare action inputs *****/
    var action = null;
    var inputs = {};
    
    // look up opportunity by salesforce record id
    if (v_query.isGet()) {
    
      action = "get_opportunity_details";
      inputs.salesforce_opportunity_record_id = v_query.getSysId();
          
    
    // look up opportunities by salesforce account id
    } else if (v_query.getParameter("u_sf_account_id")) {
    
        if (v_query.getParameter("u_sf_account_id") == "undefined") {
          gs.addInfoMessage(“Opportunities cannot be retrieved because “ +
                            “this “Account does not have associated “    +
                            “Salesforce Account. Please contact System “ +
                             “Administrator.");
          return;
    
        } else {
          action = "get_opportunities_for_account_id";
          inputs.salesforce_account_id = v_query.getParameter("u_sf_account_id");
        }
    
    // look up all opportunities
    } else {
      action = "get_all_opportunities";
    }
    

    このスクリプトは、アクションで必要なときに Salesforce アカウントが未定義の場合に情報メッセージを設定します。未定義の値は、「関連リストを使用して顧客アカウントと Salesforce 機会の間の接続を作成する」で説明されている関係から取得されます。

    Salesforce アカウントが未定義の場合、この場合は照会するものがないため、スポークアクションを呼び出さずに関数が返されます。

    スポークアクションの呼び出し

    スクリプトのこのセクションで、Salesforce スポークの名前と選択したアクションを使用してアクションを呼び出し、呼び出しの出力を保存します。

    /***** Call action *****/
    var outputs =
            sn_fd.FlowAPI.executeAction("sn_salesforce_spok." + action, inputs);
    

    アクション出力の処理

    スクリプトのこのセクションでは、エラーチェックで始まる出力を処理します。

    /***** Process action outputs *****/
    if (outputs.status != "Success") {
    throw new Error(outputs.error_message);
    }
    

    クエリでエラーが返されない場合、スクリプトは返されたレコードを処理し、行としてリモートテーブルに追加する必要があります。Salesforce 機会フィールドをリモートテーブルの列にマッピングします。

    var opportunities = outputs.opportunities.data;
    
     for (var i = 0; i < opportunities.length; i++) {
      var opportunity = opportunities[i];
      v_table.addRow({
        "u_sf_amount": opportunity.Amount,
        "u_sf_close_date": opportunity.CloseDate,
        "u_sf_name": opportunity.Name,
        "u_sf_probability": opportunity.Probability + "%",
        "u_sf_account_id": opportunity.AccountId,
        "u_sf_stage": opportunity.StageName,
        “u_sf_type": opportunity.Type,
        "sys_id": opportunity.Id,
      });
    }
    

    Salesforce 機会レコード ID がリモートテーブル sys_id に割り当てられていることに注意してください。これにより、リモートテーブルのリストとフォームが正しく機能し、次回リモートテーブルスクリプトが呼び出されたときに v_query.getSysId() を使用してレコード ID を抽出できることが確認されます。

    次に、クエリーによって渡された情報メッセージがある場合はそれを表示します。

    if (outputs.info_message) {
      gs.addInfoMessage(outputs.info_message);
    }
    

    リモートテーブルスクリプトセクションをまとめて配置する

    スクリプトの 3 つのセクションは、エラー処理に対応する try-catch ブロックに含まれています。

    (function executeQuery(v_table, v_query) {
    
      try {
    
        // place code here from: <Selecting a spoke action and preparing the inputs>
    
        // place code here from: <Calling the spoke action>
    
        // place code here from: <Processing the action output>  	
            
      } catch (error) {
        gs.addErrorMessage("Error retrieving  Salesforce Opportunities. “ +
                           “Please contact System Administrator.");
        gs.addErrorMessage("System Error: " + error.message);
      }
    
    })(v_table, v_query);