SambaPOS API Integration with NewBook PMS/Booking System


Too bad that doesn’t seem to work for Show Message or Ask Question actions. :wink:


It also doesnt work to well with multi monitor :stuck_out_tongue:


lol I meant to give an alternative to saving response content to file. On big popup dialogs you can ctrl+c to paste content to notepad. I’ve quickly reviewed posts so sorry if I’ve misunderstood the use of saving to file.


No, it is really what we were looking for @emre… we wanted to capture the JSON output to post here in the forum. I didn’t know Ctrl-C trick worked for that, so I suggested writing it to file.


The write to file did the trick though :smile:

Does anyone have a sample script doing a multiple field update script they can post for me to breakdown and learn from?


What does it mean? I totally missed the conversation :slight_smile:


I expected as qmckay confirmed the best method to pull/update info from the Api would be to pull a full update routinely rather than individually on demand.
One of there rest request urls returns a full site (room) list with sub arrays for each booking.
I will need to break this down to per room and pull the needed data from the needed lines.
Using the Api. Helper to update the fields of the entity fields for info like room/booking status (checked in or not) guest name, allowance etc in to custom entity fields which I can then use in other rules and settings like guest name into custom format entity format an sundry info to improve guest experience like pax, arrival date so when serving you can see when they checked in etc to spark up some small talk etc.

Just wounderd of anyone had any scripts performing multiple fields or more interestingly multiple entity loop type update which I can read through and learn from :slight_smile:


Everything that has to do with these type integration s are yours… and a little of my TimeTrex. You are pioneer right now :stuck_out_tongue:

You will be the one others learn from later :stuck_out_tongue:


Can you post a sample response as a reference here?


It looks like this… here are 2 records with some sub-data in child elements… it was all a single line, so I added some line-breaks for readability. The original data was about 71000 characters.

,"booking_arrival":"2013-11-28 10:30:00"
,"booking_departure":"2016-08-24 10:00:00"
,"booking_source":"Driving By"
,"booking_placed":"2013-11-28 10:32:50"
,"category_name":"Permanent Cabin"
,"guests":[{"firstname":"RAY AND JAMES"
,"street":"71 Lions Dr"
,"state":"New South Wales (NSW)"
,"content":"0487 637 236"}]}]}

