client scriptで別テーブルから値を取得し、反映する方法  How to get the value from another table with client script and reflect it

Kazuki Maekawa
Mega Explorer

こんにちは。

現在、「1」で設定した値を基に「member」テーブルを参照し、1と一致するレコードの「原価」「利益」の値を取得し、「稼働時間」に入力された数字をかけた値を、それぞれ「shien_kanri」テーブルの「原価」、「利益」フィールドに反映させるという処理を実行したいです。

下記ページを参考にコードを書いているのですが、初学者のためうまくいかず、
どのように書き直せば上記の処理を実現できますでしょうか。
https://community.servicenow.com/community?id=community_question&sys_id=c74a21961b407450a17c62c4bd4bcb31

Hi.

Currently, refer to the "member" table based on the value set in "1", get the "原価" and "利益" values ​​of the records that match 1, and multiply by the number entered in "稼働時間". I want to execute the process of reflecting the value in the "原価" and "利益" fields of the "shien_kanri" table, respectively.

I wrote the code referring to the following page, but it didn't work,
How can I rewrite it to achieve the above process?

Thank you.
 

 

 

10 REPLIES 10

shloke04
Kilo Patron

Can you please translate on what help you need in your requirement to assist you further?

 

Regards,

Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

I want a sample script that can achieve the above, or a page that can be used as a reference to achieve the above.

thanks.

Sure can help you out , but what is that above is not clear to me.

Can you write down your requirement in English to assist you further. 

 

Regards,

Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hitoshi Ozawa
Giga Sage
Giga Sage

 テーブル間のキーが不明です。例えばshien_kanriテーブルとmemberテーブルを関連するキー列です。ServiceNowは内部ではsys_idと言うGUIDのキーを利用しています。フォームの参照フィールを選択された場合はこのsys_idがテーブルに登録されます。U_reference_1とはこのsys_idのことでしょうか?それともnameのようなユーザ名かプロジェクト名で紐づいているのでしょうか?

その他にshien_kanriレコードとmemberレコードは1:1の関係なのでしょうか?それとも1:nの関係でしょうか?

また、どのように実行されるかの説明も必要です。例えば、月次バッチで実行するのかフォームを登録した後に直ぐに実行するのか。それとも何かのトリガーにより実行されるのでしょうか?

一応、基本的には次のようなコードになります。ただし、仕様により変わります。

var gr_shien = new GlideRecord('shien_kanri');

gr_shien.addQuery('sys_id', U_reference_1);

gr_shien.query();

if (gr_shien_query.next()) {

  var gr_member = new GlideRecord('member');

  gr_member.addQuery('sys_id', gr_shien.get('member_id'));

  gr_member.query();

  var total_cost = 0;

  var total_profit = 0;

  while (gr_member.next()) {

     var cost = gr_member.cost;  // 原価

     var profit = gr_member.profit;  // 利益

     var hours = gr_member.hours;  // 稼働時間

     total_cost += cost * hours;

      total_profit += profit * hours;

  }

  gr_shien.cost = total_cost;

  gr_shein.profit = total_profit;

  gr_shein.update();

}