Rocket Modern Experience (formerly LegaSuite)

 View Only

 Call an Engine Side script function from the client side script function

Ameer Salman's profile image
Ameer Salman posted 07-15-2024 07:08

Hi,

I have created an engine-side JavaScript file as a class-based script with a function called 'search'. I am attempting to call the 'search' function from the client-side JavaScript, but the function is not being invoked. Can anyone provide a solution for this?

Roger Van Valen's profile image
Roger Van Valen

Hi Ameer,

To call an engine side JavaScript function from the Client, the function needs to be connected to the 'globalThis' object.

Also, the API needs to be opened up, and allow to call this particular function.

As example, I made a simple 'search'  function in a engine side JS file to create a search function on the globalThis:

/**
* Engine Javascript
*/

globalThis.search = async function(searchstring) {
	console.log('Searching for "' + searchstring + '"');
	
	return 'I have found nothing!'; 
}

I have connected this script to the page onLoad event to make sure it is executed before I call the script from the client script.

I've also opened up the API, and allow the 'JavaScriptFunctionCall' and the 'search' to be called from the API. 

To do this, I added the following in my deployment.properties:

servlet.host_api=true
servlet.host_api_access_control_order=deny-allow
servlet.host_api_access_control_deny=*
servlet.host_api_access_control_allow=JavaScriptFunctionCall,search

Now I can create a client side script to call the engine side script using the API, like this:

/**
* Client Javascript
*/

function CallEngineJSfromClientJS() {
	const hostApi = ajaxclient.HostFieldApi.getInstance();

	hostApi.javaScriptFunctionCall('search', 'SomethingToSearchFor', (ret) => {
		console.log('javaScriptFunctionCall completed, returned', ret);
	});
}

I connect this as a client side javascript to a button on the OnCommand event.

When I run this and click the button, my engine console will show:

INFO  javascript.console - Searching for "SomethingToSearchFor" 

And in my browser console log, I can see:

javaScriptFunctionCall completed, returned I have found nothing!

I hope this helps,

Regards,

Roger van Valen.