,"booking_arrival":"2013-11-27 14:00:00"
,"booking_departure":"2016-08-23 10:00:00"
,"booking_source":"Top Tourist Guide"
,"booking_placed":"2013-12-03 13:22:32"
,"category_name":"Permanent Cabin"
,"street":"Lions Drive"
,"state":"New South Wales (NSW)"

    "booking_id": 11033,
    "booking_arrival": "2015-09-13 14:00:00",
    "booking_departure": "2015-09-14 10:00:00",
    "booking_status": "Arrived",
    "booking_adults": "2",
    "booking_children": "0",
    "booking_infants": "0",
    "booking_animals": "0",
    "booking_total": "99.00",
    "booking_method": "",
    "booking_source": "",
    "booking_reason": "Business",
    "booking_demographic": "None",
    "booking_placed": "2015-08-21 16:59:27",
    "tariff_name": "BOOK NOW",
    "tariff_total": "99.00",
    "discount_name": "None",
    "discount_total": "0.00",
    "account_id": "14042",
    "account_balance": "99.00",
    "auto_billing_limit": null,
    "site_id": "1",
    "site_name": "101",
    "category_id": "1",
    "category_name": "Single Room",
    "guests": [
                "firstname": "John",
                "lastname": "Doe",
                "street": "",
                "city": "",
                "state": "Queensland (QLD)",
                "postcode": "",
                "country": "Australia",
                "account_id": "13783",
                "account_balance": "0.00",
                "auto_billing_limit": null,
                "primary_client": "1",
                "contact_details": [
                                    "type": "mobile",
                                    "content": "0411111111"
"type": "email",
"content": ""


OK I hope I’m getting what you’re asking for.

First of all this is full reference for JScript language. You can find a lot of samples here.

Some brief information about our sample may help.

  1. JSon is a string data formatted with a markup language. To be able to parse that easily you should convert string data to object data. You’ll use JSON.parse to convert string to object and JSON.stringify to convert object to string (for posting JSON).
  2. When you see a square bracket in JSon that means it is an array and holds multiple values. You’ll use an indexer (like guests[2]) to access items.
  3. resonseObject[0] returns first booking.
  4. responseObject[0].booking_id is id for first booking.
  5. responseObject[1] returns second booking.
  6. responseObject.length returns booking count.
  7. responseObject[1].guests[2].firstname is first name of third guest of second booking.
  8. you can loop through values with for statement.
  9. you can create nested for blocks to loop through nested items. For example you can loop through all guests of all bookings.

Loop Sample

var responseObject = JSON.parse(response);
var result = "Guest Names: ";
for(var i = 0;i < responseObject[0].guests.length;i++)
   result += responseObject[0].guests[i].firstname+ " ";
return result;

This for loop joins all guest names of first booking to a single string.

Nested loop sample.

for(var bookingIndex = 0; i < responseObject.length;i++)
  `for(var guestIndex = 0; i < responseObject[bookingIndex].guests.length;i++)
      result += responseObject[bookingIndex].guests[guestIndex].firstname+ " ";


Thanks emre, that is very useful, have also gone back and looked as the JSON.stingify which is very tidy;
Here is my testing code, although still using the single room data from single room request URL, getting to grips with it before tackling arrays LOL;
What do you think;

entitysitenametest('Room 1')

function entitysitenametest(room)
//Site Name Call from Entity Data
 var sitename = api.Entity(room).Data('NewBook Site Name').Get();

//URL and HTTP Auth Details
 var url = '';
 var username = 'sambapos';
 var password = 'xxxxxxxxxxxxxxxxxxx';
//JSON Request Data
 var requestdata = new Object();
 requestdata.api_key = "instances_xxxxxxxxxxxxxx";
 requestdata.site_name = sitename;
 var requestjson = JSON.stringify(requestdata);
//JSON POST and Response
 var response = web.PostJson(url,requestjson,username,password);
 var responseObject = JSON.parse(response);
//Response Processing
 var result = responseObject.account_id;
 api.Entity(room).Data('NewBook Booking AccountNo').Update(result)
 return result

And its works as expected so far :slight_smile:


Seems great. As we improve api helper we’ll have more possibilities. Keep going.


OK, so am just trying to work out a way to ‘search’ the bookings arrays for the appropriate ‘site_name’.
There might be a berry way you can sugest but was looking to try and use an if in the loop, - this might show what i mean better.

If this gives a list of site_names;

 var result = 'jsonarray'
 for(var i = 0;i < responseObject.length;i++)
  result += responseObject[i].site_name;

I was hoping maybe I could use the if to go though the array values and return the ‘i’ value of the corasponging button which could then be fed into the api update values (on another loop) does that make sense?

So my thought was to try something like this; (where sitename is variable pulled from entity data as a site_name reference to website site_name)

 var result = 'jsonarray '
 for(var i = 0;i < responseObject.length;i++)
  result += if (responseObject[i].site_name == sitename) {i;};
 return result;

This gives a syntax error result.

Does what im doing make sense?
If there is a cleaner solution please let me know, if not can I use an if in this manor to ‘search’ a loop for specific value?


Is this what you’re going for?

var result = 'jsonarray'
var sitename= 'DCxxx';
var siteindex;
for(var i = 0;i &lt; responseObject.length;i++) {
  if (responseObject[i].site_name == sitename) {
    siteindex = i;
return siteindex;

The break statement stops the loop. You could remove it, but leaving it in there will speed things up.


Urmmmm, guve me a minute to absorb that LOL
Looks like thats what im trying to achive… will try it out now.

was it the ; break (false value to end look i was missing?)


presuming the ‘and lt’ is a mishandeling of <?

AWESOME :slight_smile: thanks changed that and worked!!


Thanks, really getting somewhere now;

bookinglist('Room 1')

in to

//Response Processing Loop for Array Index
 var nameofsite = sitename;
for(var i = 0;i < responseObject.length;i++) {
 if (responseObject[i].site_name == nameofsite) {
    var siteindex = i;

//Entity Data Updating Using Aray Index
 var result_status = responseObject[siteindex].booking_status;
 var result_guestname = responseObject[siteindex].guests[0].firstname+' '+responseObject[siteindex].guests[0].lastname;
 var result_arrival = responseObject[siteindex].booking_arrival;
 var result_departure = responseObject[siteindex].booking_departure;
 var result_guestsqty = responseObject[siteindex].booking_adults;
 var result_bookingaccount = responseObject[siteindex].account_id;
 api.Entity(room).Data('Guest Status').Update(result_status);
 api.Entity(room).Data('Guest Name').Update(result_guestname);
 api.Entity(room).Data('Arrival Date').Update(result_arrival);
 api.Entity(room).Data('Departure Date').Update(result_departure);
 api.Entity(room).Data('No. Guests').Update(result_guestsqty);
 api.Entity(room).Data('NewBook Booking AccountNo').Update(result_bookingaccount);
  return result_guestsqty;

Gives me this :-);

So next challenge is is to loop that whole section of code for each Room entity…

FYI site DC001 was array index 57 :smile:


Since the existing processing section is based upon room from the function vale where I was specifying ‘Room 1’


which then pulled “site_name” reference from field ‘NewBook Site Name’

In theory looping that whole section with
var room = [each room name in turn] should update all rooms i think…

Just need a loop to fire one room name at a time into the code.

I cant see any of the above api helpers being any use for this, am not sure where to start with this bit.
Will some SQL be needed to pull full list of room entities from the database?
Would prefer to keep it semi flexible rather than specify a list of room names.

@QMcKay any suggestions on a loop of Rooms Names (entities)?

Can you run functions within functions? To keep things clean if the ( sitename search loop --> index --> update fields using index ) was in function process(room) and then function process(roomname) was fired from with an entity name listing loop?
Hopefully nesting/function within function call is posable as have just put together a ‘Cecked Out’ script to clear guest data and set an ‘Account=NonChargable’ state which would hopefully work with the look as another if, something like;

if ( responseObject[roomsiteindex].booking_status == 'Arrived') {