Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

how to use functional field in record rule?

Subscribe

Get notified when there's activity on this post

This question has been flagged
human_resourcerecords
4 Replies
18810 Views
Avatar
Nishant Kashyap

I am trying to achieve that all the users that are Hr managers should be allowed to see the records. I have created a Functional Field:

def list_HRM(self, cr, uid, ids, field_name, arg, context):
         attribute = {}
        hr_managers = self.pool.get('hr.employee').search(cr, uid, ['&', ('department_id.name', '=', 'Human Resources'), ('manager', '=', True)], context=context)
        hr_managers_uid = []
        for record in hr_managers:
            hr_managers_uid.append(self.pool.get('hr.employee').browse(cr, uid, record, context=context).user_id.id)
        record = self.browse(cr, uid, ids)[0]
        attribute[record.id] = str(uid in hr_managers_uid or uid==1)
        return attribute

    _columns={
    'hr_managers_func' : fields.function(list_HRM, type='char', method=True, string='List of HR Managers'),
    'always_true':fields.boolean()
     }
   _defaults={
      'always_true':True
      }

In .xml file:

<field name="always_true" invisible="1"/>
<field name="hr_managers_func" invisible="1"/>

In Record Rule:

['&','|',('state','=','hod_depart'),('state','=','hr_review'),('always_true','=',eval(hr_managers_func))]

I used field 'always_true' because of the record rule condition format i.e. [('field_name','operator',values)]. I thought that rule will evaluate the functional field using eval but unfortunately eval is not working on the record rule , I am getting this error:

NameError: name 'eval' is not defined

I could not think of more than this. I saw few forum somewhat similar to my problem, they were using the related field to avoid the functional field in the record, but here I have to check whether the current user belong to hr managers or not . I have tried explaining this in the best possible way, Looking forward for some reply.

1
Avatar
Discard
Avatar
Axel Mendoza
Best Answer

You could see another example of using a function field in the record rules here:

https://www.odoo.com/forum/help-1/question/how-to-show-different-task-stages-to-different-users-depending-on-other-model-90009#answer-90031 

 

1
Avatar
Discard
Tarek Mohamed Ibrahim

thx for comment, I didn't read this before, I'll check it.

Avatar
Max
Best Answer

I found out another method to call a function from ir.rule.

Here it is.

    <record id="manager_asterisk_cdr_rule" model="ir.rule">
        <field name="name">Manager Asterisk CDR rule</field>
        <field name="perm_write" eval="1"/>
        <field name="perm_read" eval="1"/>
        <field name="perm_create" eval="1"/>
        <field name="perm_unlink" eval="1"/>
        <field name="groups" eval="[(6,0, [ref('group_barrier_manager')])]"/>
        <field name="model_id" ref="asterisk_base.model_asterisk_cdr"/>
        <field name="domain_force">user.manager.get_cdr_domain()</field>
    </record>

The main part is:

    <field name="domain_force">user.manager.get_cdr_domain()</field>

And here is the function:

    @api.multi
    def get_cdr_domain(self):
        # Used from facility_manager/security.xml to filter CDR records
        facility_ids = [f.id for f in self.facilities]
        barriers = self.env['barrier.barrier'].search(
                                            [('facility', 'in', facility_ids)])
        numbers = []
        for b in barriers:
            # Add barrier access phones
            numbers.extend([a.number for a in b.access_phones])
            # Add panels GSM number
            numbers.extend(
                [p.gsm_number for p in b.call_panels if p.gsm_number])
            # Add panels SIP number
            numbers.extend(
                [p.sip_number for p in b.call_panels if p.sip_number])
        # Add keys of type=phone
        key_numbers = [k.number for k in self.env[
            'barrier.resident_key'].search([
                                           ('facility', 'in', facility_ids),
                                           ('key_type', '=', 'phone')])]
        numbers.extend(key_numbers)
        domain = ['|', ('src', 'in', numbers), ('dst', 'in', numbers)]
        return domain

Also when you add new records to the models requested from the function you need to clear cache:

    self.env['ir.rule'].clear_cache()

1
Avatar
Discard
fudo

this worked good, but you have to use user.your_function in domain_force, it have to be user, company_id, company_ids or else, it will causing error.
also, you need to write your_function() when inherit "res.users".

Avatar
Tarek Mohamed Ibrahim
Best Answer

Your request is to allow only managers to see the states hod_depart, hr_review

I'll achieved this differently, using the idea of the search function related to the function field. 

