In the constantly changing world of Odoo development, it's crucial to master the skill of Remote Procedure Calls (RPC). With the introduction of Odoo 17, there have been notable changes, especially with the phasing out of rpc.query and ajax.json rpc. This article will explore the latest methods, highlighting JSON-RPC and the RPC service. Learn about the reasons behind these changes, how to apply them, and where they outshine their outdated counterparts.
Understanding JSON-RPC
JSON-RPC, or JavaScript Object Notation Remote Procedure Call, is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. In Odoo 17, it has taken center stage as a preferred method for handling RPC calls.
Why JSON-RPC?
JSON-RPC provides a standardized way to structure data and requests, making it versatile and interoperable. Unlike its predecessor, ajax.json rpc, JSON-RPC is designed to offer a cleaner and more consistent approach to handling remote procedure calls.
How to Use JSON-RPC in Odoo 17: A Step-by-Step Guide
1. Import Necessary Modules:
To begin using JSON-RPC in Odoo 17, ensure that you import the required modules. For example:
from odoo import http
2. Implement JSON-RPC in Your Controller:
Define a new controller or update an existing one to use JSON-RPC. Here's a simple example:
class SampleController(http.Controller):
@http.route('/sample/rpc/endpoint', type='json', auth='public', website=True)
def sample_rpc_method(self, param1, param2):
# Your JSON-RPC logic here
return result
3. Invoke the RPC Call:
Make RPC calls from your client-side code using the JSON-RPC format. For instance, using JavaScript with jQuery:
jsonrpc('/sample/rpc/endpoint', {}).then((res)=>{
if (res){
//your logic here
}
})
Example:
Here, we create the dynamic website Snippet, which shows the details of the total product sold.
Controller code:
from odoo import http
from odoo.http import request
class Snippet(http.Controller):
@http.route('/get_total_sold', auth='public', type='json')
def get_total_product(self):
product = request.env['product.template'].sudo().search_read([], ['name', 'list_price', 'id'], limit=4)
return product
Js jsonrpc code:
/** @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 DynamicSnippets = publicWidget.Widget.extend({
selector: '.js_dynamic_snippet',
start: function(){
jsonrpc('/get_total_sold', {}).then((res)=>{
if (res){
this.$el.find("#total").html(renderToElement('snippet.dynamic_snippet_custom', {res: res}))
}
})
}
});
publicWidget.registry.DynamicSnippets = DynamicSnippets;
Rendering template code:
<?xml version="1.0" encoding="UTF-8" ?>
<templates>
<t t-name="snippet.dynamic_snippet_custom">
<div class="w3-green w3-hover-shadow w3-right">
<div>
<section>
<div class="list-group" id="list-tab" role="tablist">
<div class="w3-card-44 w3-dark-grey">
<t t-foreach="res" t-as="prod" t-key="prod.id">
<a class="list-group-item list-group-item-action active" id="list-home-list" data-bs-toggle="list" href="#list-home" role="tab" aria-controls="list-home">
<t t-out="prod.name"/>: <t t-out="prod.list_price"/>
</a>
</t>
</div>
</div>
</section>
</div>
</div>
</t>
</templates>
Embracing the RPC Service in Odoo 17:
In addition to JSON-RPC, Odoo 17 introduces the RPC service as a powerful tool for handling remote procedure calls. This service simplifies the process, providing a streamlined and efficient solution.
Consistency and Standardization:
JSON-RPC adheres to a well-defined standard, ensuring a consistent and predictable structure for your remote procedure calls.
Improved Readability:
JSON-RPC's human-readable format makes it easier to understand and debug, enhancing the overall development experience.
Enhanced Interoperability:
JSON-RPC's design promotes better interoperability between different systems and platforms, ensuring seamless integration.
As Odoo 17 ushers in a new era of development, adapting to modern practices is crucial. JSON-RPC and the rpcService offer a cleaner, more standardized, and efficient way to handle remote procedure calls. Embrace these changes, bid farewell to deprecated methods, and elevate your Odoo development to new heights.
To read more about How to Call JSON RPC to Webcontroller in Odoo 17, refer to our blog How to Call JSON RPC to Webcontroller in Odoo 17.