Rocket Modern Experience (formerly LegaSuite)

 View Only

 Read JSON file and decide whether to run VB script or JavaScript on load of server

Ameer Salman's profile image
Ameer Salman posted 07-17-2024 03:31

Hi,

I have a scenario, where I have config.json file in my resource folder and on load of the project , it must read the file and check the flag inside that. If the flag is true then the project should access only the JavaScript function on all the pages and if it is false then it must call only the VB Script function on all the pages. Can anyone provide a solution for this?

Below is the sample config.json file , I added for your reference

Note: "TRUE" for JavaScript function call and "FALSE" for VB Script function call
{
"functionKeysLoad"  : true,
"optionsSelect": false
}

 

Roger Van Valen's profile image
Roger Van Valen

Hi Ameer,

Reading the JSON file in JavaScript can be done easily, I can provide an example.

Also reading this in LegaSuite/Seagull script can be done. I can provide an example for that too if needed.

However, I am not sure about the intention to switch between the two.

Events on a page will be in either JavaScript or Seagull/LegaSuite script - these will need to be configured and linked to the page at the moment.

So completely doing all with one script or all with the other script, would still require to start the other script with a small check to see if that should should be executed.

Can you explain a bit more about your use case so we can give the best advice?

Regards,

Roger van Valen.

Ameer Salman's profile image
Ameer Salman

Hi Roger,

As a client requirement, The switch can be used if JavaScript functions aren't working in production, enabling us to access VB scripts and vice versa if negative scenarios occur.

Roger Van Valen's profile image
Roger Van Valen

Hi Ameer,

In order to execute JavaScript, your engine needs to have that capability. So I am assuming you might want to toggle the script for the purpose of script implementations and not so much for the script support of the product.

Do use the JSON to control that, you could for instance create a JSON file like this on a path like Resources/Configuration/scripttype.json:

{
"functionKeysLoad"  : true,
"optionsSelect": false
}

And create a structure to contain these values in LegaSuite/Seagull script, in my example I've names the structure Scripts/ScriptType.structure:

Structure ScriptTypeStruct
   dim functionKeysLoad as integer
   dim optionsSelect as integer
End Structure

This file can be read into a global variable, that can be used in both LegaSuite script as well as JavaScript.

Reading by JavaScript can be done for instance like this:

/* import the file system api for file handling */
import * as fs from 'mx:fs';

// Just a small example to read configuration data to a global variable
let fullpath = script.environment.pckgdir + 'Resources/Configuration/scripttype.json';
let file_contents = fs.readTextFile(fullpath);
let imported_persons = JSON.parse(file_contents);
script.global.gScriptType = new script.structure.ScriptTypeStruct(JSON.parse(file_contents));

console.log("script.global.gScriptType.functionKeysLoad=" + script.global.gScriptType.functionKeysLoad + ", script.global.gScriptType.optionsSelect=" + script.global.gScriptType.optionsSelect);  

And in LegaSuite script for instance using something like this:

Option Explicit

Global gScriptType as ScriptTypeStruct
	
Dim filecontents as string
Dim filename as string
Dim result as integer

filename = @"@&&WRKDIR@" + "/Resources/Configuration/scripttype.json"

result = FileLoad(filename, filecontents, -3)
If (result > 0) then
	' flags:
	' 0 (default) This flag specifies that any JSON valueis encoded. Note that this method is discourage as it may not be possibleto interact with other JSON systems.
	' 1 - Forces the script to fail. When the flag is notspecified, the script goes on and any errors are indicated via the global variable ERR.
	' 2 - Reserved.
	' 4 - Ignore Missing Data. This flag specifies thatstructure members are filled even when not all values are specifiedin the JSON string.
	' 8 - Convert types. This flag specifies that all non-matchingvalue types are converted.
	' 32 - Initialize script variable before filling itwith values from JSON string / xml. (default not set)
	result = ReadJSON(gScriptType, filecontents, 1 + 4 + 8 + 32)
	'return values:
	'0 - No error.
	'1 - Parsing error.
	'2 - Invalid JSON String.
	'3 - Invalid variable.
	'4 - Type mismatch.
	'5 - No data in JSON string matching structure member 'member_name'.

	if ((result = 0)) then
		MessageBox("Success", "Script type for functionKeysLoad=" + str(gScriptType.functionKeysLoad) + ", script type for optionsSelect=" + str(gScriptType.optionsSelect), 0, 0)
	else
		MessageBox("Error", "Something went wrong", 1, 0)
	end if
end if

To decide which script to run, you would need to add both JavaScript and LegaSuite script to your object events, and in the scripts decide.

For instance in a LegaSuite script:

If gScriptType.functionKeysLoad = 0 then
   <... do the work ...>
End If

And similar for the JavaScript:

if (script.global.gScriptType.functionKeysLoad) {
  <... do the work ...>
}

This approach would cause duplicate definitions for every event and every script in the two types though.

Hope this helps,

Regards,

Roger.