Debasish
Debasish

Reputation: 322

how to call python method from server action in odoo 9

I want try to call the python method from server action in same module . but there is no error found but the method is not called . I also follow this link for call the python method . This is the code i use for call the method .

.xml

<record id="action_email_data_parser_server" model="ir.actions.server" >
  <field name="name">[Server Action] Create Leads from Mail</field>
  <field name="model_id" ref="model_email_data_parser"/>
  <field name="condition">True</field>
  <field name="type">ir.actions.server</field>
  <field name="state">code</field>
  <field name="code">
      lead = self.browse(cr, uid, context['active_id'], context=context)
      mail_message = lead.message_ids[0]
      mail_body = mail_message['body']
      lead_data_dict = self.parse_body(cr, uid, mail_body)
      self.write(cr, uid, context['active_id'], lead_data_dict)
  </field>
</record>

.py

class email_data_parser(osv.osv):
  _name = "email_data_parser"
  _description = "Email Data Parser"

  def parse_body(self, uid, body):
     data_dist = body
     retun data_dist

So , if any one have any idea please share with me , how can i solve this .

Upvotes: 1

Views: 4667

Answers (2)

Debasish
Debasish

Reputation: 322

I got the answer , when you want to trigger any server action you just follow this code , that may be help you .

.xml

<record id="action_email_data_parser_server" model="ir.actions.server" >
    <field name="name">[Server Action] Create Leads from Mail</field>
    <field name="model_id" ref="model_email_data_parser"/>
    <field name="condition">True</field>
    <field name="type">ir.actions.server</field>
    <field name="state">code</field>
    <field name="code">
        action = self.parse_body(cr, uid, context=context)
    </field>
</record>

.py

import logging
from openerp.osv import fields,osv,expression
from openerp import models,api
_logger = logging.getLogger(__name__)

class email_data_parser(osv.osv):
  _name = "email_data_parser"
  _description = "Email Data Parser"

  def parse_body(self, cr, uid, context=None):
      _logger.info('This is FDS email parsing and through we can parse the mail from any email')

then check it in odoo-server.log . if the message will show , then your server action will call the python method .

Upvotes: 1

Charif DZ
Charif DZ

Reputation: 14721

 <!-- server action : can be called from menu and open a view from a python methode so we can  change   -->
        <!-- server action : the context of the action    -->
        <record id="idOfServerAction" model="ir.actions.server">
            <field name="name">Action Title</field>
            <field name="condition">True</field>
            <field name="type">ir.actions.server</field>
            <field name="model_id" ref="model_modelName" /> 
             <!--Ex ref='model_res_users -->
            <field name="code">action = self.MethodeName(cr, uid, None, context)</field>
            <!-- EX: action = self.AddUser(cr, uid, None, context) -->
        </record>

        <menuitem id="idOfMenuItem" name="Title of the menu" action="idOfServerAction" />

Upvotes: 0

Related Questions