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

Create multi records from one server action

Subscribe

Get notified when there's activity on this post

This question has been flagged
development
1 Reply
2762 Views
Avatar
Ali Ammar
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 )


0
Avatar
Discard
Avatar
Malay Khamar (Serpent Consulting Services Pvt. Ltd.)
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.
2
Avatar
Discard
Ali Ammar
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

Malay Khamar (Serpent Consulting Services Pvt. Ltd.)

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.

Malay Khamar (Serpent Consulting Services Pvt. Ltd.)

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

Ali Ammar
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

Malay Khamar (Serpent Consulting Services Pvt. Ltd.)

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.

Ali Ammar
Author

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

Malay Khamar (Serpent Consulting Services Pvt. Ltd.)

Please share the create method.
Thanks

Ali Ammar
Author

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

Malay Khamar (Serpent Consulting Services Pvt. Ltd.)

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

Ali Ammar
Author

What should I write in this method?

Ali Ammar
Author

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

Malay Khamar (Serpent Consulting Services Pvt. Ltd.)

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

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
Odoo 17: How to Create a Custom Table Header in List (Tree) View Using OWL
development
Avatar
0
Dec 25
120
How to create custom Table header by using odoo 17
development
Avatar
Avatar
1
Dec 25
351
Oddo Integration with Intune
development
Avatar
0
Dec 25
1752
@guia@¿Cómo hablar con una persona en Air France?
development
Avatar
0
Dec 25
2
How do I change the name of the module, or rather the name assigned to the module, after I created it in Odoo Studio?
development
Avatar
Avatar
1
Dec 25
568
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