Odoo 17 is a robust ERP system that supports smooth integration with external applications through different APIs. One of the most effective methods of communicating with Odoo programmatically is through JSON-RPC (JavaScript Object Notation – Remote Procedure Call). JSON-RPC is a light, stateless protocol that supports structured requests and responses between a client and a server.
In this in-depth guide, we will take you deep into the realm of RPC Odoo 17. We will describe what JSON-RPC is, why it is useful, and how to utilize JSON RPC in Odoo 17 in your Odoo projects. Additionally, we will walk through real-life implementation using correct Odoo 17 coding practices and provide a detailed example for better understanding.
{
"jsonrpc": "2.0",
"method": "call",
"id": 1,
"params": {
"model": "product.template",
"method": "search_read",
"args": [[]],
"kwargs": {"fields": ["id", "name", "list_price"], "limit": 4}
}
}
{
"jsonrpc": "2.0",
"id": 1,
"result": [
{"id": 101, "name": "Wireless Mouse", "list_price": 29.99},
{"id": 102, "name": "Mechanical Keyboard", "list_price": 89.99}
]
}
JSON-RPC provides a number of benefits when implementing Odoo with external systems:
Modify the odoo.conf file and add the following lines:
[options]
jsonrpc = True
jsonrpc_interface = 127.0.0.1
jsonrpc_port = 8069
Restart the Odoo server after making these changes.
We need to create a custom endpoint in Odoo that can be accessed via JSON-RPC. Here’s how you can create a controller to fetch sold products:
rom odoo import http
from odoo.http import request
class ProductSnippetController(http.Controller):
@http.route('/fetch_sold_products', auth='public', type='json')
def retrieve_sold_items(self):
items = request.env['product.template'].sudo().search_read([], ['name', 'list_price', 'id'], limit=4)
return items
This controller defines an endpoint (/fetch_sold_products) that returns a JSON response containing sold product details.
To fetch data using JSON-RPC in JavaScript, use the following Odoo module:
/* @odoo-module /
import publicWidget from '@web/legacy/js/public/public_widget';
import { jsonrpc } from "@web/core/network/rpc_service";
import { renderToElement } from "@web/core/utils/render";
let CustomSnippetWidget = publicWidget.Widget.extend({
selector: '.js_custom_snippet',
start: function(){
jsonrpc('/fetch_sold_products', {}).then((data)=>{
if (data){
this.$el.find("#sold_products").html(renderToElement('snippet.custom_snippet_template', {data: data}))
}
})
}
});
publicWidget.registry.CustomSnippetWidget = CustomSnippetWidget;
Suppose you operate an e-commerce site where you sell home appliances in your online store, and you want your website to dynamically display the 4 best-selling products. That is, instead of having to constantly update the product list manually, you can use JSON-RPC from the Odoo API. Now, every time a customer visits your site, the latest top-selling products are fetched automatically via JSON-RPC and displayed in real-time, improving accuracy and user experience.
JSON-RPC is a powerful and efficient protocol for interacting with the rpc Odoo 17. It provides a structured, lightweight, and scalable approach to performing CRUD operations, and automating processes. Using the proper implementation procedures, organizations can improve their integration capabilities and make data exchange between Odoo & third-party applications more seamless and efficient.
Additionally, through proper utilization of JSON-RPC, robust high-performing Odoo systems can be built integrating with third applications seamlessly.
Innovative ERP Solutions.
Office