業務ルールをJavaScriptで代用する方法
こんにちは。
皆さんは『業務ルール』を使用されていますか?
業務ルールは画面上ですぐに修正できるので便利なのですが、指定できる条件に制約があったり、大量のフィールドに対してGUIで設定を行うのが大変だったりする経験がある方もいらっしゃるかと思います。
今回は、そんな業務ルールで使用できるアクションを JavaScript でコーディングする方法をまとめてみましたので、参考にしていただければと思います。
レコメンデーション
// レコメンデーションの追加 Xrm.Page.getControl("new_field_name").addNotification({ messages: "入力された値が不正ですがよろしいですか。", notificationLevel: "recommendation", uniqueId: "something_id", actions: null }); // レコメンデーションの削除 Xrm.Page.getControl("new_field_name").clearNotification("something_id");
エラーメッセージの表示
// エラーメッセージの表示 Xrm.Page.getControl("new_field_name").setNotification("不正な値です。");
フィールド値の設定
// フィールド値の設定 // テキスト形式 Xrm.Page.getAttribute("new_field_name").setValue("東京都"); // 数値形式 Xrm.Page.getAttribute("new_field_name").setValue(100); // オプションセット Xrm.Page.getAttribute("new_field_name").setValue("100000001"); // 2つのオプション Xrm.Page.getAttribute("new_field_name").setValue(1); // 検索フィールド var Entity = new Array(); Entity[0] = new Object(); Entity[0].id = "{レコードのGUIDを設定}"; Entity[0].name = "{レコードのタイトルを設定"; Entity[0].entityType = "{エンティティのidを設定}"; Xrm.Page.getAttribute("new_field_name").setValue(Entity); // フィールド値のクリア Xrm.Page.getAttribute("new_field_name").setValue(null);
既定値の設定
フィールド値の設定と同じです。表示方法の設定
// フィールドの表示 Xrm.Page.getControl("new_field_name").setVisible(true); // フィールドの非表示 Xrm.Page.getControl("new_field_name").setVisible(false);
ロック/ロック解除
// フィールドのロック Xrm.Page.ui.controls.get("new_field_name").setDisabled(true); // フィールドのロック解除 Xrm.Page.ui.controls.get("new_field_name").setDisabled(false);
必須項目の設定
// フィールドの必須化 Xrm.Page.getAttribute("new_field_name").setRequiredLevel("required"); // フィールドの推奨化 Xrm.Page.getAttribute("new_field_name").setRequiredLevel("recommended"); // フィールドの必須/推奨解除 Xrm.Page.getAttribute("new_field_name").setRequiredLevel("none");