Reading a Global Setting in a script

You will only need the “GQL” functions or the “SQL” functions; not both. This is just to illustrate different ways to get the same result. The “SQL” functions were the only way to do this via JScript before GraphQL came into existence. Now my preference is the “GQL” methods.

Name: Update Setting
Handler: setting

function readGlobal(settingName,method) {
	settingName  = typeof settingName  === 'undefined' ? '' : settingName;
	method  = typeof method  === 'undefined' ? 'GQL' : method;
	
	var returnVal = "UNKNOWN";

    if (method=='SQL') {
	  returnVal = readGlobalSQL(settingName);
	} else {
	  returnVal = readGlobalGQL(settingName);
	}
	
	return returnVal;
}

function readGlobalSQL(settingName) {
	settingName  = typeof settingName  === 'undefined' ? '' : settingName;
	
	var qry = "";
	var res = "";
	var currentValue = "UNKNOWN";
	var newValue = "";
	
	if (settingName != '') {
	  qry = "SELECT [Value] FROM [ProgramSettingValues] WHERE [Name] = '" + settingName + "'";
	  res = sql.Exec(qry);
	  currentValue = res[0];
	}

    return currentValue;
}

function readGlobalGQL(settingName) {
	settingName  = typeof settingName  === 'undefined' ? '' : settingName;
	
	var qry = "";
	var res = "";
	var currentValue = "UNKNOWN";
	var newValue = "";
	
	if (settingName != '') {
      qry = '{setting:getGlobalSetting(name:"'+ settingName + '"){name,value}}';
	  res = gql.Exec(qry);
	  res = JSON.parse(res);
	  currentValue = res.data.setting.value;
	}

    return currentValue;
}

function updateGlobal(settingName,settingValue,method) {
	settingName  = typeof settingName  === 'undefined' ? '' : settingName;
	settingValue = typeof settingValue === 'undefined' ? '' : settingValue;
	method  = typeof method  === 'undefined' ? 'GQL' : method;
	
	var returnVal = "UNKNOWN";

    if (method=='SQL') {
	  returnVal = updateGlobalSQL(settingName,settingValue);
	} else {
	  returnVal = updateGlobalGQL(settingName,settingValue);
	}
	
	return renturnVal;
}


function updateGlobalSQL(settingName,settingValue) {
	settingName  = typeof settingName  === 'undefined' ? '' : settingName;
	settingValue = typeof settingValue === 'undefined' ? '' : settingValue;
	
	var qry = "";
	var res = "";
	var currentValue = "";
	var newValue = "";
	
	currentValue = readGlobalSQL(settingName);
	
	if (settingValue == 'increase') {
		newValue = Helper.ToNumber(currentValue) + 1;
	} else if (settingValue == 'decrease') {
		newValue = Helper.ToNumber(currentValue) - 1;
	} else {
		newValue = settingValue;
	}
	
	qry = "UPDATE [ProgramSettingValues] SET [Value]='" + newValue + "' WHERE [Name] = '" + settingName + "'";
	res = sql.Exec(qry);
	
	return newValue;
}

function updateGlobalGQL(settingName,settingValue) {
	settingName  = typeof settingName  === 'undefined' ? '' : settingName;
	settingValue = typeof settingValue === 'undefined' ? '' : settingValue;
	
	var qry = "";
	var res = "";
	var currentValue = "";
	var newValue = "";
	
	qry = '{setting:getGlobalSetting(name:"'+ settingName + '"){name,value}}';
	res = gql.Exec(qry);
	res = JSON.parse(res);
	currentValue = readGlobalGQL(settingName);
	
	if (settingValue == 'increase') {
		newValue = Helper.ToNumber(currentValue) + 1;
	} else if (settingValue == 'decrease') {
		newValue = Helper.ToNumber(currentValue) - 1;
	} else {
		newValue = settingValue;
	}
	
	qry = 'mutation m {setting:updateGlobalSetting(name:"'+ settingName + '",value:"' + newValue + '"){name,value}}';
	res = gql.Exec(qry);
	res = JSON.parse(res);
	newValue = res.data.setting.value;
	
	return newValue;
}

Usage:

setting.readGlobal('mySettingName')
setting.updateGlobal('mySettingName','mysettingValue')

3 Likes

Thank you I will work through this to make sure I understand what is happening here.

So guys have learnt a bit more about scripts but having some problem with Posting JSON.

The script below works fine until trying to Post data to Beds24 api.

The script collects the exchange rate, applies it to the currency setting in Samba and applies it using the var rate to the JSON script I am trying to Post to the Beds24 API.

the variable jsondata is a formatted JSON string which I have posted to the Beds24 page both manually and with Postmaster. Both methods work fine and update the Beds24 site accordingly.

Nothing happens with Sambapos and something is wrong with the script as my final Test message does not display.

This works

and this works

But my attempt to Post this working JSON data through SambaPOS does not work.

So the samba post is t working or the rate in samba isn’t updating?