I don't have your status list, so I applied my logic on crm_lead table with the condition that employees in sales dep. can see all priorities, while others can see up to priority 3.

I replaced the word 'manager' with 'sales' to agree with the example I'm describing, the logic is the same. I created a module for this example and tested it, here is the module:

__init__.py: 
import community
__openerp__.py: 
{
"name" : "Community Examples",
"author" : "Tarek Mohamed Ibrahim",
"category" : "Examples",
"licence" : "AGPL-3",
"depends" : ['hr','crm'
],
"data" : [
'security/community_security.xml',
],
"demo" : [],
"installable": True,
"auto_install": False,
"application": False,
}

community.py

class crm_lead(osv.osv): 
     _inherit = 'crm.lead'
     #changing this method , but actually it works like a dummy function, the one which is really works is the search method
     def _get_list_sales(self, cr, uid, ids, field_name, arg, context):
         attribute = {}
         hr_sales = self.pool.get('hr.employee').search(cr, uid, [('department_id.name', '=', 'Sales')], context=context)
         hr_sales_uid = []
         for record in hr_sales:
             hr_sales_uid.append(self.pool.get('hr.employee').browse(cr, uid, record, context=context).user_id.id)
        
         record = self.browse(cr, uid, ids)[0]
         attribute[record.id] = str(uid in hr_sales_uid or uid==1)
         return attribute


     def list_sales_search(self, cr, uid, obj, name, args, context=None):
         if not args:
             return []

         #get the uid from the passed value
         uid = args[0][2]

         #get some code from lst_HRM to evaluate the current uid
         hr_sales = self.pool.get('hr.employee').search(cr, uid, [('department_id.name', '=', 'Sales')], context=context)
         hr_sales_uid = []
         for record in hr_sales:
             hr_sales_uid.append(self.pool.get('hr.employee').browse(cr, uid, record, context=context).user_id.id)
        
         is_sales = uid in hr_sales_uid or uid==1

         #build a query
         if is_sales:
             query = "select id from crm_lead"
         else:
             query = "select id from crm_lead where priority<'4'"

         #execute the query
         cr.execute(query)
         res = cr.fetchall()
         if not res:
             return [('id','=','0')]
         else:
             return [('id','in', [x[0] for x in res])]

         _columns = {
                 'list_sales' : fields.function(_get_list_sales, type='char', method=True, string='List of HR Sales',fnct_search=list_sales_search),
         }

crm_lead()

I defined the function field in crm_lead as the rule is going to be applied on this class

here is the rule

<?xml version="1.0" encoding="utf-8"?> 
<openerp>
     <data noupdate="0">
         <record model="ir.rule" id="ir_rule_sales_crm_leads">
             <field name="name">filter leadss</field>
             <field name="model_id" ref="crm.model_crm_lead"/>
             <field name="domain_force">[('list_sales','=',user.id)]</field>
             <field name="perm_read" eval="True"/>
             <field name="perm_write" eval="True"/>
             <field name="perm_unlink" eval="True"/>
             <field name="perm_create" eval="True"/>
         </record>
     </data>
</openerp>

The thing that one can say it is not logical is the expression

('list_sales','=',user.id) 

which indicates that I'm going to equate the user.id value with the result of the function field list_sales, but the point is that I used a feature in the search function related to the function field to return a list of records ids based on the input sent to the function, which in our case is the user.id.

This could be generalized by sending any tuple, or even a dictionary, to the search function and doing any calculations. This could be achieved by replacing the user.id parameter with any tuple, or dictionary and receiving all needed values in the search function body by indexes

One more thing, I can use the 'operator' placeholder also to send any other parameters, as I didn't use the operator in the function.

Pls copy the code and create a module to test it and then you can borrow the logic to your own case, you need to adjust the indentation.

I think this idea is new, my first knowledge of the search function got from the link:  https://www.odoo.com/nl_NL/forum/help-1/question/how-to-implement-fnct-search-29984

I hope this search is useful.


0
Avatar
Discard
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Related Posts Replies Views Activity
Record rules not working for specific group of users in Odoo 16 - Need an Urgent Resolution
records
Avatar
Avatar
2
Aug 23
4601
employee hiring date
human_resource
Avatar
Avatar
1
Jul 23
4470
Count Related Records Solved
records
Avatar
Avatar
1
Jan 21
5446
how can i integrate Finger print Reader device to OpenERP v7
human_resource
Avatar
Avatar
1
Mar 18
4294
Can I see our Deleted Records if yes then how in odoo10.
records
Avatar
Avatar
1
Dec 17
7770
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now