Skip to Content
Odoo Menú
  • Registra entrada
  • Prova-ho gratis
  • Aplicacions
    Finances
    • Comptabilitat
    • Facturació
    • Despeses
    • Full de càlcul (IA)
    • Documents
    • Signatura
    Vendes
    • CRM
    • Vendes
    • Punt de venda per a botigues
    • Punt de venda per a restaurants
    • Subscripcions
    • Lloguer
    Imatges de llocs web
    • Creació de llocs web
    • Comerç electrònic
    • Blog
    • Fòrum
    • Xat en directe
    • Aprenentatge en línia
    Cadena de subministrament
    • Inventari
    • Fabricació
    • PLM
    • Compres
    • Manteniment
    • Qualitat
    Recursos humans
    • Empleats
    • Reclutament
    • Absències
    • Avaluacions
    • Recomanacions
    • Flota
    Màrqueting
    • Màrqueting Social
    • Màrqueting per correu electrònic
    • Màrqueting per SMS
    • Esdeveniments
    • Automatització del màrqueting
    • Enquestes
    Serveis
    • Projectes
    • Fulls d'hores
    • Servei de camp
    • Suport
    • Planificació
    • Cites
    Productivitat
    • Converses
    • Validacions
    • IoT
    • VoIP
    • Coneixements
    • WhatsApp
    Aplicacions de tercers Odoo Studio Plataforma d'Odoo al núvol
  • Sectors
    Comerç al detall
    • Llibreria
    • Botiga de roba
    • Botiga de mobles
    • Botiga d'ultramarins
    • Ferreteria
    • Botiga de joguines
    Food & Hospitality
    • Bar i pub
    • Restaurant
    • Menjar ràpid
    • Guest House
    • Distribuïdor de begudes
    • Hotel
    Immobiliari
    • Agència immobiliària
    • Estudi d'arquitectura
    • Construcció
    • Gestió immobiliària
    • Jardineria
    • Associació de propietaris de béns immobles
    Consultoria
    • Empresa comptable
    • Partner d'Odoo
    • Agència de màrqueting
    • Bufet d'advocats
    • Captació de talent
    • Auditoria i certificació
    Fabricació
    • Textile
    • Metal
    • Mobles
    • Menjar
    • Brewery
    • Regals corporatius
    Salut i fitness
    • Club d'esport
    • Òptica
    • Centre de fitness
    • Especialistes en benestar
    • Farmàcia
    • Perruqueria
    Trades
    • Servei de manteniment
    • Hardware i suport informàtic
    • Sistemes d'energia solar
    • Shoe Maker
    • Serveis de neteja
    • Instal·lacions HVAC
    Altres
    • Nonprofit Organization
    • Agència del medi ambient
    • Lloguer de panells publicitaris
    • Fotografia
    • Lloguer de bicicletes
    • Distribuïdors de programari
    Browse all Industries
  • Comunitat
    Aprèn
    • Tutorials
    • Documentació
    • Certificacions
    • Formació
    • Blog
    • Pòdcast
    Potenciar l'educació
    • Programa educatiu
    • Scale-Up! El joc empresarial
    • Visita Odoo
    Obtindre el programari
    • Descarregar
    • Comparar edicions
    • Novetats de les versions
    Col·laborar
    • GitHub
    • Fòrum
    • Esdeveniments
    • Traduccions
    • Converteix-te en partner
    • Services for Partners
    • Registra la teva empresa comptable
    Obtindre els serveis
    • Troba un partner
    • Troba un comptable
    • Contacta amb un expert
    • Serveis d'implementació
    • Referències del client
    • Suport
    • Actualitzacions
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Programar una demo
  • Preus
  • Ajuda

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

  • CRM
  • e-Commerce
  • Comptabilitat
  • Inventari
  • PoS
  • Projectes
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
Ajuda

Create multi records from one server action

Subscriure's

Get notified when there's activity on this post

This question has been flagged
development
1 Respondre
2742 Vistes
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
Descartar
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
Descartar
Ali Ammar
Autor

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
Autor

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
Autor

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
Autor

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
Autor

What should I write in this method?

Ali Ammar
Autor

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!

Registrar-se
Related Posts Respostes Vistes Activitat
Odoo 17: How to Create a Custom Table Header in List (Tree) View Using OWL
development
Avatar
0
de des. 25
97
How to create custom Table header by using odoo 17
development
Avatar
Avatar
1
de des. 25
302
Oddo Integration with Intune
development
Avatar
0
de des. 25
1741
@guia@¿Cómo hablar con una persona en Air France?
development
Avatar
0
de des. 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
de des. 25
552
Community
  • Tutorials
  • Documentació
  • Fòrum
Codi obert
  • Descarregar
  • GitHub
  • Runbot
  • Traduccions
Serveis
  • Allotjament a Odoo.sh
  • Suport
  • Actualització
  • Desenvolupaments personalitzats
  • Educació
  • Troba un comptable
  • Troba un partner
  • Converteix-te en partner
Sobre nosaltres
  • La nostra empresa
  • Actius de marca
  • Contacta amb nosaltres
  • Llocs de treball
  • Esdeveniments
  • Pòdcast
  • Blog
  • Clients
  • Informació legal • Privacitat
  • Seguretat
الْعَرَبيّة 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 és un conjunt d'aplicacions empresarials de codi obert que cobreix totes les necessitats de la teva empresa: CRM, comerç electrònic, comptabilitat, inventari, punt de venda, gestió de projectes, etc.

La proposta única de valor d'Odoo és ser molt fàcil d'utilitzar i estar totalment integrat, ambdues alhora.

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