odoo add validation for double field - odoo

I have this odoo py
class CrmProject(models.Model):
_name='crm.project'
customer_id = fields.Many2one('res.partner','Customer')
project_product_id = fields.Many2one('crm.project.product','Project Product')
how to validate and show a warning when the customer_id and project_product_id are inputted together with the same value as the one in the crm.project database? so it must be checked with the database first then show warning if it have the same value with the one in customer_id AND project_product_id input.
For example:
Database table crm.project
customer_id = 1
project_product_id = 2
Will create warning ONLY if input:
customer_id = 1
project_product_id = 2
Beside that no warning will be create
I have tried this but no warning created (update 25-04-2021)
#api.model
def create(self,vals):
vals['name'] = self.env['ir.sequence'].next_by_code('crm.project')
res = super(CrmProject,self).create(vals)
# Add code here
#res= super(CrmProject,self).create(vals)
customer_id = vals.get('crm.project.customer_id')
project_product_id = vals.get('crm.project.project_product_id')
get_customer_id = self.env['crm.project'].search([('customer_id','=',customer_id)])
get_project_product_id = self.env['crm.project'].search([('project_product_id','=',project_product_id)])
if get_customer_id and get_project_product_id:
raise UserError(_('The project has already been set before'))
else:
return res
Any help will be grateful

you can create constraint in your db with :
create unique index name_your_constraint
on crm_project (customer_id, project_product_id);

Related

How do I create a service ticket using CRM_ORDER_MAINTAIN?

I'm trying to learn the Function Module CRM_ORDER_MAINTAIN and so far I managed to create a standard order with partners and their roles, however now I'm having trouble creating a service ticket with its required fields.
I tried debugging the FM when calling it from WEBUI to see what structures and tables are to be filled but I'm having a bit of trouble figuring out which ones to fill and which ones are generated and don't know if I'm missing something small somewhere that's causing it not to save.
I think the error lies in this part of the code that handles Categorization, because the other code that handles adding partners worked for the standard order.
I'm not filling any GUIDS and only filling HANDLES because that's what I did for the standard order.
*****categorization: motive+submotive******
clear ls_input_fields.
clear ls_fieldsname.
ls_fieldsname-fieldname = 'CONC_KEY'.
INSERT ls_fieldsname INTO TABLE ls_input_fields-field_names.
ls_input_fields-ref_handle = 1."
ls_input_fields-ref_kind = gc_object_kind-orderadm_h.
ls_input_fields-objectname = 'SERVICE_OS'.
ls_subject-ref_handle = ls_orderadm_h-handle.
ls_subject-ref_handle_h = ls_orderadm_h-handle.
ls_subject-katalogart = 'Z1'.
ls_subject-codegruppe = 'ZCA00001'.
ls_subject-code = 'Z044'.
append ls_subject TO ls_osset-subject.
ls_osset-ref_handle = ls_orderadm_h-handle.
ls_osset-profile_type = 'A'.
ls_osset-subject_profile = 'ZCCCAST'.
append ls_osset to ls_service_os-osset.
ls_service_os-ref_handle = ls_orderadm_h-handle.
append ls_service_os to it_service_os.
INSERT ls_input_fields INTO TABLE lt_input_fields.
ls_subject-ref_handle = ls_orderadm_h-handle.
ls_subject-ref_handle_h = ls_orderadm_h-handle.
ls_subject-katalogart = 'Z1'.
ls_subject-codegruppe = 'ZCA00002'.
ls_subject-code = 'Z009'.
append ls_subject TO ls_osset-subject.
ls_osset-ref_handle = ls_orderadm_h-handle.
ls_osset-profile_type = 'A'.
ls_osset-subject_profile = 'ZCCCAST'.
append ls_osset to ls_service_os-osset.
ls_service_os-ref_handle = ls_orderadm_h-handle.
append ls_service_os to it_service_os.
INSERT ls_input_fields INTO TABLE lt_input_fields.
Any help is appreciated
For Service OS there is a multilevel categorization up to four levels. You can check the hierarchy via CRM_ORDER_READ FM in parameter ET_SERVICE_OS.
Also, you can check the hierarchy in the below structure CRMT_SRV_OSSET_WRK
To maintain the data for more than 2 levels of hierarchy you have to create a new ref GUID. You have to prepare and fill data to Create OS as below
CALL FUNCTION 'GUID_CREATE'
IMPORTING
ev_guid_16 = lv_ref_guid.
ls_subject-ref_handle = '0000000000'.
ls_subject-ref_guid = lv_ref_guid. " newly created ref GUID
ls_subject-cat_id = 'As per your req.'.
ls_subject-katalog_type = ''.
ls_subject-mode = 'A'. "For creation A, for update B
" maintain other Subject parameter as per your requirement
APPEND ls_subject TO lt_subject.
" you can skip preparing Ref-Object structure if you don't want to update
ls_refobj-ref_guid = lv_ref_guid. " newly created ref GUID
ls_refobj-product_id = "Product ID". " optional
ls_refobj-ref_handle = '0000000000'.
ls_refobj-main_object = abap_true.
ls_refobj-mode = 'A'. " mode A for creating, B for update
INSERT ls_refobj INTO TABLE lt_refobj.
ls_osset-ref_handle = '0000000000'.
ls_osset-ref_guid = "Header or Item GUID of Service".
ls_osset-subject_profile = 'SERVICE'.
ls_osset-profile_type = 'A'. " service profile type
ls_osset-refobject = lt_refobj.
ls_osset-subject = lt_subject.
INSERT ls_osset INTO TABLE lt_osset.
ls_service_os-ref_guid = "Header or Item GUID of Service".
ls_service_os-ref_kind = "A or B". " A for Header and B for Item
ls_service_os-osset = lt_osset.
INSERT ls_service_os INTO TABLE lt_service_os.

