Development Book V17:Remote Procedure Calls (RPC)

XML-RPC

It's a set of protocols and a standard that allows software working on different operating systems and environments to call procedures through the internet. When using XML-RPC, a client sends an HTTP request to a server that has the protocol implemented, and the server responds with an HTTP request. A call can have one outcome and several parameters. A few data types are defined by the protocol for the arguments and result.

It's a request for a remote operation. HTTP is used for transfer, and XML is used for encoding. XML-RPC is designed to be as simple as possible, even as it makes it easier to send, process, and receive complicated data structures.

Connection to Odoo:

We need to first create a connection with Odoo to access data in Odoo. Two kinds of XML-RPC endpoints are available in Odoo. The xmlrpc/2/common endpoint did not require to be authenticated. This endpoint can be used to retrieve the Odoo version or perform user authentication. The user's ID gets returned by the authenticate procedure.

API Key

To generate an API key for a user in Odoo 17.0, access Developer Mode. Then, navigate to the user's Preferences within the My Profile section. In Odoo, API keys are supported and might be necessary for web service operations based on specific modules or configurations.

When incorporating API Keys into your scripts, replace your password with the generated key while retaining the login credentials. Safeguard the API Key as securely as your password since both grant equivalent access to your user account, although API Keys cannot be used for interface log-ins.

To add a key to your account, visit your Preferences or My Profile section.

odoo-development

After choosing the Account Security Tab, select the New API key.

odoo-development

Verify the password for the account and provide a name for the API key.

odoo-development

You will then receive the API key.

odoo-development

You can use this API key to connect to Odoo.


import xmlrpc.client
data_url = 'http://localhost:8016' # odoo instance url
database = 'data' # database name
user = 'admin' # username
password = '6c089b9a84ee6e2e9c5423c12458c510b03cc066' # api key
common_auth = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(data_url))
uid = common_auth.authenticate(database, user, password, {})

Utilizing xmlrpc/2/common, authentication is possible by providing the database name, user login credentials, and the API key as the password. Upon successful authentication, the authenticate method yields the user ID, which needs to be retained for accessing data within Odoo.

Calling Methods

The second endpoint, xmlrpc/2/object, is where data created or accessed within Odoo will be accessible. We can obtain the data with the use of the xmlrpc/2/object execute_kw method.

model = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))

Since xmlrpc/2/object needed authentication, we will be utilizing the user ID that was generated during the Odoo connection.

Search/Read records:

Search:


search_partners_ids = data_model.execute_kw(database, uid, password, 'res.partner', 'search', [[['is_company', '=', True]]])

The search function returns all record IDs in the selected model.

Output:


[14, 10, 11, 15, 41, 1, 12, 13, 9]

Limit your search to:


 partners = data_model.execute_kw(database, uid, password, 'res.partner', 'search', [[['is_company', '=', True]]], {'limit': 1})

By selecting the limit option, we can decide on the maximum amount of records.

Output:


[14]

Search with Offset:


 partners = data_model.execute_kw(database, uid, password, 'res.partner', 'search', [[['is_company', '=', True]]], {'offset': 1})

We can choose the record to begin our search by using the offset option.

Output:

[10, 11, 15, 41, 1, 12, 13, 9]

Search Count:

partners = data_model.execute_kw(database, uid, password, 'res.partner', 'search_count', [[['is_company', '=', True]]])

The number of records that satisfy the condition is returned by the search count.

Read:

Partner_id = data_model.execute_kw(database, uid, password, 'res.partner', 'search', [[['is_company', '=', True]]], {'limit': 1})
partners = data_model.execute_kw(database, uid, password, 'res.partner', 'read', [partners_id])

The read function returns a key-value pair containing the values for each field. We need to specify the IDs of the records we want to obtain.

Output:

