MarcomCentral (PTI) and FusionPro User Communities

MarcomCentral (PTI) and FusionPro User Communities (http://forums.pti.com/index.php)
-   Graphic Rules (http://forums.pti.com/forumdisplay.php?f=33)
-   -   If the Resource doesn't exist... (http://forums.pti.com/showthread.php?t=5315)

geeool March 15th, 2019 11:51 AM

If the Resource doesn't exist...
 
Hi,

This is for an online product. I'm using FusionPro Creator 10.0.6 on Windows.


I am programming tags for people to customize and order online through a MarcomCentral site. There are 100 or so different tag backgrounds that are being called in based on the SKU number the buyer enters. I have these backgrounds setup as resources and they are named with the SKU number (for example: 356304.pdf).

If the SKU number entered doesn't match one of the resources, then I want it to return error.pdf instead. My rule works fine if the resource is present, but it doesn't return the error.pdf if the resource is missing. Below is the code. Any suggestions would be appreciated.


Code:

var tagImage = Resource(Field("SKU") + ".pdf", "graphic", true);

if (tagImage.exists) {
    return tagImage;
    }
else    {
    return Resource("error.pdf");
    }


Dan Korn March 15th, 2019 01:17 PM

Re: If the Resource doesn't exist...
 
Do you actually have a NAMED resource with the RESOURCE name (not just the file name) error.pdf in your FusionPro job, in the Resources dialog? If not, then you need to change Resource on the next-to-last line of the rule to CreateResource. Either way, you also need to make sure that you have a file with the file name error.pdf in a MarcomCentral image library that's used by the job for the MarcomCentral composition.

Also, questions like this which are specific to the MarcomCentral application should really be asked in the MarcomCentral sub-forum.

geeool March 18th, 2019 12:37 PM

Re: If the Resource doesn't exist...
 
1 Attachment(s)
Hi Dan,

I've stripped everything but the essentials out of my FusionPro file and I'm running it from my desktop, but I'm still having the problem. I do have a resource called error.pdf (see my collected files). Any insight you can give me is appreciated.


Thanks,

Dan Korn March 18th, 2019 01:47 PM

Re: If the Resource doesn't exist...
 
I see. The problem is on the first line:
Code:

var tagImage = Resource(Field("SKU") + ".pdf", "graphic", true);
You're mixing up the usages of two different functions: Resource and CreateResource.

The Resource function takes only a single parameter: the name of a resource.

The CreateResource function (or new FusionProResource constructor function) requires one parameter (a file name), but can also take up to three other optional parameters: (2) the resource type, (3) a boolean "nonExistOK" value which denotes whether to set the .exists property instead of throwing an exception if the file is not found, and (4) the encoding (for text resources).*

Since you're calling the Resource function, that last parameter that you're setting to true has no effect, and if a named resource with the specified name has not been added to your FusionPro template, an exception is thrown, and the rules doesn't set any value in the InsertTag_RULE variable, as you can see if you do a full composition (not a Preview) and view the log file:
Quote:

uncaught exception: Error: In Resource(), no resource named 35630A.pdf
So my first bit of advice if you have a problem like this in the future is:
ALWAYS look in the log file.

There are two ways to fix this:

1. Put the Resource function call into a try block, so that when it fails to find the specified resource (by resource name), you can catch the exception, like so:
Code:

try
{
    return Resource(Field("SKU") + ".pdf");
}
catch (e)
{
    return Resource("error.pdf");
}

2. Change that first call to CreateResource, so that if the file name specified is not found, the .exists property is set instead of an exception being thrown:
Code:

var tagImage = CreateResource(Field("SKU") + ".pdf", "graphic", true);
if (tagImage.exists)
    return tagImage;
//else
return Resource("error.pdf");

I recommend solution number 2, because then you don't have to keep adding resources to the job in FusionPro. You can just add graphics to the image library in MarcomCentral, and they will be found by their file names.

* Note that the Resource function doesn't have the extra parameters that CreateResource does, because Resource deals with named resources in the template, and you specify the types and encodings of named resources in the Resource Editor dialog. Note that the .exists property of a resource can be set to true for a resource returned from either function (Resource or CreateResource), but the Resource function throws an exception only if there's no resource with the specified resource name. If there is a resource with the specified resource name defined in your template, then the Resource function won't throw an exception, but it may set the .exists property to false if the file for that named resource is not found. The CreateResource function, on the other hand, throws an exception right away if the file name specified is not found, unless a value of true is passed in the third parameter.

geeool March 19th, 2019 08:33 AM

Re: If the Resource doesn't exist...
 
#2 worked. Also, thanks for explaining everything. It's always helpful to have a detailed response to better understand what is happening.


All times are GMT -7. The time now is 11:08 AM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.