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

Only Spawn One Page Determined by Dropdown Selection

Community Beginner ,
Mar 15, 2024 Mar 15, 2024

I'm not a coder and am struggling.  I need the PDF form to spawn a page from templates based on the option selected in the dropdown.  If the user changes their selection, I need the spawned page removed and replaced with a page spawned from the tempalte associated with the new selection.

 

I have a dropdown field and each option corresponds to a template.  The script I have isn't working (nothing happens).

 

Also, is there any way to set the tab order to resume from the dropdown and not move to the spawned page?

 

Any help would be greatly appreciated!!!

 

Here's the code I have in "2 Type" Dropdown properties under Actions/OnBlur/Run a Javascript:

 


var dropdownField = "2 Type"; 
var pageTemplates = {
"Joint": "Template_Joint",
"Trust": "Template_Trust",
"Estate": "Template_Estate",
"C Corp": "Template_Business",
"S Corp": "Template_Business",
"LLC": "Template_Business",
"Sole Proprietorship": "Template_Business",
"Partnership": "Template_Business",
"Traditional": "Template_Traditional",
"Roth": "Template_Traditional",
"Simple IRA": "Template_Simple",
"Self-Employed 401(k)": "Template_SelfEmployed401k",
"Pooled 401(k)/PSP": "Template_Pooled401k",
"UTMA": "Template_UTMA",
"Annuity": "Template_Annuity"
};


this.getField(dropdownField).setAction("OnBlur", function() {
spawnAssociatedPage();
});


function spawnAssociatedPage() {
var selectedValue = this.getField(dropdownField).value;
var templateName = pageTemplates[selectedValue];

// Remove previously spawned pages
removeSpawnedPages();


if (templateName) {
this.spawnPageFromTemplate(templateName);
}
}


function removeSpawnedPages() {
for (var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
if (fieldName.startsWith("Template_")) {
this.removeField(fieldName);
}
}
}

TOPICS
How to , JavaScript , PDF forms
699
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 ,
Mar 15, 2024 Mar 15, 2024

Is this the only Template you have in the file? If so, you can check the number of pages when a selection is made.

Say the original file has 4 pages, and you always spawn the additional page as the last one. If the file has 4 pages when a selection is made, you spawn the new page. If it has 5 pages you first delete the last page, then spawn.

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 ,
Mar 15, 2024 Mar 15, 2024

Thank yo so much for your reply!

 

Good idea, but there are 10 templates related to this field/script and another field in the document also spawns a different page unrelated to this.  The people filling out the forms won't be able to delete the pages, so I need the script to remove the previously spawned pages when the person changes their selection.

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 ,
Mar 15, 2024 Mar 15, 2024

In that case it's more complicated. You will need to use some kind of marker to identify those pages, such as a unique field. You then look up that field's page property and use its value to delete the page it's on (and of course I meant that you will do that in your code, not manually by the user).

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 ,
Mar 15, 2024 Mar 15, 2024

Is that not addressed in the code above?  I thought i had established the values in the function for spawning associated pages?  I don't know enough coding to take your words and write them as a script 😞  Thank you, though!!

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 ,
Mar 15, 2024 Mar 15, 2024

Well, your code gets you close, but instead of removing the field you need to delete the page, using deletePages. However, after doing that you must break the loop, or you'll get all kinds of errors about missing field names.

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 ,
Mar 15, 2024 Mar 15, 2024

Could this work?

 

function removeSpawnedPages() {

    for (var i = this.numPages - 1; i >= 0; i--) {

        this.deletePages(i);

        break; // Exit the loop after deleting the page

    }

}

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 ,
Mar 15, 2024 Mar 15, 2024
LATEST

This would just delete the last page in the file. You don't need a loop for that...

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 ,
Mar 15, 2024 Mar 15, 2024

removeSpawnedPages doesn't delete pages.

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 ,
Mar 15, 2024 Mar 15, 2024

Preach! 😉

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