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

How to script a button to clear a field value in one field if another field is null

Community Beginner ,
Apr 30, 2016 Apr 30, 2016

Have a form with fields txtCode.0 to txtCode.20 and txtRow.0 to txtRow.20 created with multiple copies. Want when button butClear is pressed that if field txtCode is null in any rows that the pre-populated value of txtRow is "". 

TOPICS
Acrobat SDK and JavaScript , Windows
1.6K
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

correct answers 1 Correct answer

Advocate , Apr 30, 2016 Apr 30, 2016

Sure, the code as such can not work.

Where does the index come into the game (or, better, the field name)?

The error gets detected in line 4, which means that it occurs befor that, and that would be in line 3.

Does your document have a field with name "txtCode." ???

Because most likely not, the script fails before even executing line 4, because that field does not exist. The error message by Acrobat says that the getField() method returns null. In this context, null has the meaning of not existing.

B

...
Translate
LEGEND ,
Apr 30, 2016 Apr 30, 2016

Do these fields have any formatting being set?

What version of Acrobat are you using?

What is your operating system?

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 ,
Apr 30, 2016 Apr 30, 2016

No formatting, Acrobat Pro DC, Windows 7.

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
LEGEND ,
Apr 30, 2016 Apr 30, 2016

Why not just start out with the field's set to an initial value?

Otherwise you need to write some custom JavaScript to clear the fields and then check each field to see it is a null string and if so then set the value of the field.

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 ,
Apr 30, 2016 Apr 30, 2016

I am still a novice at scripting. Tried the following script:

//Script to remove txtRow numbers with null txtCode field value

var n = 20;

var txtCRR = this.getField("txtCode.").valueAsString;

var txtRRN = this.getField("txtRow.").valueAsString;

for (var i = 0; i <= n; i++)

if (txtCRR.value=="" ||

txtRRN==null)

(txtCRR.value=txtRRN.value)

Unfortunately getting this JavaScript console error:

TypeError: this.getField(...) is null

4:Field:Mouse Up

Any ideas?

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
Advocate ,
Apr 30, 2016 Apr 30, 2016

Sure, the code as such can not work.

Where does the index come into the game (or, better, the field name)?

The error gets detected in line 4, which means that it occurs befor that, and that would be in line 3.

Does your document have a field with name "txtCode." ???

Because most likely not, the script fails before even executing line 4, because that field does not exist. The error message by Acrobat says that the getField() method returns null. In this context, null has the meaning of not existing.

Back to the original question: As you mention null in the question, there may be a chance that you do not mean "does not exist", but "is empty". Under this assumption, you might try the following:

for (var i = 0 ; i <= 20 ; i++) {

if (this.getField("txtCode." + i).valueAsString == "") {

this.getField("txtRow." + i).value = this.getField("txtRow." + i).defaultValue ;

}

And that should do it…

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 ,
Apr 30, 2016 Apr 30, 2016

Thanks for your help. I have fields txtCode.0 to txtCode.20 and txtRow.0 to txtRow.20.

Changed the button script to:

//Script for removing row numbers with null txtCode field values

for (var i = 0 ; i <= 20; i++) {

if (this.getField("txtCode." + i).valueAsString == "") {

this.getField("txtRow." + i).value = this.getField("txtRow." + i).defaultValue ;

}

}

But still getting error:

TypeError: this.getField(...) is null

4:Field:Mouse Up

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
Advocate ,
Apr 30, 2016 Apr 30, 2016

When you still get that error, you'd have to carefully check whether your field names are correct, and whether by chance one field is missing.

You could get some additional help by adding the following line as first line in the loop:

console.println("I am in row " + i) ;

When the error occurs, it shows which i value causes the trouble, and you can locate (or, better said, not locate) the fields.

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 ,
Apr 30, 2016 Apr 30, 2016

Thank you for your help, You were right it was a field naming problem. That's it working now. Much appreciated.

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
LEGEND ,
Apr 30, 2016 Apr 30, 2016

You are trying to access the parent field in a hierarchical group of fields. 

The parent can control all the common properties of the children fields like readonly, fillColor, etc but not fields that have to be unique to each child field like value.

With hierarchical fields one can access each child field by name or each child field as an element in an array. It appears you are trying to access the child fields by name.

var nDefault = 20;

for(var i = 0, i < 21; i++) {

if(this.getField("txtRow." + i).valueAsStreing == "" || this.getField("txtRow." + i).valueAsString == "") {

this.getField("txtRow." + i).value = nDefault;

}

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 ,
May 02, 2016 May 02, 2016

Hit a problem and am wondering if you can help. When I ran the script it looked ok, in that I looked at the first txtCode. null value field and txtRow. was empty. However, in trying test forms the script is only clearing the next row txtRow. value after the last popultated txtCode. field.

I created another form with just these fields and tested it. Script works great. So I know it is not a script issue.

Have you any idea why it is just clearing the first txtRow. value after the last populated txtCode. value, and not the other null txtCode. rows?

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 ,
May 02, 2016 May 02, 2016
LATEST

Just noticed, if I repeat click the button (containing the script) it clears txtRow. after the last populated txtCode. row one row at a time.

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