Copy link to clipboard
Copied
Using the excellent code examples found on Thom Parkers www.pdfscripting.com site I created a PDF form that calculates a URL for a Net.HTTP.request to a PHP file that queries a MySQL database for a specific price.
The attached screenshot shows the form with results in the console for 3 queries with different sets of parameters assembled from the drop-down meus.
My question is how do I take the value that is being written to the console and insert it in to a form field so it can be used in further calculations.
The example scripts can be found in the "Load Remote Data with HTTP" section in this post...
https://www.pdfscripting.com/members/Acquiring_Raw_File_Data.cfm#Scripts
Copy link to clipboard
Copied
The usual way is to set the value attribute of a field object obtained by name with getField.
Copy link to clipboard
Copied
Exactly. I tried the "this.getField("QueryResult").value = util.stringFromStream(oStream) approach but no joy. I think it has to do with not being able to reference the target field using the "this" keyword in a function that is in in the Folder Level automation file below...
var GetRemoteDataFile = app.trustedFunction(function(cFileURL, OnHttpResponse, OnHttpError)
{
app.beginPriv();
function ResponseHandler(oRequest, cURL, oException, aHeaders)
{
if(oException)
{
if(arguments.callee.OnError && (typeof(arguments.callee.OnError) == "function"))
arguments.callee.OnError(oException);
}
else if(arguments.callee.OnResponse && (typeof(arguments.callee.OnResponse ) == "function"))
arguments.callee.OnResponse(oRequest);
};
ResponseHandler.OnResponse = OnHttpResponse;
ResponseHandler.OnError = OnHttpError;
Net.HTTP.request({cVerb:"GET",cURL:cFileURL, oHandler:{response:ResponseHandler} });
app.endPriv();
});
function OnMyReturn(oStream)
{
console.println("Price=" + util.stringFromStream(oStream));
}
function OnMyError(oExcept)
{
console.println("** ERROR #" + oExcept.error + ": " + oExcept.msg);
}
Copy link to clipboard
Copied
Also, thanks for your earlier response. I read your reply to an earlier question...
How can I connect to an API and import JSON or XML... - Adobe Support Community - 9221891
...where you wrote "The response from the request is passed to the function you define. So, msg, url and e are defined in that function - and nowhere else. If you want to read those values later you have to copy them somewhere." which sounds like my issue.
Any idea how to do that from a funcion contained in a Folder Level automation script?
Copy link to clipboard
Copied
You have the document object when you execute the function GetRemoteDataFile.
Copy link to clipboard
Copied
OK, but how do you return the result from the GetRemoteDataFile function to a form field?
Copy link to clipboard
Copied
Pass the document object as a parameter to any document level function. Standard practice because of limitations of "this".
Copy link to clipboard
Copied
OK. How is that done?
Copy link to clipboard
Copied
I added a global variable to hold the function's result in the inside the function...
function OnMyReturn(oStream)
{
console.println("Price=" + util.stringFromStream(oStream));
global.QueryResult = util.stringFromStream(oStream);
}
After running the function I tried to set the form field named "QueryResult" using "this.getField("QueryResult").value=global.QueryResult;"
but get the following error...
NotAllowedError: Security settings prevent access to this property or method.
Global.QueryResult:1:Field QueryResult:Mouse Up
Any insights would be greatly appreciated.
Copy link to clipboard
Copied
There are strict restrictions on the global object, as described in the documentation. You are probably better off setting a normal full-scope variable (by just using it without a var declaration BEFORE calling the function).
Copy link to clipboard
Copied
OK. I changed...
function OnMyReturn(oStream)
{
console.println("Price=" + util.stringFromStream(oStream));
global.QueryResult = util.stringFromStream(oStream);
}
to...
function OnMyReturn(oStream)
{
console.println("Price=" + util.stringFromStream(oStream));
QueryResult = util.stringFromStream(oStream);
}
and changed the QueryResult javascript action to...
this.getField("QueryResult").value=QueryResult;
No errors are generated but the value of the QueryResult variable isn't returned either.
Copy link to clipboard
Copied
I do not see the code where you are setting the variable. Anyway, isn't this an asynchronus return that will happen long after you try to copy the value? You do seem to be going in a different direction to the ones we suggest, I refer you specifically to "Pass the document object as a parameter to any document level function".
Copy link to clipboard
Copied
How do I do that? I've tried every possible combination but can't figure it out.