Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Subtraction javascript

Explorer ,
Jul 13, 2023 Jul 13, 2023

I am currently using the below formula for subtraction. However, if "FieldB.0" is empty, I do not want the script to run. When ever "FieldA.0" is filled in, it is still returning the value. I know I need to use an "if" in the script but I do not know how to do that.

 

 

var v1 = getField("FieldA.0").value;

var v2 = getField("FieldB.0").value;

event.value=v1-v2;

TOPICS
JavaScript , PDF
783
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Jul 13, 2023 Jul 13, 2023

Use this:

 

var v1 = getField("FieldA.0").valueAsString;
var v2 = getField("FieldB.0").valueAsString;

if (v1=="" || v2=="") event.value = "";
else event.value=Number(v1)-Number(v2);

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 13, 2023 Jul 13, 2023

Use this:

 

var v1 = getField("FieldA.0").valueAsString;
var v2 = getField("FieldB.0").valueAsString;

if (v1=="" || v2=="") event.value = "";
else event.value=Number(v1)-Number(v2);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 13, 2023 Jul 13, 2023

Perfect, thank you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 15, 2024 Jan 15, 2024

Hi it just worked Fine, but there is a problem, i want the subtraction as if Field1 - Field2 = Ans, but i want to add +1 in Answer if the value is given, and if the value is not given then answer must be zero plz guide me. for example 401 - 500 = 100 (Answer should be this) but if the fields are empty then answer must be the zero. picture is attached. Reference.pngexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 15, 2024 Jan 15, 2024

Change the last two lines to:

 

if (v1=="" || v2=="") event.value = 0;
else event.value=(Number(v1)-Number(v2))+1;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 16, 2024 Jan 16, 2024

I have applied the following formula but i want that if the given data is zero then the result in total shall also be zero (0) Rest of the things are okay.

 

Here is the formula which i applied:

 

var v1 = getField("field2.0").valueAsString; var v2 = getField("field1.0").valueAsString; if (v1=="" || v2=="") event.value = 0; else event.value=(Number(v1)-Number(v2))+1;ec copy.pngexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 16, 2024 Jan 16, 2024

Change the third line to:

if (v1=="" || v2=="" || (v1+v2)==0) event.value = 0;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 16, 2024 Jan 16, 2024
LATEST

Thank u so much God bless u, Thanks alot .

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines