Odoo Help


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.


If statement triggering action - send mail

Dr Obx
on 1/4/16, 9:42 AM 691 views

Guys, how to do it:

#Read quantity from table, if qty_available <50 send message



 def _get_current_stock(self, cr, uid, ids, field_name, arg, context=None):

res = []

tot_qty = 0

product_id = 0

for obj in self.browse(cr, uid, ids, context=context):

cr.execute(""" select sum(qty) from stock_quant where product_id = %s and location_id != 7;""" %(obj.product_id.id))

tot_qty = cr.fetchall()

if tot_qty<50: #here message sending procedure, how to  send message ?


print '==========================================='

print '|| Product QTY ', res

print '==========================================='

return res



Tarek Mohamed Ibrahim

--Tarek Mohamed Ibrahim--
| 5 3 7
Gîza, Egypt
--Tarek Mohamed Ibrahim--

I am an old VFP developer on ERP

I have moved to 2p since Nov-2014 and started developing with Python on Odoo.



Tarek Mohamed Ibrahim
On 1/4/16, 10:43 AM

First, you have to define the outgoing mail server in Settings->General settings, you can use for example your gmail account.

You have to test it and get an 'ok' message , if there is any error, it will show you a message what needs to be changed

I used the following code to send emails under a certain condition

    def mailing(self, cr, uid, ids,vals,context=None):
domain=[('name','=','MY_TEMPLATE')] # here is my email template which contains the message
templates = self.pool.get('email.template').search(cr,uid,domain)
if not templates:
template = self.pool.get('email.template').browse(cr,uid,templates[0])
if template.email_to:                             #The 'To' email is defined in the template
self.pool.get('email.template').send_mail(cr, uid, template.id, ids[0], True, context=context)

I applied this code and sent emails successfully

Oh great Tarek, thank you, I'll try. However I would like to use a special template or define it inside the method. What do you think ?

Dr Obx
on 1/4/16, 10:46 AM

Also i would like to add some data from my module ... so either template must be generated by module or template will be able to use data from module. Don't know yet which way to go :)

Dr Obx
on 1/4/16, 10:52 AM

But first I have to find out how to use this definition :) How to connect it to if statement :)

Dr Obx
on 1/4/16, 10:55 AM

to add data to your template you have to define variables in the template based on 'object' variable, check the following example : Dear ${object.responsible.name}, The document ${object.name} status has been changed to ${object.state}, pls check and take the appropriate action

Tarek Mohamed Ibrahim
on 1/4/16, 6:41 PM

Got it ;) Because i'm quiet new in this, still looking how to trigger it, how to call the function which generate the message (def mailing).

Dr Obx
on 1/5/16, 9:41 AM

Pls note that this function is not standard, I just created it to collect this block of code in one place, you can think your own ideas also. to use it you can do something like the following: if some condition : self.mailing(cr, uid, ids, vals, context)

Tarek Mohamed Ibrahim
on 1/5/16, 12:47 PM

Now asking for vals. vals not defined.

Dr Obx
on 1/7/16, 8:39 AM

As a testing stuff I created an empty form which is displayed in console once the statement is true. What about template ? Have you created a new one or used an existing one ? And what about vals ?

Dr Obx
on 1/8/16, 7:35 AM

Tarek, by any chance, can you tell me how can i using this method send more than just short message which contain only one item (). I would like to create a list of products and put into the message then send.

Dr Obx
on 1/12/16, 12:04 PM
Dr Obx
On 1/9/16, 3:51 PM

Awesome, already got it, working fine. Now i need to create a template with appropriate fields :)

Can you show me your example mate?

on 1/27/16, 2:49 AM

On 27 January 2016 at 07:49, wizardz <stee.diez@gmx.ch> wrote:

Can you show me your example mate?


Dr Obx
on 1/27/16, 4:41 AM
On 1/27/16, 3:19 AM

Can someone give me a working example of  the modul ?

Already gave you an example of the function and how to apply. What else do you need ?

Dr Obx
on 1/27/16, 3:04 PM

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)


Asked: 1/4/16, 9:42 AM
Seen: 691 times
Last updated: 1/27/16, 3:19 AM