How can I reuse a script to set variables?

Geckoz100

12-08-2016

I have a script to set some variables and I need to run the script many times throughout the form, for different users at different times.  How can I set up the script as an object in Variables and call it when I need to run it?  Greatly appreciate any help.  I'm using ES3.

var emailSubject = "";

var cleanDate = Page1.EventDetails.Block2.SpeakingDate.rawValue.replace(/[\/]/g, "-");

var eventName = Page1.EventDetails.Block1.EventName.rawValue;

var eventCity = Page1.EventDetails.USaddress.CityEvent.rawValue;

var eventState = Page1.EventDetails.USaddress.StateEvent.rawValue;

emailSubject = cleanDate + " - " + eventName + " - " + eventCity + " - " + eventState;

Accepted Solutions (1)

Accepted Solutions (1)

mattiaswallin

15-08-2016

To create a function to be able to reuse code easily right click in your hierarchy window (for example on your "form1") and click "Insert Script Object".

A new script object will be created, just rename it to something appropriate and then create a function inside of it as

function myFunction(var1,var2,var3){

' just an example '

page1.textfield1.rawValue = var1 + var2 + var3

}

(I didn't fully understand the meaning of the date and event and city and exactly what your function is supposed to do, but this is how you declare a function.)

To call this function you simply reference it as form1.scriptobjectname.myFunction(123,"asdf",variablename)

(just an example of possible arguments).

I hope this isn't too hard to follow,

regards Mattias

Answers (2)

Answers (2)

mattiaswallin

19-08-2016

I must admit I haven't used functions that much in LiveCycle, the idea with the way I wrote it was just an example of general function syntax.

The difference between the way I wrote the function and your way (which works excellently for you) is that in your case you can't "impact" your function. Your function at this moment is simply a set piece of instructions that will be called the same way each time (of course your variables might have changed but you can't change how you use them).

Let me try make an easy example. If you want to add two numerical fields together you could write a function that would look (more similar to the way you did it):

function add(){

     form1.page1.NumericalField1.rawValue = form1.page1.NumericalField2.rawValue + form1.page1.NumericalField3.rawValue;

}

And this code would always work if you only want to add field2 and field3 and put the result in field1 (when you call the function form1.add() ). However, you could also write the code like this:

function add(result,term1,term2){

     result.rawValue = term1.rawValue + term2.rawValue

}

And when calling this function you could go with form1.add(form1.page1.NumericalField1, form1.page1.NumericalField2, form1.page1.NumericalField2)

However you could also write form1.add(form1.page2.NumericalField3, form1.page1.NumericalField2, form1.page2.NumericalField4). This writes a very versatile function that gives you a lot of possibilities.

In LiveCycle you generally have a few very defined actions you want to use and in that sense the first function is easier to use, and rather make different functions and leave the "versatility" in what functions you choose to call

I hope it cleared it up a little at least.

Geckoz100

18-08-2016

Thanks, Mattias, that helped a lot.  I was confused because I didn't understand the names related to each other, so your example clarified that.  I found that one other thing I had to do is set my variables as universal variables in Form Properties, because they would not carry over to where I was calling them from when I ran the script.  So ended up having to use ".value" for the variables.  I have a bunch of submit buttons on this form, so I needed a way to create a consistent set of headings across all of them that contained the same information.

The one thing I still don't get in your example is where you have "(123,"asdf",variablename)" -- what would you need that for?  In my example I just used functions.emailSet() and did everything else in the brackets:

//Set up e-mail subject line and internal topic headings

function emailSet() {

  emailSubject.value = "";

  emailHeading.value = "";

  if (Page1.EventDetails.Block1.r_Location.rawValue == "1") // US address

  {

  cleanDate.value = Page1.EventDetails.Block2.SpeakingDate.rawValue.replace(/[\/]/g, "-");

  eventName.value = Page1.EventDetails.Block1.EventName.rawValue;

  eventCity.value = Page1.EventDetails.USaddress.CityEvent.rawValue;

  eventState.value = Page1.EventDetails.USaddress.StateEvent.rawValue;

  emailSubject.value = cleanDate.value + " - " + eventName.value + " - " + eventCity.value + " - " + eventState.value;

  emailHeading.value = Page1.EventDetails.Block1.EventName.rawValue + "\n " +

  Page1.EventDetails.USaddress.CityEvent.rawValue + ", " + Page1.EventDetails.USaddress.StateEvent.rawValue + "\n " +

  "Speaking Date:  " + Page1.EventDetails.Block2.SpeakingDate.rawValue + "\n\n";

  }

  if (Page1.EventDetails.Block1.r_Location.rawValue == "2") // Foreign/Int'l address

  {

  cleanDate.value = Page1.EventDetails.Block2.SpeakingDate.rawValue.replace(/[\/]/g, "-");

  eventName.value = Page1.EventDetails.Block1.EventName.rawValue;

  eventCity.value = Page1.EventDetails.IntlAddress.CityEventIntl.rawValue;

  eventCountry.value = Page1.EventDetails.IntlAddress.Country.rawValue;

  emailSubject.value = cleanDate.value + " - " + eventName.value + " - " + eventCity.value + " - " + eventCountry.value;

  emailHeading.value = Page1.EventDetails.Block1.EventName.rawValue + "\n " +

  Page1.EventDetails.IntlAddress.CityEventIntl.rawValue + ", " + Page1.EventDetails.IntlAddress.Country.rawValue + "\n " +

  "Speaking Date:  " + Page1.EventDetails.Block2.SpeakingDate.rawValue + "\n\n";

  }

}