有用な承認アサインスクリプト

  • リリースバージョン: Yokohama
  • 更新日 2025年01月30日
  • 所要時間:10分
  • これは、有用な承認およびアサインスクリプトの検索可能なバージョンです。

    警告:
    ここに記載されているカスタマイズは、特定のインスタンスで使用するために開発されたものであり、Now Support ではサポートされていません。この方法は現状のまま提供され、実装の前に完全にテストする必要があります。このカスタマイズに関するすべての質問およびコメントは、コミュニティフォーラムに投稿してください。

    使いやすいバージョンについては、「有用なスクリプトポータル」を参照してください。

    ESS 要求のグループのアサイン

    タイプ:アサインルール。

    説明:このスクリプトは、すべての ESS 要求に対してグループを自動的にアサインします。

    スクリプトの例:

    if(current.opened_by.roles==""){
        current.assignment_group.setDisplayValue('Network');
        current.update();}

    配送計画タスクに基づいたカタログアイテムのグループへのアサイン

    タイプ:アサインルール。

    説明:このアサインルールは、デスクトップグループにアサインされたカタログタスクを持つ配送計画を使用する場合に、サービスカタログアイテムをデータベースグループにアサインします。
    //Return catalog items that have no group but do have a delivery plan assigned
    var ri =new GlideRecord("sc_cat_item");
    ri.addQuery("group","=",null);
    ri.addQuery("delivery_plan","!=",null);
    ri.query();
    while(ri.next()){
        gs.log("Found an item");
        //Return tasks that point to the same delivery plan as the above item
        var dptask =new GlideRecord("sc_cat_item_delivery_task");
        dptask.addQuery("delivery_plan","=",ri.delivery_plan);
        dptask.query();while(dptask.next()){
            gs.log("Found a task");var gp = dptask.group.getDisplayValue();
            gs.log(gp);//If the task is assigned to desktop, assign the item's group to desktop
            if(dptask.group.getDisplayValue()=="Desktop"){
                ri.group.setDisplayValue("Desktop");
                gs.log("updating "+ ri.getDisplayValue());
                ri.update();break;}}}

    1 つのタスクでアイテムをアサイン

    タイプ:アサインルール。

    説明:特定のグループに関連付けられた 1 つのタスクのみを持つカタログアイテムを自動的にアサインします。
    //Get the catalog item for the current requested item
    var scCatItem =new GlideRecord("sc_cat_item");
    if(scCatItem.get('sys_id', current.cat_item)){
    // If the catalog item already has an assignment group or if using workflow we don't need to make an assignment
      if(!scCatItem.delivery_plan.nil()&& scCatItem.group.nil()){
            var dpTask =new GlideRecord("sc_cat_item_delivery_task");
            dpTask.addQuery("delivery_plan","=",scCatItem.delivery_plan);
            dpTask.query();
            if(dpTask.getRowCount()==1&& dpTask.next()){
            // Check that there is only 1 record in the GlideRecord
                dpTask.group;}}}

    作業負荷に基づく割り当て

    タイプ:ビジネスルール。

    説明:アクティブなインシデントの数が最も少ないアサイン先グループメンバーに基づいて、アサイン先を入力します。

    パラメーター:
    • order:>1000 (アサインルールの後に実行する場合)
    • condition:current.assigned_to == '' && current.assignment_group != ''
    • when:before, insert/update
    var assignTo = getLowestUser();
    gs.addInfoMessage("assigning to is "+ assignTo);
    current.assigned_to= assignTo;
     
    function getLowestUser(){
      var userList =new Array();
      var cg =new GlideRecord('sys_user_grmember');
      cg.addQuery('group', current.assignment_group);
      cg.query();
      while(cg.next()){
        var tech = cg.user.toString();
        var cnt = countTickets(tech);
        gs.addInfoMessage("Tech counts "+ cg.user.name+' '+ cnt +" "+ tech);
        userList.push({ sys_id: tech,name: cg.user.name, count: cnt });}
    
      for(var i=0; i < userList.length; i++){
        gs.addInfoMessage(userList[i].sys_id+" "+ userList[i].name+" "+ userList[i].count);}
      userList.sort(function(a, b){
        gs.addInfoMessage("Sorting: "+ a.sys_id+"("+ a.count+");
        "+ b.sys_id+"("+ b.count+")");
        return a.count- b.count;});
    
      if(userList.length<=0)return"";
      return userList[0].sys_id;}
     
    function countTickets(tech){
      var ct =new GlideRecord('incident');
      ct.addQuery('assigned_to',tech);
      ct.addQuery('active',true);
      ct.query();
      return ct.getRowCount();}

    カテゴリが変更されたときにアサインルールを実行

    タイプ:クライアントスクリプト。

    テーブル:インシデント。

    説明:この例は、インシデント内のカテゴリフィールドの onChange クライアントスクリプトです。注意:このスクリプトは、同期 AJAX を使用していました (非同期動作は ajaxRequest 呼び出しの 3 番目のパラメーターで指定されます)。以下の実装では、非同期 AJAX を使用しています。同期バージョンを使用する場合の短所は、ネットワーク応答の問題によってブラウザーがハングアップする可能性があることです。
    // Make an AJAX request to the server to get who this incident would be
    // assigned to given the current values in the record. This runs the assignment 
    // rules thathave been defined in System Policy and returns the assigned_to and 
    // the assignment_group
     
    function onChange(control, oldValue, newValue, isLoading){
      if(isLoading){return;
      // No change, do not do anything
      }
    
       // Construct the URL to ask the server for the assignment
      var url ="xmlhttp.do?sysparm_processor=AJAXAssignment&sys_target=incident";
      var uv = gel('sys_uniqueValue');
      if(uv){
            url +="&sys_uniqueValue="+ uv.value;}
      // Make the AJAX request to the server and get the response
      var serial = g_form.serialize();
      // get all values currently assigned to the incident
      var response = ajaxRequest(url, serial,true, responseFunc);}
     
    // This callback function handles the AJAX response.
    function responseFunc(response){
      varitem= response.responseXML.getElementsByTagName("item")[0];
      // Process the item returned by the server
      if(item){
      // Get the assigned_to ID and its display value and put them on the form
        varname=item.getAttribute("name");
        var name_label =item.getAttribute("name_label");
        if(name_label &&name){
          g_form.setValue('assigned_to',name, name_label);}
        else{
          g_form.setValue('assigned_to','','');}
        // Get the assignment_group ID and its display value and put then on the form
        var group =item.getAttribute("group");
        var group_label =item.getAttribute("group_label");
        if(group_label && group){
          g_form.setValue('assignment_group', group, group_label);}
        else{
          g_form.setValue('assignment_group','','');}}}

    カスタム承認 UI マクロ

    タイプ:UI マクロ。

    次のオプションは、新しい UI マクロを作成して、実行計画の [自分の承認] ビューから詳細を取得する方法を示しています。
    • [システム UI] に移動して、[UI マクロ] をクリックします。
    • 既存の「approval_summarizer_sc_task」という名前を「approval_summarizer_sc_task_old」などに変更して、無効にします。
    • 同じ名前を使用して新しいマクロを作成します (「approval_summarizer_sc_task」)。名前は基本的に、マクロの実行内容と適用対象を示すものにする必要があります。ここでは、既存のものを置き換えるため、既存の名前を再利用することにしました。
    図 : 1. マクロリスト
    「approval_summarizer_sc_task」と「approval_summarizer_sc_task_old」がハイライト表示されたマクロリスト。

    次に、この記事の下部にある xml スクリプトを、新しい UI マクロの xml コードウィンドウにコピーします。これは、サービスカタログ実行計画内の承認タスクを使って品目の承認を行うときに、承認者に何らかの詳細を提供するのに最適な方法です。

    さまざまな方法

    従来の方法

    これは、従来の方法を使って承認タスクを使用するときに [自分の承認] に表示されるビューです。

    図 : 2. 自分の承認
    従来の方法で表示された [自分の承認] 情報。

    承認者が実際に何を承認しようとしているのか、あまり詳しく書かれていないことに注意してください。タスクの簡単な説明は確認できますが、アイテムの詳細はわかりません。

    新しい方法

    これは、OOB (Out-Of-Box:すぐに利用可能な) UI マクロの代わりに以下の xml スクリプトを使用した場合に表示されるビューです。

    図 : 3. 自分の承認
    新しい方法で表示された [自分の承認] 情報。

    この方法を使用すると、要求の承認と同様に詳細を表示できます。注文したアイテムへのリンク、簡単な説明 (アイテムから変数を展開する機能を含む)、価格、数量、合計価格があります。この方法は、より詳細な情報を示すという点で、承認者の助けとなります。承認者は、実際に承認しているものを確認できるようになりました。