blog

RPC-Calls-in-Odoo-POS-blog
News & Events

RPC Calls in Odoo POS

Point of Sale

Odoo’s Point of Sale (POS) application is beautifully designed and a visual treat. The user experience is great, and it is pretty easy to be good at using the application. It stored most of the data of the POS in the browser itself until it was sent to the backend. One of the major advantages of Odoo’s POS system is that it can work in an offline mode with most of the features working.

Remote Procedure Call

What if we need some data from the database in POS? Or what if we need to create a record in the database from POS?

That’s where Remote Procedure Call RPC comes in.

  • It can utilise remote Procedure Call to interact with the backend.
  • We can execute a method in a particular model or call any default ORM methods on a model from Odoo’s Point of Sale application itself, without ever leaving.
  • What RPC does is that it establishes a connection with Odoo’s ORM and then executes the method which we defined and will return the response of the method into POS.
  • We can basically call any methods of Odoo ORM or any other methods which we defined using RPC.
  • The RPC component in Odoo’s JavaScript framework takes care of all the heavy lifting. We just need to fall into a simple format, and that’s it. 

Request Your Free Quote

Example of the RPC

Let us look at an example of the RPC. First, we need to import the RPC component into our JavaScript file.

var rpc = require('web.rpc');

Now we can use this `rpc` variable to execute any methods in Odoo’s ORM or any other custom methods. Let us try to read the price of a single product from the `product. product` model. 

var price = rpc.query({
    model: 'product.product',
    method: 'read',
    args: [[1], ['price']],
    context: context
})

How RPC works in Odoo’s POS module

  • In the above code, we can see that we are using the model `product.product` and calling the method `read` for this model.
  • We are also passing 2 arguments, number `1` and a list of fields to be read.
  • Here number `1` is the id of the product from which we need to read the value of the field `price`.
  • We are also passing context if needed. As you can see, we have only written a couple of lines, but what it does is execute the `read` method in the `product.product` model and pass 2 arguments, the first one the domain and the second one the fields to be fetched.
  • This will then fetch the `price` of the product with the id `1`.

So here we have learnt how RPC works in Odoo’s POS module. One thing to note is that even though in the start we said creating or writing a value to the database, what we are actually doing is that we create a connection to Odoo’s ORM to do all the heavy lifting.

Another important point to note is that the RPC will only work if there is an active internet connection. That is, Odoo’s front end framework should be able to connect to the ORM to execute these methods and then fetch the values. That’s it for today. We will be back with more interesting articles.

Request Your Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

Open chat
1
Hello, can we help you?