These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Need help with a "getField" script!

Rajkurt
Registered: May 19 2010
Posts: 47

Background:

I have a datefield (which is, on document load, automaticly gets the current date).
You can edit this field and put in desired date if you want to.

The field name is: "Date1"

SCRIPT FOR "Date1":

var f = this.getField("Date1");
f.value = util.printd("yy/mm/dd", new Date());

---

I also have a field which you can put in a desired number of days.
The field name is: "Add_days"

----

And finally I have a datefield which add the input value of the "Add_days"-field and calculates it to a new date. The script is a "Custom calculation script".
The field name is: "Date2"

SCRIPT FOR "Date2":

var sDate1 = "Date1";
var sAdd_days = "Add_days";

var nAdd = this.getField(sAdd_days).value;

var sDateFmt = 'yy/mm/dd';

var sDate = this.getField(sName).value;
var oDate = util.scand(sDateFmt, sDate);

var nFullYear = oDate.getYear();
var nMonth = oDate.getMonth();
var nDate = oDate.getDate();
nNewDate = nAdd + nDate;

var oNewDate = new Date(nFullYear, nMonth, nNewDate);
var sNewDate = util.printd(sDateFmt, oNewDate);
event.value = sNewDate;

---

This is what I need help with:

I want the default value of "Date2", to be a slave to the value of "Date1"-field. So if the value of "Date1" is changed, the value of "Date2" also shall change.

I think its a very quick fix whith the "this.getField" script, but I cant get it to work.

Thanks for the help you guys!

My Product Information:
Acrobat Standard 8.0, Macintosh
try67
Expert
Registered: Oct 30 2008
Posts: 2398
So what seems to be the problem? Does your code not work? Do you get any error messages?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Rajkurt
Registered: May 19 2010
Posts: 47
No, zero errors. The code works like it is. But I want to "link" the Date1 and Date2 field to eachother.
So if you update the "Date1" value, the "Date2" should have the same value.

I want the "Date2" field to always have the same value as "Date1", regardless if you change the value of "Date 1" or not.

I hope I calrified it...

Thanks for the fast answer!
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Not really... If you want them to have the same value, why the custom calculation for Date2? Just call it Date1 and it will always be identical to the other Date1 field.

If you want to keep the separate name, put this in the custom calc script:
event.value = this.getField("Date1").value;

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Rajkurt
Registered: May 19 2010
Posts: 47
Maybe I can clarifie it by describing the function of the form:

Its suposed to be a invoice form.

The "Date1" field is for the day the invoice is created or sent away.

The "Add_days" field is how many days the client has to pay the invoice.

The "Date2" is the actual date for the payment to be here.

Lets say that I were supposed to have created the invoice two days ago, and then have to put the date in manually in "Date1". I want the calculation to start calculate on that day and be presented in "Date2" field.

(I hope this helps you to understand the problem, im sorry for my bad english)
try67
Expert
Registered: Oct 30 2008
Posts: 2398
I see. As far as I can see, it should work like that with the original scripts you had.
The only problem is that when you open the file again, Date1 will default to today's date and Date2 will change according to that. You can maybe overcome that by adding a condition to the document script so that it will only update Date1 if Date2 is not already filled, or something like that.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Rajkurt
Registered: May 19 2010
Posts: 47
It dosent. When I open the document, the "Date1" is filled in, no problems there.
The "Add_days" field is blank and the "Date2" field is blank (which is what i need to change..)
I can send you a working copy of the pdf if it helps.

Thanks for your time!
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Did you set the field calculation order so that Date2 is calculated after Date1?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Rajkurt
Registered: May 19 2010
Posts: 47
Today its nothing that links the fields together, the script for "Date1" is totally seperated from "Date2".

Thats why I thought to link the value of "Date1" to "Date2" with a "getField" script. So the very second you open the document "Date1" gets the current date and sends it forward to "Date2" (the current date is now on two places in the document, the perfect starting point for the calculation that is later made by "Add_days" field).
Rajkurt
Registered: May 19 2010
Posts: 47
Is it possible to do?
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Yes, it's possible. Have a look here:
http://help.adobe.com/en_US/Acrobat/9.0/Standard/WS58a04a822e3e50102bd615109794195ff-7de2.w.php
(it's for version 9, but I think it will work for 8 as well)

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Wait, there's something wrong with your code as well. In this line:
var sDate = this.getField(sName).value;
Where do you define sName ? Shouldn't it be sDate1 ?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Rajkurt
Registered: May 19 2010
Posts: 47
Ooops......Yeah, thats right, my bad. When I pasted the script here i alterd the names of the value to make it more easy to understand and mixed the code up.... Im so sorry! Maybe this is why its been hard for me to commuicate the problem.

This is the real code for Date2:

var sDate2 = "Date2";
var sAdd_days = "Add_days";

// number of days to add
var nAdd = this.getField(sAdd_days).value;

// date format for input and displayed date
var sDateFmt = 'yy/mm/dd';

// get inputted date string
var sDate = this.getField(sDate2).value;
// convert date string to date object
var oDate = util.scand(sDateFmt, sDate);


// get parts of the date
var nFullYear = oDate.getYear(); // full year
var nMonth = oDate.getMonth(); // zero based month
var nDate = oDate.getDate(); // date of month
// add nAdd days to date
nNewDate = nAdd + nDate;

// convert to new date object
var oNewDate = new Date(nFullYear, nMonth, nNewDate);
// make date string
var sNewDate = util.printd(sDateFmt, oNewDate);
// set field value
event.value = sNewDate;
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Now I don't understand it even more.
You are using the current value of Date2 to calculate its new value? How is that supposed to work?

Here's what I think you should do:
- Add to the document-level script an if statement that checks if Date2 is empty. If not, don't change Date1. If it is empty, set Date1 to today's date.
- Change the calculation of Date2 so that it reads the date from Date1, adds the number of days from Add_Days to it, and sets the result to Date2.
If there's no date in Date1, set Date2 to be empty as well.

That way, if you manually change the value of Date1, Date2 will also be updated, and it won't be deleted when you re-open the file.
If you want today's date you can simply clear Date1 and then re-open the file.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Rajkurt
Registered: May 19 2010
Posts: 47
Haha! Thanks for the help I solved it thanks to you!

What I did was that I posted the answer with the question all along! Except that when I posted it here I alterd the code so that It would be easier to understand. But when I alterd it I actually fixed it. So when I found out that I have posted the wrong code, I looked closer to your reply:

"... As far as I can see, it should work like that with the original scripts you had... ."

And I tried it to alter the code as posted it here, now it works beautifully!

BIG THANKS FOR ALL YOUR HELP!
try67
Expert
Registered: Oct 30 2008
Posts: 2398
I still don't really get it, but if it works, good enough...

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com