Jump to content

Can't get ExternalDataFile to work


havdp

Recommended Posts

Has anyone been able to get ExternalDataFile or ExternalDataFileEx to work? I have written an OnJobStart rule to load an external data file into a variable, then another rule to read a record from that file. My OnJobStart rule validates, but the rule which reads from the external file generates an error that the variable I loaded the external file into does not exist.

 

I have written another rule in Javascript globals to define another external data file and then a rule which simply reports the recordCount of the other data file. This rule validates and returns the correct record count. But in the preview and composed result returns the number zero. A rule which reads a record from the other file returns an empty string.

 

Any suggestions. I am using FusionPro 4.2P1d on a Mac OS 10.4.11 in Acrobat 7.0.9.

Link to comment
Share on other sites

I am having the same issue and I am on a Mac as well. Try putting the full path of the external data file. That seem to work.

 

Here's the code that loads the External File in the JavaScript Globals:

var filename = "/FullPathGoesHere/classes_database.txt";
Classes_File = new ExternalDataFileEx(filename, "\t");
if (!Classes_File.valid)
 Print("Unable to open External Data File: " + filename);

 

Here's the code that calls the content of the External File:

if (!Classes_File.valid)
 return "Failed to load external file";
var Desc = Classes_File.FindRecord("Title", Field("C1Class1Title"));
return Classes_File.GetFieldValue(Desc, "Description");

 

Let me know if you need more explanation.

Link to comment
Share on other sites

This has been a great help. My problem was a result of declaring my external file as a variable. Using your example, my own code would have been:

 

var filename = "/FullPathGoesHere/classes_database.txt";
[color=Red][b]var[/b][/color] Classes_File = new ExternalDataFileEx(filename, "\t");
if (!Classes_File.valid)
 Print("Unable to open External Data File: " + filename);

The red "var" above caused all my woes. When I removed it, I was able to finally access the data in the external file.

Link to comment
Share on other sites

I'm not 100% sure on this, because FP executes JavaScript a little unclear for me. (What exactly are OnJobStart, or any other rule for that matter? are they functions?)

 

For creating variables:

If the var is not used then the variable will always be defined as global

If the var is used it will be global, or local, depending on how used.

If the var is used inside of a function then that variable is only avaliable inside that function. I suspect that somehow we are inside a function when we declare the external file and therefore want to declare it with out var as to make it globally accessible. Some one please correct me if I'm wrong.

Link to comment
Share on other sites

I'm not 100% sure on this, because FP executes JavaScript a little unclear for me. (What exactly are OnJobStart, or any other rule for that matter? are they functions?)

 

Yes, all rules in FusionPro are functions. That's why you "return" a value from them.

 

This is true of Callback rules as well as "regular" Text and Graphic rules, although in the case of Callbacks, the return value is optional.

 

For creating variables:

If the var is not used then the variable will always be defined as global

If the var is used it will be global, or local, depending on how used.

If the var is used inside of a function then that variable is only avaliable inside that function. I suspect that somehow we are inside a function when we declare the external file and therefore want to declare it with out var as to make it globally accessible. Some one please correct me if I'm wrong.

 

Yes, this is correct. The keyword "var" makes a variable local in scope, i.e. only accessible to the function (or rule) in which it's declared. As previously mentioned, all rules are functions, so anytime you're in a rule, variables declared with "var" are local to the rule. However, if you are in the JavaScript Globals, than all variables get global scope, whether you use the "var" keyword or not.

 

So in this case, the variable is local to the scope of the OnJobStart rule/function. There are two ways to fix this:

 

1. Move the declaration into the JavaScript Globals.

OR

2. Remove the "var" keyword

 

Personally, I prefer the first approach. Declaring all of your globals in the JavaScript Globals area makes them more explicit. And I recommend always declaring all variables with the "var" keyword unless you're specifically making a variable global, to avoid unintended carry-over of global values from one rule to another. (Unfortunately, JavaScript does not offer an "Option Explicit" setting like Visual Basic to require declarations for all variables.)

 

Note that you can always declare a variable in JavaScript Globals but initialize its value somewhere else. For example, you can do this in the JavaScript Globals:

// JavaScript Globals
var Classes_File; // still global even with "var"

And then in OnJobStart or another rule:

Classes_File = new ExternalDataFileEx("myfile.txt", "\t");

Also, if you're creating the ExternalDataFileEx object in a callback such as OnJobStart, you may find it helpful to add some code like this at the beginning of any rule that's using it:

if (FusionPro.inValidation)
 Rule("OnJobStart");

This ensures that you're opening the file at rule validation time in the Rule Editor, while avoiding multiple reads of the file at composition time.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...