This question has been flagged
1 Reply
4777 Views

I am trying to write code to raise a warning when a user forgets to enter phone number or email in the contact linked to a sales order. This is what I have so far and it works:

Model: sale.order


if not record.partner_id.phone and not record.partner_id.mobile:
raise Warning('Customer phone number is not set')
if not record.partner_id.email:
raise Warning('Customer email is not set')
if not record.partner_shipping_id.phone and not record.partner_shipping_id.mobile:
raise Warning('Shipping Address phone number is not set')
if not record.partner_shipping_id.email:
raise Warning('Shipping Address email is not set')
if len(record.partner_shipping_id.name) > 35:
raise Warning('Delivery Address: Name field is greater than 35 characters')
if len(record.partner_shipping_id.street) > 35:
raise Warning('Delivery Address: Street 1 field is greater than 35 characters')​

My question is how can I raise one Warning with all the missing fields? Basically Make my warning text to be dynamic and print a list of the Display Name of all the missing fields.


Avatar
Discard
Best Answer

Hi,

You can show the field values inside the warning message like this, see the sample:


raise exceptions.ValidationError(_("Cannot create new attendance record for %(empl_name)s, the employee was already checked in on %(datetime)s") % {
'empl_name': attendance.employee_id.name_related,
'datetime': fields.Datetime.to_string(fields.Datetime.context_timestamp(self, fields.Datetime.from_string(attendance.check_in))),
})

Thanks

Avatar
Discard
Author

@Niyas Thanks for the comment.

My question was to display the field name (NOT the string stored in that field)

For example:

If the user forgot to input a value for Field "Phone" (partner_id.phone) and "Email" (partner_id.email)

The warning message should say

"The following fields are not set: 'Phone', 'Email'"

Currently my code goes line by line, and will display a warning

"Customer phone is not set"

and then when user enters a phone number it will then display another warning

"Customer email is not set"

Using the if condition you can achieve it. What is complication you face in it