Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Custom module Onchange function is not working in version 7 and not shows warning message

By
Odoo
on 12/22/14, 2:26 AM 760 views

class hr_analytic_timesheet(osv.osv):
   
    _inherit = 'hr.analytic.timesheet'
    
       
    _columns = {
                
              'worked_hours':fields.float('Worked Hours'),
              'overtime_hours':fields.float('Over Time'),
              'total':fields.float('Total')
            
                }

 

  def onchange_worked_hours(self, cr, uid, ids, worked_hours, context=None):
        for i in self.read(cr,uid,ids,['worked_hours'],context=context):
             if i['worked_hours'] >8.00:
                 print("##########################inside worked hours")
                 raise osv.except_osv(_('Warning'),_('Worked Hours Cannot be greater than 8'))
             return True

 

====================================

 <record id="timesheet_tree_inherited_view" model="ir.ui.view">
        <field name="name">timesheet.tree.inherited</field>
        <field name="model">hr.analytic.timesheet</field>
        <field name="inherit_id" ref="hr_timesheet.hr_timesheet_line_tree"/>
        <field name="arch" type="xml">
        
         <xpath expr="//tree/field[@name='unit_amount']" position="before">
        
            <field name="worked_hours" on_change="onchange_worked_hours(worked_hours)"/>        
            
          
            
              <field name="total"/>
            
            
        
         </xpath>
        
            </field>
        </record> 

0

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 12/22/14, 5:17 AM

@LIBU, are you trying to validate the new value of worked_hours or the saved value?  Currently you are checking against the saved ['worked_hours'], if the record has been saved before (in the case of you editing the record).  As @Ludo has mentioned, the record have not been saved if you expect this to be triggered when you create a new record.

Also, wouldn't it be better if you use the 'warning' return value key instead of raising exception?

Yes @Ivan I want to validate the value when I create a new record.....It is not a saved data

Odoo
on 12/22/14, 6:18 AM

So you should be using worked_hours variable directly as @Ludo had explained. if worked_hours >8.00 .... Also, please consider using 'warning' return as opposed to raising errors.

Ivan
on 12/22/14, 6:56 AM

Sorry, forgot to mention, remove the for loop.

Ivan
on 12/22/14, 6:56 AM
0

Ludo - Neobis

--Ludo - Neobis--
2387
| 2 2 5
Hoogvliet, Netherlands
--Ludo - Neobis--

Developer

Ludo - Neobis
On 12/22/14, 4:01 AM

Are you sure ids is not empty? Since you are on the on_change method, the object you are working on does not have to be saved yet.

Also, if you supply the worked hours (which can be a list of ids of the working_hours object), then you don't need a read on self, but rather on the object the worked_hours field is referencing.

Print/debug list the values in the beginnen of your method to be sure everything is supplied in the first place and see which types they are.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 12/22/14, 2:26 AM
Seen: 760 times
Last updated: 3/16/15, 8:10 AM