fbpx
  • Another way of doing consulting.

Start » Tutorials and news » How to link a JavaScript file to make asynchronous requests from Sage X3

How to link a JavaScript file to make asynchronous requests from Sage X3

In this blog post, we will see how to make requests from sage to a JavaScript file asynchronously, as well as some advice to make life easier.

Make requests from Sage to a JavaScript file asynchronously

Create file structure

We start with the basics, create a new folder and create the following file structure:

Create file structure

It is in this image that we can already see the first trick for our developments, and that is, to make any changes to our scripts, if we only have the index, we would have to restart Syracuse for these changes to be effective. However, if in our index, we link a separate js (in our case, eaxexample-service._js) and we call it from the requireUncache function, our js will not be cached and we can update it.

function requireUncached(module) { delete require.cache[require.resolve(module)] return require(module) }

Perform asynchronous function

Another point to mention is that the extension used (_js) is to indicate to Sage that an asynchronous function is going to be performed, using the streamline.js package with the _ callback. More information in the corresponding sage's guide.

Once the file structure structure is done, we are going to write our package.json, in which we will declare our author, script name, version and description of the script and the module(s) to load:

Perform asynchronous function

Configure index

Finally, before we start building the api call, we'll configure index to call our service. To do this, within the js we create the exports.query function as follows:

exports.query = function(_, params) { let res; const EAXExampleService = requireUncached("./services/eaxexample-service"); const eaxexample = new EAXExampleService(); try { res = eaxexample.query(_, params); // Result of http response } catch (error) { //If any error happens, it's written inside a log fs.appendFileSync("C:/logpath/eax_log.txt", "n--------- --------------------------------------n[" + new Date().toLocaleString() + "] SUBMIT => Error Data: " + error); } return res }

Call the API

As we can see, inside the function, we first create a service by calling the script with our requireUncached function to make sure that it is loaded with the latest changes made. Then, the query is executed, passing the _ and the parameters that arrive from Sage as parameters. Finally, if there has been no error (in this case a log would be written to the specified path), it returns the response from the api.

Inside our service (eaxexample-service._js) we export the EAXExampleService class.

In the constructor of our class we will find the url and the path of the api to which we will make the request:

Call the API

Next, we create a small function to pass the arguments received from Sage to a string in order to pass it as a parameter to the URL:

Call the Sage X3 API

And finally, we create our query function, which is the one that will be in charge of making the call:

query = (_, args) => { let response = ''; let params, res; try { params = this.objectToQueryParams(args); // Declaring request options var options = { 'method': 'GET', 'url': this.HOSTNAME+this.BASE_PATH+params, 'headers': this.BASE_HEADERS, }; // Setting up the request response = httpClient.httpRequest(_, options).end().response(_); res = response.readAll(_); res = JSON.parse(res); returnres; } catch (error) { //If any error happens, it's written inside a log fs.appendFileSync("C:/logPath/eax_log.txt", "QUERY REQUEST ERROR - [" + new Date().toLocaleString() + "] " + error + "n"); res = { 'error': 'Error fetching query' }; } return res; } }

receive reply

As we can see, we use the httpClient library to make a request together with the previously configured options, passing it our callback “_”, immediately afterwards, we read its response and parse our response to an object.

Once again, if an error occurs somewhere in our query, we catch it and write it to our log.

Finally, in case we only want some data, we can create a function that filters our response before returning it.

If all went well, our response will be sent back to Sage.

Prepare/Modify script

Once we have written the json and the index, we open the path of the Node modules of our Syracuse (normally located in C:\SageSafeX3\SyracuseComponents\yracusebinnode_modules) and copy the newly created folder:

Prepare/Modify script

Then, we restart the Syracuse service and, once started, we will already have our script ready and, if we want to modify it, we will not have to restart the service again.

Call the JavaScript file

Finally, within the Sage script, to call the JavaScript file, we declare the arguments that we will pass to the EXEC_JS function

### Module, function and element to be returned Local Char MODULE(100) : MODULE = "zeaxexample/lib/index" # Script location Local Char FONCTION(30) : FONCTION = "query" #'getUserProfile' Local Char RETURNS(50) : RETURNS = "" ### The function is asynchronous Local Char MODE(4) : MODE = "wait" ### The callback argument is located in the first place for this function Local Integer CALLB : CALLB = -1 ### No Base 64 in or out coding, the arguments can remain empty Local Char B64_IN(20): B64_IN = "0" Local Char B64_OUT(20): B64_OUT = "0" ### No additional arguments Local Clbfile ARGUMENTS : ARGUMENTS = DATOS # Let's call the function STATUSCODE = func ASYRWEBSER.EXEC_JS(MODULE, FONCTION, MODE, ARGUMENTS, B64_IN, CALLB, RETURNS, B64_OUT, RESHEAD, RESBODY)

This function takes as parameters the file it needs (the ._js), the name of the function, if it needs to filter the response, if the function is asynchronous, if the response is encoded, and its parameters.

When that function has finished, the response from the api will be stored in the RESBODY variable.
 
 

Do you want to implement Sage X3 in your company?
We help you!

In Emiral We know how much your company is worth, whether it is small, medium or large. For this reason, we want to offer you the best ERP management solutions so that your business continues to grow.

Latest related posts