Search code examples
apiweb-servicesodooxml-rpc

How to set a related field with a one 2many, many2one and many2many field, using Odoo API(XMLRPC)?


new to Odoo, i'm trying to create a web service in Java, loading data into Odoo, in particular, into the models product.template and technical.sheet, a custom model that i created. For one product there can be 0 or many technical sheets and for one technical sheet - one product. In the model technical.sheet i created a field x_product_id, the type - many2one and the object relation – product.tamplate. In product.template i create the same field, x_product_id, but the type is one2many, the object relation - technical.sheet and the field relation x_product_id(technical.sheet). I’ve succeded to load the products and the technical sheets but when I try to set the relational fields, I get errors. Does anybody have an example or an idea how to set fields of type one2many, many2one and many2many, using Odoo external API? Thanks! This is the piece of code:

for(Article article: articles.values()) {

if(article.getTechSheets().size()>0){
        technicalSheetsMap.put(article.getMfsIdentifier(), article.getTechSheets());
}

ArrayList<Integer> techSheetsIds = new ArrayList<>();

for(TechnicalSheet sh: article.getTechSheets()){
    techSheetsIds.add(Integer.valueOf(sh.getId()));
}

ArrayList<Integer> arrids = new ArrayList<Integer>();
arrids.add(Integer.valueOf(article.getMfsIdentifier()));

/* Load of the article into model product.tamplate of the odoo database */
final Integer id = (Integer)models.execute("execute_kw", Arrays.asList(
           db, uid, password,"product.template", "create",
                 Arrays.asList(new HashMap() {{ 
                                               put("is_published", true);
                                               put("active", true);
                                               put("x_standartization_level", article.getStandartizationLevel());
                                               put("id", Integer.valueOf(article.getMfsIdentifier()));
                                               put("default_code", article.getCode());
                                               put("name", article.getLabelEn());
                                               put("x_msf_identifier", article.getMfsIdentifier());
                                               put("display_name", article.getLabelEn());
                                               put("x_label_en", article.getLabelEn());
                                               put("x_oca", article.isOca());
                                               put("x_ocb", article.isOcb());
                                               put("x_ocba", article.isOcba());
                                               put("x_ocg", article.isOcg());
                                               put("x_ocp", article.isOcp());
                                               put("x_cold_chain_group", article.getTermosensitive());
                                               put("x_justification_id", article.getJustificationId());
                                               put("x_transport_un_code_id", article.getTransportUnCodeId());
                                               put("x_picture_content", article.getPictureNb());
                                               put("x_picture_label", article.getPictureLabel());
                                               put("x_controlled_substance", article.getControlledSubstance());
                                               put("x_medical_device_class", article.getMedicalDeviceClass());
                                               put("x_code", article.getCode());
                                               put("x_type", article.getType());
                                               put("x_family_id", article.getFamilyId());
                                               put("x_group_id", article.getGroupId());
                                               put("x_who_id", article.getWhoIds());
                                               put("x_product_id", Integer.valueOf(article.getMfsIdentifier()));
                                                       }})

                  ));



                  for(TechnicalSheet sheet: article.getTechSheets()){


                          final Integer idsh = (Integer)models.execute("execute_kw", Arrays.asList(
                                 db, uid, password,
                               "x_product.technical_sheet", 
                               "create",
                               Arrays.asList(new HashMap(){{    
                                                             put("id", sheet.getId());
                                                             put("x_name", sheet.getLabelEn());
                                                             put("x_description", sheet.getDefinition());
                                                             put("display_name", sheet.getLabelEn());
                                                             put("x_product_id", Integer.valueOf(article.getMfsIdentifier()));
                                                             put("x_norms", sheet.getNorms());
                                                             put("x_precat", sheet.getPrecat());
                                                           }})
                         ));

                  }

          }

Solution

  • To fill or manipulate one2many or many2many field with according values (records) you need to use special commands.

    In the following example we add a new order line (order_line One2many):

    main.models.execute("execute_kw", Arrays.asList(
                main.db, uid, main.password,
                "sale.order", "write",
                Arrays.asList(
                    Arrays.asList(20),
                    new HashMap() {{ 
                            put("order_line", 
                                 Arrays.asList(
                                     Arrays.asList(0, 0, 
                                        new HashMap() {{
                                            put("product_id", 3);
                                        }}
                                     )
                                 )
                            ); 
                    }}
                )
            ));