Copy link to clipboard
Copied
Hi everyone,
I have been working on accomplishing a task, that while I have the know-how on completing, is too large for my needs. Looking to see if someone has an idea on how to make this smaller in size as to reduce bogging down the form.
Here is what I am doing. I have a medical form I need to complete. It is a list of medications. Each row has medication name, dosage, frequency, side effects, etc. I have a dropdown list for the medications - the list contained 43 common medications with an "Other" so it can be added if necessary. According to the medication chosen, three other fields in the same row are autopopulated - common medication name, what it treats, and side effects.
The code I wrote works great. The problem is it is quite large with 43 medications. The page contains 11 rows. I would need to change the code for each row for it to autopopulate the corresponding fields. Is there an easier way to populate each row without needing to change each line of code according? It would be enormous with how I am doing it. Please, help me make it a bit more efficient. 😃
var MedData = {
"Medicine 1":{ addmed: "Tylenol", purpose: "Headache/Fever", sides: "Nausea, cramps" },
"Medicine 2":{ addmed: "Advil", purpose: "Pain/Fever", sides: "Side 1, 2, 3" },
"Medicine 3":{ addmed: "Aspirin", purpose: "Blood Thinner/Headache", sides: "Side 3, 2, 4" },
"Medicine 4":{ addmed: "Vitamin C", purpose: "Supplement", sides: "Side 3, 5, 4" }};
function SetFieldValues(cMedList) {
this.getField("Med1").value = MedData[cMedList].addmed;
this.getField("Purpose1").value = MedData[cMedList].purpose;
this.getField("Field2").value = MedData[cMedList].sides;
}
Many thanks and I look forward to another set of eyes.
Stephanie
(I am not sure why the formatting on the code is messing up when I post. Sorry!)
Copy link to clipboard
Copied
You're right that hard-coding the data into the script is not a good idea. It makes updating it very cumbersome and risky (if you enter something wrong, the whole code stops working...). That is why it's a good idea to separate the two. Place the data in a text file and attach it to the PDF and then read the data from that file and use it as the data-model in your code to determine which values to populate in the other fields when a selection is made. That requires a more complex script, but is better in the long run.
If you're interested, I've developed a (paid-for) tool that allows you to do just that quite easily. You just run it on your file, select the text file that contains the data, the main drop-down field, and it does the rest for you! If in the future you want to update the data you just replace the text file with the new version, and the embedded script will pick it up automatically the next time you open the file. You can find it here: Custom-made Adobe Scripts: Acrobat -- Populate Fields From Dropdown
Copy link to clipboard
Copied
Put your data in a document level script, then it only has to be changed in one place. You'll also need a code for populating the dropdowns in each row with the data.
For example:
var aMedList = [];
for(var cNm in MedData)
aMedList.push(cNm)
for(var i=1;i<15;i++)
this.getField("DropDown_Row" + i).setItems(aMedList);
The actual dropdown name pattern needs to be changed to match your form.
Since this code can is in a document script, it will be run every time the document is opened, or you can comment it out and then copy and paste it to the console if you need to update the form.
This technique is discussed here:
https://acrobatusers.com/tutorials/js_list_combo_livecycle
Copy link to clipboard
Copied
Hi Thom,
Thank you so much. I tried to take a look at the provided 700+ page manual, and well, that is a lot to read through. I needed to be pointed in the right direction. I knew there was a way to do it, but I wasn't quite sure how. Kind of like having a word at the tip of your tongue. 😃
I will hopefully be able to figure it out now.
Best regards,
Stephanie