Help with while loop

Community Beginner ,
Jul 20, 2018 Jul 20, 2018

Copy link to clipboard

Copied

Hello everyone, hope all is well.

It has been a while since I last visited and this forum has always been very helpful and I thought it would be good to clarify a (newbie) question.

I am trying to obtain data from several form fields using the below code:

var count_item = 1;

while (this.getField("Bem_Descr_"+count_item).value!=="")

{

var REG27 = [];

REG27 ["descricao"] = this.getField("Bem_Descr_"+count_item).value;

REG27 ["valor_ant"] = this.getField("Bem_Posicao_Anterior_"+count_item).value;

REG27 ["valor_atual"] = this.getField("Bem_Posicao_Atual_"+count_item).value;

REG27 ["pais"] = this.getField("Bem_Codigo_Pais_"+count_item).value;

var output_reg27 = "\r27"+NR_CPF+REG27.descricao+REG27.valor_ant+REG27.valor_atual+REG27.pais;

app.alert(output_reg27);

count_item++

}

I am able to see each of the results using the app.alert, but I need to have each of the results in a single string altogether.

Could anyone point me in the right direction?

Many thanks in advance!

TOPICS
Acrobat SDK and JavaScript

Views

208

Likes

Translate

Translate

Report

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

Adobe Community Professional , Jul 20, 2018 Jul 20, 2018
HI,If I understand correctly you want to create a string that keeps getting added to so you would end up with a string which contains all the fields in the form?If that is the case then just declare "output_reg27" outside of your loop and use the "+=" inside the loop so your code would look like this.var count_item = 1;var output_reg27 = "";// as you don't know how many fields, this shouldn't cause an error when you run out of fieldswhile (this.getField("Bem_Descr_"+count_item) !== null){var REG...

Likes

Translate

Translate
Adobe Community Professional ,
Jul 20, 2018 Jul 20, 2018

Copy link to clipboard

Copied

It's not clear to me what you mean by "a single string altogether"? It would be helpful if you provided some example field values and the resulting string that you'd like to see. There are some problems with that code that make me unsure what it is you're trying to accomplish.

Likes

Translate

Translate

Report

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 ,
Jul 20, 2018 Jul 20, 2018

Copy link to clipboard

Copied

Many thanks for the reply, George.

Basically I need to obtain data from form fields and each field name ends with a number.

I need to have all field values into one text.

Example:

in field "Bem_Descr_"+count_item I have a value of "this is my text from bem_descr_1".

in field "Bem_Posicao_Anterior_"+count_item I have a value of "this is my text from bem_posicao_anterior_1".

I need the result to be a single text joining all values in each for field. In the above example, it would be "this is my text from bem_descr_1"+"this is my text from bem_posicao_anterior_1".

This repeats for the other form fields indicated and increases until I find an empty "Bem_Descr_"+count_item field.

Since there are several fields, each one ending in a number (e.g. "Bem_Descr_2", "Bem_Descr_3" etc), I am struggling to find a solution.

Could you help?

Thank you,

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jul 20, 2018 Jul 20, 2018

Copy link to clipboard

Copied

Have you actually test the code that adds new fields based on a template so you know what the resulting field names are? If you're adding new pages by spawning a template and choosing to rename the fields when you do, yiur code will have to account for the resulting field names.

Likes

Translate

Translate

Report

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 ,
Jul 20, 2018 Jul 20, 2018

Copy link to clipboard

Copied

Hi George, thanks. Yes, the names are known upon spawning.

My issues is with "isolating" each result and using the result as a new variable, so I can add it as necessary to a string.

E.g., the values I get from the fields ending with 1 are stored, so I can join it with the values I get from the fields ending with 2 etc.

Thanks!!!

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jul 20, 2018 Jul 20, 2018

Copy link to clipboard

Copied

HI,

If I understand correctly you want to create a string that keeps getting added to so you would end up with a string which contains all the fields in the form?

If that is the case then just declare "output_reg27" outside of your loop and use the "+=" inside the loop so your code would look like this.

var count_item = 1;

var output_reg27 = "";

// as you don't know how many fields, this shouldn't cause an error when you run out of fields

while (this.getField("Bem_Descr_"+count_item) !== null)

{

var REG27 = [];

REG27 ["descricao"] = this.getField("Bem_Descr_"+count_item).value;

REG27 ["valor_ant"] = this.getField("Bem_Posicao_Anterior_"+count_item).value;

REG27 ["valor_atual"] = this.getField("Bem_Posicao_Atual_"+count_item).value;

REG27 ["pais"] = this.getField("Bem_Codigo_Pais_"+count_item).value;

// this means, concat the string to what is already there.

output_reg27 += "\r27"+NR_CPF+REG27.descricao+REG27.valor_ant+REG27.valor_atual+REG27.pais;

app.alert(output_reg27);

count_item++

}

Do take note of the response above, if you are renaming the fields when you spawn the template, then this above code wont work as expected.

Hope that helps,

Malcolm

Likes

Translate

Translate

Report

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 ,
Jul 20, 2018 Jul 20, 2018

Copy link to clipboard

Copied

Hi Malcolm,

Thank you so much for this, your code works like a charm!

Best Regards,

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jul 20, 2018 Jul 20, 2018

Copy link to clipboard

Copied

Hi,

I would not loop this way as when you run out of fields, you code will crash as the while loop will be unable to get the value of the field as it will be null.

I would change your loop to be

while (this.getField("Bem_Descr_"+count_item) !== null)

Although if you know how many fields you have, it would probably be better just to loop for those fields

for ( var i = 1; i <= 25; i++)

{

    // your code here

}

In you string creation line you have the NR_CPF variable, where does this come from?

Regards

Malcolm

Likes

Translate

Translate

Report

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 ,
Jul 20, 2018 Jul 20, 2018

Copy link to clipboard

Copied

Hi Malcolm,

Thanks for your reply, it is much appreciated. It makes perfect sense to use it as null, thank you!

The NR_CPF is a variable previously defined in other part of the code. It is basically 11 numbers (it is the taxpayer id that needs to come in front of the results found).

Unfortunately the number is not known, as new fields will be created upon spawning a page using a template in the future.

Thanks again!

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jul 20, 2018 Jul 20, 2018

Copy link to clipboard

Copied

Happy to help.

Regards

Malcolm

Likes

Translate

Translate

Report

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 ,
Aug 03, 2018 Aug 03, 2018

Copy link to clipboard

Copied

LATEST

Hi guys,

I really appreciate all the help provided. However, as indicated by Malcolm, I cant get the code to work with the spawned pages.

I have already set the applicable page counters etc. but can not get past this (example):

while (this.getField("P"+count_page+".Template."+"Bem_Descr_"+count_item) !== null)  {

code here;

}

Since this field will only show on a specific page (e.g. page 8, but could be page 9, 10 etc), how can I work around to have the loop check whether or not the field exists? I am trying something with try and catch, but no luck also.

Any help in pointing me to the right direction is greatly appreciated.

Thanks and Best Regards,

Likes

Translate

Translate

Report

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