Call Document Function into Form Field

New Here ,
Aug 09, 2022 Aug 09, 2022

Copy link to clipboard

Copied

Call a function from Document to output data using other fields into a text field.

I'm trying to use the diffDate function to put "Hello" into the PA2 Field.

Document Javascript

function diffDate(SubStart, K9Start, PAge){

this.getField("PA2").value = "Hello";}

 

This code works to directly put it into the field.

But I will be repeating this code with different fields, so I would like to mearly call the function & parameters needed to run the function for each field.


Document Javascript:

function diffDate(SubStart, K9Start, PAge){

this.getField("PAge").value = "Hello";}

 

also tried PAge.value = "Hello";}

                PAge = "Hello";}

This code does not work. Somehow PAge is not getting relayed back to PA2 field.

 

PA2 Field Script:

diffDate((this.getField("SDTP2").value), (this.getField("K9S2").value), (this.getField("PA2").value));

TOPICS
Create PDFs , General troubleshooting , How to , JavaScript , PDF forms

Views

34

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 Expert ,
Aug 09, 2022 Aug 09, 2022

Copy link to clipboard

Copied

Your function has parameters, but it doesn't do anything with them. What's the reason for the SubStart and K9Start parameters? As for the one you are using (or trying to), you're not doing it correctly. When you put something in quotes it's treated as a string. Remove the quotes and it will be treated as a variable, like this:

 

this.getField(PAge).value = "Hello";

 

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
New Here ,
Aug 09, 2022 Aug 09, 2022

Copy link to clipboard

Copied

So  the thinknig behind it: SubStart and K9Start parameters will get a value from SubStartField (SDTP2) and K9StartField (K9S2), the diffDate calculates the difference between these dates and should return the results, in a string, from the various if & else statements to PAge.

This is where my understanding is.
PAge is a parameter that cooresponds to the field: (this.getField("PA2").value));

     So PAge Parameter should output to PA2 Field.

     If I use event.value = "Hello"; the code works regardless of the parameter PAge.  However; when I use the full code.

   There is no output. I'm not sure there really needs to be a parameter for PAge since event.value will output to the field. But I'm hung up on getting the actual data from 2 different form fields into the function in the document javascript and then output it back to the form.

 

Full Function will be:

function diffDate(SubStart, K9Start){

var SubStartdstr="";
var K9Startdstr="";

if (SubStart && K9Start) {
SubStartdstr = util.scand("m-d-yyyy hh-mm", this.getField("SubStart").value);
K9Startdstr = util.scand("m-d-yyyy hh-mm", this.getField("K9Start").value);}


var diff = ((K9Startdstr.valueOf() - SubStartdstr.valueOf())/1000)+1;
var minute=0;
var hour=0;
var day=0;

if (diff >= 0){

if (diff >= (259260)){
day = parseInt(diff / 86400);
hour=parseInt((diff%86400)/3600);
}
else{hour = parseInt((diff%(259260))/3600);
}
minute = parseInt(diff%3600/60);
}

if(day==0){
if(hour==0){
if(minute==0){
event.value = "";}
else{
event.value = minute + " m";}
}

else{
event.value = hour + "h : " + minute + " m";}}

else{
event.value = day + "d : " + hour + "h : " + minute + " m";}
}

 

Field Java:

diffDate((this.getField("SDTP2").value), (this.getField("K9S2").value));

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
LEGEND ,
Aug 09, 2022 Aug 09, 2022

Copy link to clipboard

Copied

It is no use passing a parameter for the field name, then not using it in the function. Really, it is very hard to understand what you are trying to do. I do see one hint. You have a variable K9Start and you code this.getField("K9Start"). This uses a field called, exactly, K9Start. If you want to use the value of the variable, don't use those quotes. 

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 Expert ,
Aug 10, 2022 Aug 10, 2022

Copy link to clipboard

Copied

LATEST

You're still making the same error as the one I pointed out before, ie. placing variable names in quotes, thereby converting them to strings. Another issue with your code is you're not existing the function if one of the date fields is empty. You're also mixing up String values with Dates...

I didn't test it, but I fixed those issues for you in the following code:

 

function diffDate(SubStart, K9Start) {

    var SubStartdstr = this.getField(SubStart).valueAsString;
    var K9Startdstr = this.getField(K9Start).valueAsString;
	
	if (SubStartdstr=="" || K9Startdstr=="") {
		event.value = "";
		return;
	}

    SubStartdDate = util.scand("m-d-yyyy hh-mm", SubStartdstr);
    K9StartdDate = util.scand("m-d-yyyy hh-mm", K9Startdstr);

    var diff = ((SubStartdDate.valueOf() - K9StartdDate.valueOf()) / 1000) + 1;
    var minute = 0;
    var hour = 0;
    var day = 0;

    if (diff >= 0) {

        if (diff >= (259260)) {
            day = parseInt(diff / 86400);
            hour = parseInt((diff % 86400) / 3600);
        } else {
            hour = parseInt((diff % (259260)) / 3600);
        }
        minute = parseInt(diff % 3600 / 60);
    }

    if (day == 0) {
        if (hour == 0) {
            if (minute == 0) {
                event.value = "";
            } else {
                event.value = minute + " m";
            }
        } else {
            event.value = hour + "h : " + minute + " m";
        }
    } else {
        event.value = day + "d : " + hour + "h : " + minute + " m";
    }
}

// call to function from calculation script 
diffDate("SDTP2", "K9S2");

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