[{'id': 14, 'message_is_follower': False, 'message_follower_ids': [], 'message_partner_ids': [], 'message_ids': [], 'has_message': False, 'message_unread': False, 'message_unread_counter': 0, 'message_needaction': False, 'message_needaction_counter': 0, 'message_has_error': False, 'message_has_error_counter': 0, 'message_attachment_count': 0, 'message_main_attachment_id': False, 'website_message_ids': [], 'message_has_sms_error': False, 'phone_sanitized': '(870)-931-0505', 'phone_sanitized_blacklisted': False, 'phone_blacklisted': False, 'mobile_blacklisted': False, 'phone_mobile_search': False, 'email_normalized': 'azure.interior24@example.com', 'is_blacklisted': False, 'message_bounce': 0, 'activity_ids': [], 'activity_state': False, 'activity_user_id': False, 'activity_type_id': False, 'activity_type_icon': False, 'activity_date_deadline': False, 'my_activity_date_deadline': False, 'activity_summary': False, 'activity_exception_decoration': False, 'activity_exception_icon': False, 'name': 'Azure Interior', 'display_name': 'Azure Interior', 'date': False, 'title': False, 'parent_id': False, 'parent_name': False, 'child_ids': [26, 33, 27], 'ref': False, 'lang': 'en_US', 'active_lang_count': 1, 'tz': False, 'tz_offset': '+0000', 'user_id': False, 'vat': False, 'same_vat_partner_id': False, 'bank_ids': [], 'website': 'http://www.azure-interior.com', 'comment': False, 'category_id': [5], 'credit_limit': 0.0, 'active': True, 'employee': False, 'function': False, 'type': 'contact', 'street': '4557 De Silva St', 'street2': False, 'zip': '94538', 'city': 'Fremont', 'state_id': [13, 'California (US)'], 'country_id': [233, 'United States'], 'country_code': 'US', 'partner_latitude': 0.0, 'partner_longitude': 0.0, 'email': 'azure.Interior24@example.com', 'email_formatted': '"Azure Interior" ', 'phone': '(870)-931-0505', 'mobile': False, 'is_company': True, 'industry_id': False, 'company_type': 'company', 'company_id': False, 'color': 0, 'user_ids': [], 'partner_share': True, 'contact_address': 'Azure Interior\n4557 De Silva St\n\nFremont CA 94538\nUnited States', 'commercial_partner_id': [14, 'Azure Interior'], 'commercial_company_name': 'Azure Interior', 'company_name': False, 'barcode': False, 'self': [14, 'Azure Interior'], '__last_update': '2022-03-24 07:18:33', 'create_uid': [1, 'OdooBot'], 'create_date': '2022-03-24 07:17:52', 'write_uid': [1, 'OdooBot'], 'write_date': '2022-03-24 07:18:33', 'im_status': 'im_partner', 'channel_ids': [], 'signup_token': False, 'signup_type': False, 'signup_expiration': False, 'signup_valid': False, 'signup_url': False, 'property_product_pricelist': [1, 'Public Pricelist (EUR)'], 'partner_gid': 0, 'additional_info': False, 'property_stock_customer': [5, 'Partner Locations/Customers'], 'property_stock_supplier': [4, 'Partner Locations/Vendors'], 'picking_warn': 'no-message', 'picking_warn_msg': False, 'x_encrypted': False}]

Read with fields:


partner_id = data_model.execute_kw(database, uid, password, 'res.partner', 'search', [[['is_company', '=', True]]], {'limit': 1})
partners = data_model.execute_kw(database, uid, password, 'res.partner', 'read', [partners_id], {'fields': ['name', 'email']})

We can select the fields we need to access by using the fields option.

Output:


[{'id': 14, 'name': 'Azure Interior', 'email': 'azure.Interior24@example.com'}]

Search Read:

partners = data_model.execute_kw(database, uid, password, 'res.partner', 'search_read', [[['is_company', '=', True]]], {'fields': ['name', 'email'], 'limit': 1})

Search read merges both the search and read functionalities into a single operation.

Output:


[{'id': 14, 'name': 'Azure Interior', 'email': 'azure.Interior24@example.com'}]

Create records:


partner_id = data_model.execute_kw(database, uid, password, 'res.partner', 'create', [{'name': 'Test Partner', 'email': 'test@test.com'}])

The create method in Odoo facilitates the generation of a new record and provides the ID of the newly created entry.

Output:


1

Update records:


 partner_id = data_model.execute_kw(database, uid, password, 'res.partner', 'create', [{'name': 'Test Partner', 'email': 'test@test.com'}])
partner = data_model.execute_kw(database, uid, password, 'res.partner', 'write', [[partner_id], {'name': 'Test Partner Updated'}])

Utilized for modifying data within an existing record, the write method in Odoo confirms the successful update by returning a Boolean value of True.

Output:


True

Delete Records:


partner_id = data_model.execute_kw(database, uid, password, 'res.partner', 'create', [{'name': 'Test Partner', 'email': 'test@test.com'}])
partner_id = data_model.execute_kw(database, uid, password, 'res.partner', 'unlink', [[partner_id]])

The unlink method in Odoo is employed to delete a record. Upon successful deletion, it returns a Boolean value of True.

Output:


True
whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message