How to print records number in form view in odoo?

I have in my python file this function to count number of records in (Order Lines):
class Class_sale_order(models.Model):
_inherit = 'sale.order'
caseacocher_sale_order = fields.Boolean(string='Print customized')
new_field2 = fields.Integer(compute='function_count_saleorderlines')
#api.depends('order_line')
def function_count_saleorderlines(self):
for rec in self:
rec.new_field2 = len('order_line')
But when i see the form view i find that the value of filed is 10, the problem that i have only 5 records.
len('order_line') returns the size of the string 'order_line' which is 10 that's why you are getting the value 10. Set like following:
rec.new_field2 = len(rec.order_line)

BAPI for getting material and order BOM?

Is there a BAPI for getting material and order BOM?
Function modules won't help as those can't be called remotely.
You can use CSAP_MAT_BOM_READ module like this
DATA:
lt_stpo TYPE TABLE OF stpo_api02,
lt_stko TYPE TABLE OF stko_api02,
lt_dep_data TYPE TABLE OF csdep_dat,
lt_dep_descr TYPE TABLE OF csdep_desc,
lt_dep_order TYPE TABLE OF csdep_ord,
lt_dep_source TYPE TABLE OF csdep_sorc,
lt_dep_doc TYPE TABLE OF csdep_doc,
lt_doc_link TYPE TABLE OF csdoc_link,
lt_dmu_tmx TYPE TABLE OF csdmu_tmx,
lt_ltx_line TYPE TABLE OF csltx_line,
lt_stpu TYPE TABLE OF stpu_api01.
CALL FUNCTION 'CSAP_MAT_BOM_READ'
EXPORTING
material = 'P-501'
plant = '1000'
bom_usage = '1'
TABLES
t_stpo = lt_stpo
t_stko = lt_stko
t_dep_data = lt_dep_data
t_dep_descr = lt_dep_descr
t_dep_order = lt_dep_order
t_dep_source = lt_dep_source
t_dep_doc = lt_dep_doc
t_doc_link = lt_doc_link
t_dmu_tmx = lt_dmu_tmx
t_ltx_line = lt_ltx_line
t_stpu = lt_stpu
EXCEPTIONS
error = 1
OTHERS = 2.
It is remote-enabled so can be perfectly called outside

query_exec(SELECT * FROM myTable) fails on pyqt5