The samba post is not working. The script for the rate is good and updates in Samba. Maybe should put this under a different topic?

Everything works until I get to web.PostJson

Personally I would return the postdata and drop in json validator site to first check the string is correctly formatted.

Do you get nothing in your response?

One this you might want to check is the TLS version required by the API
Newbook upped their minimum for PCI reasons which requires a registry edit for .net to force higher security level.

See

Now I am even more confused. The variable jsondata validates in Postman and returns a value as follows

{
“modifyProperty”: [
{
“template4”: “158.88”
}
]
}

which is what I expected to see (setting template4 with the value defined by currency rate). Also direct input of the variable into the text box of the API webpage also works perfectly.

If I paste the jsondata into different Json validation sites it gives me different responses and I cannot understand what I need to do.

Is there an alternative URL I can use from SambaPos which will show me exactly what web.PostJson is posting (if it is actually posting anything)

JSONlint.com tells me it is valid JSON. My worry is web.PostJson in Samba is somehow changing the output.

I put the variable jsondata into JSONlint.com as below

Pressed the validate button and got the following

So it validated the structure of the variable and then tarted it up for me also. if sambaPOS is posting the variable unchanged, there is no reason this should not work.

Incidentally I copied the tarted up version of the Json string into my samba script and it completely broke the script execution.

Yer, you can’t do multiple lines raw like that is that’s what you mean.
So formatting is good.
Did you look at the top security suggestion?

So I am officially stuck.

I can post the jsondata string to the API with postmaster with website authorisation and without. Works fine.

I can post the jsondata string to the API with Postmaster as Raw body data or as Json. Works fine

I have checked the Registry entries on the server and terminal to ensure that the default TLS version is correct (it was already set as default value (1)

Within SambaPOS I have tried web.PostJson with authorisation and without . Does NOT work

Within SambaPOS I have web.Post Data with authorisation and without. Does NOT work

Last check I have done was to use online API tester at apitester.com . Input the url and string copied and pasted from my script and got this result

The data has also updated on beds24 account. So this works.

Cannot see why SambaPOS does not work. This is pretty fundemental and a simple element that I need to see working before I can build the full POS to PMS API link.

but what response do you get from these?
even if auth or api key etc were incorrect typically a good api should return erro saying whatever the issue is, so if you get nothing its presumably not even getting to their api…
Ar you sure about the TLS? Even on the till that updated itself to windows 10 I still had to edit both registy values… also make sure you restart after changing registry

I am not sure if SambaPos even posts from the script as when it gets to this point the script just drops out to the next action in my rule (Logout). I put a message at the end of the script but this never gets displayed. Message before the web.postjson is always displayed.

The API responds with success and the updated values when I post from Postmaster.

I will have a deeper look into the TLS but I didnt have to edit the values as they were already default (1). I am running this script on a test database using the server and one terminal. There are other terminals on the network but these are connected to the live database. Should I check these?

Looking closer your script looks to have bracket issues. That or your missing an else for the if… You have }{ in the miggle after SQL and before post prep likes… Suprised this doesn’t crash or throw and error

Without one or both of these brackets the script does not work.

Script currently executes all lines and displays messages right up until the web.Post line and then crashes. Is there a way to see the script debugger because it tells me nothing. The script Test button is also non operational.

For test you should put in a sample function call with variables quoted and it should pop-up return value or error.

So took on board what you said about the script, cleaned it up, took out superfluous code and brackets, refined the variables and added some dialog to tell me how far we get.

Script works fine, outputs the correct format and information. Every dialog message until the return response line works perfectly. Script exits before the last Dialog Message and DOES NOT post anything.

It is ridiculously easy to post the json string created by this script with Postmaster, online JSon posting or direct input through the API page. SambaPOS will not POST this string. I have tried PostJson, PostData, Upload Data. Nada.

I am beyond frustrated now because of the amount of work I have done with SambaPOS over the last 2 years to tailor to our business (very successfully) and always with the aim of integrating with the right PMS API when I eventually found it. I like the SambaPOS system so much but fundamentally it does not seem to be able to post Data to a quite simple API.

Are you not missing some semmi colons on those last few lines?

Yea spotted those after posting picture. dlg.Mesaage have all had semi colons added but result is the same unfortunately.

web.PostJson implemented for a specific case that requires basic authentication. (remember web pages that displays user name and password dialog when visited).

You can use webclient class to read / post data. If you search for “webclient” or “uploadstring” on the forum you’ll find a lot of usage examples.

2 Likes

Is there some element of my Samba/Server that needs to be activated/enabled to facilitate any Web.Client activity.

Web.Client not working - Have tried a number of scripts for this and it always kiks out the script
Web.Upload not working
Web.PostJson not working
Web.PostData not working

In order to identify if its a problem with the API I have tried posting with all these options to a tame API website called Reqres. none of them even get as far as posting anything to the API.

Something fundemental is not working with Web.Anything