Jump to content

Missing Field handling error


ThePorge

Recommended Posts

I get data files that sometimes have and ID. If this ID exists it will be what I need to use. However if no ID is in the data file it won't even have the field for the ID data. I then have to generate an ID based on the input file name. Is there a way to handle a field name not being present in the data file without throwing an error?

I get these data files all the time and I have to link up to the new file and output a pdf. Right now it looks like the only solution is to put the field name in the data, but that's a hassle with the frequency of doing this.

Link to comment
Share on other sites

I get the error

"uncaught exception: Error: In Field(), no field named Order Number"

I'm trying to get the first record of the data to output if the input data file ends with ".txt" otherwise to output all the records which will come as an xls file.

The xls file will have the data column header "Order Number"

 

//Set page usage
FusionPro.Composition.SetBodyPageUsage("1Title", false);
FusionPro.Composition.SetBodyPageUsage("2Title", false);
if(Field("Division")==""){
  FusionPro.Composition.SetBodyPageUsage("1Title", true); 
}else FusionPro.Composition.SetBodyPageUsage("2Title", true);

//Pull the job number & order number from the input data file name
//Name needs to have the Job Number first e.g. 123456_86912.1.MyDataFile.txt
//FusionPro.Composition.forcePreprocessing = true; set in onJobStart

var jobNumber = GetFileName(PrimaryInputFile()); //Works in Preview, Validation & Composition mode unlike the old version that works in Composition only
var orderNumber = GetFileName(PrimaryInputFile());
var interNumber = GetFileName(PrimaryInputFile()), myTest;

jobNumber = jobNumber.match(/^\d+/);
interNumber = interNumber.match(/\_[\d\.]+/);

if (orderNumber.match(/\.txt/) == ".txt" && FusionPro.Composition.currentOutputFileNumber == 1){
   FusionPro.Composition.startRecordNumber = 1;
   FusionPro.Composition.endRecordNumber = 1;
   FusionPro.Composition.OpenNewOutputFile(jobNumber + interNumber + Field("Name") + "." + FusionPro.Composition.outputFormatExtension);
   var myTest = "Fail";
} else {
   orderNumber = Field("Order Number");
}

if (myTest != "Fail" && FieldChanged("Name")) {
   FusionPro.Composition.startRecordNumber = 1;
   FusionPro.Composition.endRecordNumber = FusionPro.Composition.totalRecordCount;
   FusionPro.Composition.OpenNewOutputFile(jobNumber + "_" + orderNumber + "." + Field("Name") + "." + FusionPro.Composition.outputFormatExtension);
}

Yeah, I know it's kinda sloppy and I attempt to clean everything up once I get it working....

Link to comment
Share on other sites

I see. You can always put a try/catch block around the call that's throwing the exception, like so:

try
{
   orderNumber = Field("Order Number");
}
catch (e)
{
   orderNumber = "(not sure what you want to happen here)";
   // or just do nothing here in the catch block
}

You could even give it a list of field names to try, something like this:

var val = null;

var fields = ["Order Number", "OrderNumber", "OrderNum"];
for (var f in fields)
{
   try
   {
       val = Field(fields[f]);
       break;
   }
   catch (e)
   {
   }
}

if (val == null)
   throw "No matching field name; tried: " + fields;

return val;

This is kind of a poor man's field mapper.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...