i have a function like below
def updateExpenseEntryToDb (self):
self.day = self.line_edit1.text()
self.category = self.line_edit2.text()
self.amount = self.line_edit3.text()
db = QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('expenses.db')
db.open()
query = QSqlQuery()
query.exec_("create table expense(date DATE primary key, "
"category varchar(20), amount varchar(20))")
query.exec_("insert into expense (date,category,amount) values('%s','%s','%s')" % (self.day, self.category, self.amount))
db.close()
db1 = QSqlDatabase.addDatabase('QSQLITE')
db1.setDatabaseName('expenses.db')
db1.open()
query1 = QSqlQuery()
query1.exec_("SELECT date, category, amount FROM expenses.expense")
while (query1.next()):
extractedDate = query1.value(0).toString()
extractedcategory = query1.value(1).toString()
extractedAmount = query1.value(2).toString()
self.line_edit1.setText(extractedDate)
self.line_edit2.setText(extractedcategory)
self.line_edit3.setText(extractedAmount)
db1.close()
Insertion of values into DB works but not the retrieval of info from DB. What am i doing wrong ? seems like select query doesnt retrieve anything at all
Put query.first() before while (query1.next()): But, the first row won't be used.
Better:
query.first()
while query.isValid():
# Your Code
query.next()

Update BSEG-ZUONR with Function Module

I successfully change BKPF-BKTXT with FM CHANGE_DOCUMENT but why can't I change BSEG-ZUONR with FM CHANGE_DOCUMENT too?
Here's the FM CHANGE_DOCUMENT:
CALL FUNCTION 'CHANGE_DOCUMENT'
TABLES
T_BKDF = t_bkdf
T_BKPF = t_bkpf
T_BSEC = t_bsec
T_BSED = t_bsed
T_BSEG = t_bseg
T_BSET = t_bset
* T_BSEG_ADD =
.
Here's the code to change BKPF-BKTXT (succeeded):
wa_t_bkpf-mandt = sy-mandt.
wa_t_bkpf-bukrs = '1000'.
wa_t_bkpf-gjahr = gjahr_import.
wa_t_bkpf-belnr = belnr_import.
wa_t_bkpf-bktxt = zuonr_import.
APPEND wa_t_bkpf TO t_bkpf.
Here's the code to change BSEG-ZUONR (failed):
wa_t_bseg-mandt = sy-mandt.
wa_t_bseg-bukrs = '1000'.
wa_t_bseg-gjahr = gjahr_import.
wa_t_bseg-belnr = belnr_import.
wa_t_bseg-buzei = '1'.
wa_t_bseg-zuonr = zuonr_import.
APPEND wa_t_bseg TO t_bseg.
As author has no time to confirm, I can do this for him as I just tested this case.
If we pass to FM all parameters from its signature the update runs smoothly. For example, like this:
DATA: lt_bkdf TYPE TABLE OF bkdf,
lt_bkpf TYPE TABLE OF bkpf,
wa_bkpf TYPE bkpf,
lt_bsec TYPE TABLE OF bsec,
wa_bseg TYPE bseg,
lt_bsed TYPE TABLE OF bsed,
lt_bseg TYPE TABLE OF bseg,
lt_bset TYPE TABLE OF bset.
wa_bkpf-mandt = sy-mandt.
wa_bkpf-bukrs = '5900'.
wa_bkpf-gjahr = gjahr_import.
wa_bkpf-belnr = belnr_import.
wa_bkpf-bktxt = 'Batch'.
APPEND wa_bkpf TO lt_bkpf.
wa_bseg-mandt = sy-mandt.
wa_bseg-bukrs = '5900'.
wa_bseg-gjahr = gjahr_import.
wa_bseg-belnr = belnr_import.
wa_bseg-buzei = '1'.
wa_bseg-zuonr = '20151131'.
APPEND wa_bseg TO lt_bseg.
CALL FUNCTION 'CHANGE_DOCUMENT'
TABLES
t_bkdf = lt_bkdf
t_bkpf = lt_bkpf
t_bsec = lt_bsec
t_bsed = lt_bsed
t_bseg = lt_bseg
t_bset = lt_bset
.
COMMIT WORK.
All FM table parameters except the last one are mandatory.
Do not use this FM
CALL FUNCTION 'CHANGE_DOCUMENT'
This FM is changing all other fields to initial if not provided.
CALL FUNCTION 'FI_DOCUMENT_CHANGE'
It seems that this FM cannot be used to change line item which has account type (BSEG-KOART) - 'S' (GL Account).
Try this FM:
'FI_ITEMS_MASS_CHANGE'
The field zuonr references to an object it belongs to.
For example a purchase order.
Lets asume you pay a position of a purchase order.
A document in bkpf/bseg is created (and more).
Bseg-Zuonr contains the number of this purchase order position.
If you were allowed to change this field, you would destroy the referential integrity of the data. It would point to a purchase order position it was not created from or one that doesn't exist at all.
So from a business standpoint it makes no sense to ever change this field after it is created, therefore SAP will never allow to change it.