What are the Different Services in Odoo 16

What are the Different Services in Odoo 16

We are all familiar with Odoo Services. In different scenarios, we use services like the Effect service, Notification service, http service, etc. Here we are discussing services in Odoo. Services are long-lived pieces of code that provide functionality. They may be imported from components (with useService) or by other services. Also, services can declare a set of dependencies.

Defining Services:

To define a service, we need to implement some interfaces.
dependencies: Here, we specify the list of dependencies that are used in this service. It is an optional value. 
start: The main definition of service. It returns either value or promise. The service loader waits for the promise to resolve to a value, which is then the value of the service. Sometimes value will be set to null when the services just do their work without a need to be directly called by another code.
async: It is an optional value; if we give this, it will return a True value or a list. For some conditions, we need to give the asynchronous functions.
Example of services:
First, we need to import the registry.
import { registry } from "@web/core/registry";
const TestService = {
   dependencies: ["notification"],
   start(env, { notification }) {
       let counter = 1;
       setInterval(() => {
           notification.add('Test ${counter++}');
       }, 5000);
   }
};
Then add this service in registry like this
registry.category("services").add("TestService", TestService);

Different Services:

Commonly used services are:

Cookie service:

It is used to read or modify the cookie. The technical name is cookie. There is no need for dependencies. It has 3 APIs.

current: each cookie and its value.
setCookie: set cookie name and its value.
Arguments are name, value and ttl. name is the name of the cookie that should be set. Value is the value we set for the cookie, it is.
deleteCookie: It deletes the cookie.
It uses only one argument, which is the name. It is used to represent the name of a cookie.

Effect service:

It is the graphical element that can be temporarily displayed on top of the page. Its technical name is ‘effect’, and there is no need for any dependencies.
Example:

const effect = useService("effect");
effect.add({
   'Hello, Odoo!',
   type: "rainbow_man",
});

Http Service:

While most interactions between the client and the server in Odoo are XMLHTTPRequest, they sometimes require lower level control on request. In this http service, it sends ‘get’ and ‘post’ http requests.
async get: It sends a get request. Arguments are route and readMethod. The route is the URL to send that request to, and readMethod is the response content type. It can be text”, “json”, “formData”, etc. its returns the result of the request with the format that we defined by the readMethod argument.
const httpService = useService("http");
const data = await httpService.get("https://test.com/posts/1");
async post: It sends a post request. Arguments are route, params and readMethod. Route is the URL to send that request to and readMethod is the response content type, it can be text”, “json,” “formData,” etc. params is the key value data to be set in the 
request. It returns the result of the request with the format that we defined by the readMethod argument.
const httpService = useService("http");
const data = await httpService.post("https://test.com/posts/1", { title: "new title", content: "new content" });

Notification service: 

It allows you to display notifications on screen.
Arguments are messages and options. The message is the message we want to display, and options are the options of the notifications. Already defined options are type, title, sticky, className, onClose, and buttons.
type: It is used to change the background color. Example warning, danger, info, success, etc.
title: The title of the notification.
sticky: It is a boolean field when we enable this notification should stay until dismissed.
className: It is the additional css class that will be added to the notification.
onClose: This will be executed when we close the notification.
buttons: This button will display in the notification.
this.notification = useService("notification");
this.notification.add('Your Notification', { type: "danger" });

Router Service :

In this service we get the information about the  services. It provide a way for the application to update the url, depending on its state.
current: By using the current key we can get access to the current route. It returns the pathname, search and hash. Pathname is the path of the current location. Search is a dictionary that maps each search keyword from the url to its value. Hash will return a dictionary value described in the hash.
Example:
const { pathname, search, hash } = env.services.router.current;
pushState: Updates the URL with each key/value pair from the hash object. If a value is set to an empty string, the key is added to the url without any corresponding value.
Example:
routerService.pushState({ key: value });
Url is like this /web#key=value
Redirect: It is used to redirect the browser to the url. It reloads to the specified url.
redirect(url[, wait])

RPC Service :

RPC services provide a single asynchronous function for sending requests to servers. Calling the  controller is pretty straightforward. The route must be the first argument and params is the second argument and it is an optional argument.
this.rpc = useService("rpc");
this.rpc("/my/route", { some: "value" });

Title Service :

It used to read or modify the document title.
const titleService = useService("title");
titleService.setParts({ odoo: "Odoo", action: "Test" });
The current document title is “Odoo”, we can change it to “Odoo – Test” by using this command.
current : It returns the current title of the document.
getParts : The current Parts object maintained by the title service.
setParts : Allows to add or replace or delete several parts of the title.

Similar Posts

Leave a Reply

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