フォームの値を取得して計算するClient Scriptの書き方について

Toshikazu
Tera Contributor

表題について、フォーム上のIntegerのタイプのフィールドから値を取得して計算し、

別のIntegerタイプのフィールドに値を設定するClient Scriptを書こうとしています。

 

私がJavascript初心者であることもあり、うまく動作せずに困っています。

ご協力いただけないでしょうか。

 

具体的には、フォーム上からIntegerの数値を取得し、消費税を計算させてIntegerのフィールドに格納したいです。

 

テーブル:Forecast

Type:onChange

フィールド:Planned Amount(Excludint Tax)

 

入力したいフィールド:Consumption Tax(消費税)

 

***以下Script***

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

    var price = 0.1 * newValue;
    g_form.setValue('u_planned_consumption_tax', price + '');  
}
 
Toshikazu_1-1704899734189.png

 

6件の返信6

Harsh Vardhan
Giga Patron

Hi @Toshikazu  "u_planned_consumption_tax" field is also decimal type field or integer type field?

 

Just to add, you wont be able to add decimal value into integer type field. either change the target field as string or decimal then you will be able to set decimal value into target field. 

 

You can use below article that will be useful to handle number methods.

https://www.servicenow.com/community/developer-articles/useful-number-methods-in-javascript/ta-p/232... 

 

Hope it will help you.

 

Thanks,

Harsh

Toshikazu
Tera Contributor

Hi @Harsh Vardhan 

Thank you for your reply.

Both "u_planned_consumption_tax" and "Planned Amount(Excludint Tax)" are integer field.

However this Script doesn't work. I cannnot make sure how to modify my script.

I think that "setValue" function is only for Strings field...

 

@Toshikazu Problem is you are multiplying the value 0.1 with integer value, the result will always come in decimal. that's the reason I suggested you wont be able to set decimal value to integer type field.

 

you need to change u_planned_consumption_tax as string so it can contain decimal number as well. 

 

Thanks,

Harsh

 

@Harsh Vardhan 

Thank you for your reply.
I make sense, but we cannnot change field type.

Is there a way to implement it while keeping the field type as Integer?