Skip to Content
Menu
This question has been flagged
1 Reply
1432 Views
ValueError: : "Expected singleton: hr.salary.details(1, 2)" while evaluating
'for record in records:\n record.calculate_salary()'

my server action xml code:

 

< field name = "name" > calculate salary field > 

 

 

list field> 

code field> 

                 for record in records:

                         record.calculate_salary()

field > 

records >                                                            

my function: 

def calculate_salary(self):

         for rec in self:

             salary = []

             get_date = datetime. today (  ) 

             month = int(get_date.month ) -1_ 

             salary . append({ 'name': ('salary for') + " " + str(rec.employee1_name_id['name'])+ " " + str(month ),

                                 'employee1_name': rec. employee1_name_id [ 'name' ],

                                 'salary_id': rec.id ,

                                 'employee_number': rec. employee_number_id ,

                                 'employee_job_title': rec. employee_job_title , 

                                'employee_salary': rec . employee_salary ,

                                 'hawafez_salary': rec. hawafez_salary , 

                                'cooperation_box': rec . cooperation_box ,

                                 ':rec. total_salary ,                                

                            })

             rec. env [ 'hr.salary.months' ].create( salary )


Avatar
Discard
Best Answer

Hi Ali Ammar,

As you are already added a loop into your function, So you can directly use the records in the server code.

Just like :

       field name="code"> records.calculate_salary()

Also at the last line , you can directly use a self instead of rec for salary month object. Just like:

                 self. env [ 'hr.salary.months' ].create( salary )

And try to defined the object in a variable outside of the loop and use that variable into the code for better code execution and performance.

I already Provide the solution for this in your previous question which was closed by you. 
Have you checked that. 

Updated Answer:

Please try with the below code 



# Server Action
record id="generate_salary" model="ir.actions.server">
field name="name">calculate salary
field name="model_id" ref="hr_salary.model_hr_salary_details"/>
field name="binding_model_id" ref="hr_salary.model_hr_salary_details"/>
field name="binding_view_types">list
field name="state">code
field name="code">
if records:
records.calculate_salary()
/field>
/record>


# Let the system take the default api
def calculate_salary(self):
salary_mnth_obj = self.env['hr.salary.months']
for rec in self:
salary_mnth_obj.create({
'name': ('salary for') + " " + str(rec.employee1_name_id.name),
'employee1_name': rec. employee1_name_id.name,
'salary_id': rec.id ,
'employee_number': rec.employee_number_id,
'employee_job_title': rec. employee_job_title,
'employee_salary': rec . employee_salary,
'hawafez_salary': rec. hawafez_salary,
'cooperation_box': rec . cooperation_box ,
})
return True
Hope it will help you.
Avatar
Discard
Author

I do all that you say
I still have the same error
if I delete the create method I don't have error
but when I add it and run for multi record I faced the error

Hi ,
Can you please try with the below code.

# Server Action
record id="generate_salary" model="ir.actions.server">
field name="name">calculate salary</field>
field name="model_id" ref="hr_salary.model_hr_salary_details"/>
field name="binding_model_id" ref="hr_salary.model_hr_salary_details"/>
field name="binding_view_types">list</field>
field name="state">code</field>
field name="code">
if records:
records.calculate_salary()
/field>
/record>

# Let the system take the default api
def calculate_salary(self):
salary_mnth_obj = self.env['hr.salary.months']
for rec in self:
salary_mnth_obj.create({
'name': ('salary for') + " " + str(rec.employee1_name_id.name),
'employee1_name': rec. employee1_name_id.name,
'salary_id': rec.id ,
'employee_number': rec.employee_number_id,
'employee_job_title': rec. employee_job_title,
'employee_salary': rec . employee_salary,
'hawafez_salary': rec. hawafez_salary,
'cooperation_box': rec . cooperation_box ,
})
return True

Hope it will help you.

Hi Ali Ammar,
Please check the updated answer. I hope it will help you.

Author

raise exception.with_traceback(None) from new_cause
ValueError: <class 'ValueError'>: "Expected singleton: hr.salary.details(1, 2, 3)" while evaluating
'if records:\n records.calculate_salary()'

after I run your code

Then you must be check the code for the create and compute methods of the model "hr.salary.months" and the api decorators for those methods.
I hope it will help you.

Author

the problem in create method and I don't know how to solve

Please share the create method.
Thanks

Author

I didn't write any thing else over the above code
create method still like itself

Ok , then try to add a create method in the model and check the result. Hope it will help you.

Author

What should I write in this method?

Author

I solve singleton error
thanx
but now it's create only the first record in my selected records as much i select records

Please print the self into your code and check if it takes the multiple records or not.
User active_ids from the context instead of the self.
Hope it will help you

Related Posts Replies Views Activity
1
Dec 24
62
0
Dec 24
36
1
Dec 24
43
0
Dec 24
54
0
Nov 24
61