Advanced Tutorial: Automatic Delivery Charge Based on Distance via Google Maps


Try this constraint:

[=TN('{ENTITY DATA:Customer:Distance}')]  Greater  1

To be clear, the better one to use is the Show Message Action, not Display Popup Action.


Nope, unfortunately that hasn’t triggered anything. (I’ve been using the Show Message Action)

Out of interested I tried [=TN('{ENTITY DATA:Customer:Distance}')] Less 1 - This triggered on everything, regardless of distance


If you remove the constraint and put the above in Show Message, what do you see?



I also tried with the message as {ENTITY DATA:Customer:Distance} which showed up as this:


That cannot be converted into a number reliably, so that is why your constraint is failing.

You need to remove the “km” from the entity data.

You must use a pure number, not a string with characters such as “km” in it.


That mean you did not change the script from text to value if it show 8.2 km.



I’ve changed the script to var distance = json.rows[0].elements[0].distance.value;
It now shows up as:

It works! :grin:
@sukasem I did change the script earlier to .value (I even copied and pasted it into my above post). Strange.
Do you think we should edit the tutorial where the script says var distance = json.rows[0].elements[0].distance.text; and change it to distance.value?

One further question, now it shows 336km as 336246 - is it possible to change this format so it can display on the ticket lister only as 336km - solely because it is hard at quick glance to see how far the deliveries are, example below.


Distance = [=F(TN('{ENTITY DATA:Customer:Distance}')/1000,'0.00')] km
in ticket lister change 0.00 to 0 if you want just km no dicimal

I didn’t try it but should be something like that.


So that value is METERS. Maybe change your script so that it returns Km …

var distance = json.rows[0].elements[0].distance.value/1000; // divide meter value by 1000 to give Km


Wow awesome, thanks :grin:. I’m actually enjoying Sambapos more than my business :sweat_smile:

I even managed to edit the script to get duration time from google! At least it means at one glance I can quickly see how far deliveries are (although I won’t be able to tell the direction without opening the map), still should be useful


Yes, I think so because I think @pizzaeilat4 actually use Value not Text because distance in his screenshot is 4805.


btw less and greater operators converts data to numeric. (if it can). So TN usage is not needed.


Okay, just for anyone else out there using Imperial units (or out of curiosity). However I’ve just searched and found out that if the distance is returned as a value it always has to be in metric metres.


  • units=metric (default) returns distances in kilometers and meters.
  • units=imperial returns distances in miles and feet.
    Note: this unit system setting only affects the text displayed within distance fields. The distance fields also contain values which are always expressed in meters.

My plan: Make two custom fields -

  1. Distance (metric) which will use distance.value and delivery fee will automatically be calculated from there.
  2. Distance (imperial) which I will use distance.text to solely display the distance in miles/feet on the ticket lister so I have an idea of how far things are.

Sorry if i spammed - got a bit excited :relaxed:


Original script in Tutorial has been edited to use .value instead of .text so we won’t need to go over this issue again.

I assumed this too - thanks for confirming Emre.


Hi Team,

can you point me to right direction I’ have followed the tutorial and managed to impalement Automatic Delivery charge. I’m having minor issue with the Show message action, basically the distance message only appears when product is added to the ticket. I have missed something in the rules and can not pinpoint (I’m still learning).

No distance message displayed

Distance message only appears when adding a product,



A quick look at the Tutorial indicates that the Entity Data and Distance is set when the Event for Order Added to Ticket is triggered, so it is operating as designed.

You could possibly change that Rule to fire on the Event of Entity Selected and/or Ticket Entity Changed, however, since the Delivery Charge is based on a Percentage of the Ticket Total, it would not know how to calculate that when no Orders are on the Ticket.

So instead of chaning the Rule, maybe just add another Rule or Rules for Entity Selected and/or Ticket Entity Changed and fire the Show Message action in those Rules to display the Distance.

BEFORE YOU DO THAT … I see you have 2 Actions in your Rule which you named Delivery Message and Delivery Charge. What do they do? Show the Action for Delivery Message. You need to use a [:variable] in that Action for the parameter so you can feed a value into the Action from the Rule. Like this:


Here are the two actions.
Delivery Message Action displays the distance and Delivery Charge calculates 10% from the ticket amount.


You cannot do that. The Action (Show Message) cannot directly retrieve the value you have supplied. You need to supply the value from the Rule. So you must use a [:variable] in the Action “Message” Parameter and then feed the values into the Action from the Rules as I have shown in my previous post.

You will need to remove the Action from the Rule, then Save the Rule, then open the Rule and add the Action back in. Then supply your calculated value expression to the Action via the Rule.


I have changed to following and the behaviour is same. So the calculated value expression to the Action via the Rule need to be corrected.

Then supply your calculated value expression to the Action via the Rule.

can you show how to calculate the value expression for following example.

10% of ticket total
Fixed price of £5 to all deliveries

Thank you