Net.HTTP.request - Return Query Result to Form Field

New Here ,
Sep 04, 2022 Sep 04, 2022

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. 

 

PDFPriceLookup.jpg

 

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

TOPICS
JavaScript , PDF forms

Views

126

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 04, 2022 Sep 04, 2022

Copy link to clipboard

Copied

The usual way is to set the value attribute of a field object obtained by name with getField.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 04, 2022 Sep 04, 2022

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);
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 04, 2022 Sep 04, 2022

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?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 04, 2022 Sep 04, 2022

Copy link to clipboard

Copied

You have the document object when you execute the function GetRemoteDataFile.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 05, 2022 Sep 05, 2022

Copy link to clipboard

Copied

OK, but how do you return the result from the GetRemoteDataFile function to a form field?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 05, 2022 Sep 05, 2022

Copy link to clipboard

Copied

Pass the document object as a parameter to any document level function. Standard practice because of limitations of "this".

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 05, 2022 Sep 05, 2022

Copy link to clipboard

Copied

OK. How is that done?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 05, 2022 Sep 05, 2022

Copy link to clipboard

Copied

I added a global variable to hold the function's result in the inside the function...

 

javascript01.jpg

 

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 05, 2022 Sep 05, 2022

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).

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 05, 2022 Sep 05, 2022

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.

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 05, 2022 Sep 05, 2022

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".

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 05, 2022 Sep 05, 2022

Copy link to clipboard

Copied

LATEST

How do I do that? I've tried every possible combination but can't figure it out.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines