Create server actions
Server actions are actions that work on the base models. These actions are executed automatically. Let’s check the way how it is created;
After enabling the developer mode, check out the menu Settings > Technical > Actions > Server Actions.
Let’s check the server action types;
1. Update the record
2. Create a new record
3. Execute Python Code
4. Execute several actions
5. Send Email
6. Add Followers
7. Create the next activity
8. Send an SMS text Message
Once you click on the ‘Create’ button, this form view will appear. Here the ‘Action name’ needs to be filled, and also select any model upon which this server action needs to be acted. The next mandatory field is the ‘Action to do,’ which determines the action type.
XML code for creating a server action. It is placed in the data file of the module.
<record id="action_set_salesperson" model="ir.actions.server" >
<field name="name" >Set Sales Person </field >
<field name="model_id" ref="base.model_res_partner"/ >
<field name="state" >code </field >
</record >
In this record, as with every record, it has a unique id and also a name used to name the server action. Another field is the ‘model_id’ used to mention the model to which this action is linked. Next is the ‘state’ field that is used for specifying the type of server action.
Let’s go through the type of actions;
Update the record
This type of server action is created in order to update something in the specified model.
For example, If we need to set anyone as the salesperson for the records of contact, then the server action will be like this;
Here, the page ‘Data to Write’ refers to the fields and their corresponding actions to be done on the model. The field ‘Evaluation Type’ contains three modes like ‘Value, ‘Reference,’ and ‘Python Code.’ If it’s Value, then it will be specified as a literal value. If it is ‘Reference,’ then the existing value will be available to select and set its field. Otherwise, if it is the ‘Python Code,’ then a python expression is required.
Once the details are filled up then, the button ‘Create Contextual Action’ can be clicked. As a result, you will get a new menu in the contextual action in the view as given below.
If the option is clicked, the salesperson field will get assigned the value for the server action. Hence the record can be updated.
In the code, the update can be used as;
<field name="state" >object_write </field >
Create a new Record
Here, the server action created a new record in the specified model.
Its server action will be like this;
After creating this action, the model ‘Contact’ will have a contextual action to create a record based on the values set in the action. Its target model is also specified here.
The code will be like this;
Execute Python Code
This type of server action is used to execute python code.
This is done in the code as follows;
<field name="state" >code </field >
Execute several actions
This is a type of server action that performs multiple actions at a time.
Thus here, we can choose the actions that need to be executed simultaneously.
After creating this record, a contextual action will be created, and the listed child actions will be executed simultaneously when the action is selected.
This type of action is declared in the XML as;
<field name="state" >multi </field >
Send Email
This is used to send emails to our partner from a specified model. Once this type of action is selected, then a field will be displayed to select the email template.
Here is another option to select the mode of sending the template. Either it can be sent as an ‘Email’ or ‘ Post as Message’ or ‘post as Note.
Its declaration in the code is;
<field name="state" >email </field >
Add Followers
This is used to add followers to the specified model.
After creating this server action, a similar contextual action will be created, and the selected followers will be added to the corresponding model.
In the XML, this type if defined as followers;
<field name="state" >followers </field >
Create Next Activity
These types of actions are used to schedule the next activities based on a model.
This will create a contextual action to schedule the action that is mentioned in the record for server action.
In the XML code, it is defined as;
<field name="state" >next_activity </field >
Send SMS Text Message
This type of server action is used to send text messages in bulk.
The field ‘state’ is declared as ‘SMS’ here;
<field name="state" >sms </field >
Once the action type is selected, a field for selecting the template and the field to set the mode of sending the text message will appear. Thus once the contextual action is created, then the text message will be sent based on the model.