Skip to Content
Menu
This question has been flagged
2 Replies
6959 Views

Hi guys, new to Odoo, i have to fetch the category a product is related to, using the read method of Odoo API. I get a list of objects, then for each category i need to extract the field product_tmpl_ids, a list of integers. I'm not sure how to do it. This is the method: 


public List readModelfields(String modelName, List<Integer>ids, List<String> fields, Integer uid)throws XmlRpcException {
        List record = (List)Arrays.asList((Object[])models.execute("execute_kw", Arrays.asList(
                db, uid, password,
                modelName, "read",
                 ids,
                new HashMap() {{
                    put("fields", fields);
                }}
            )));
        return record;
   }

This is the rest of the code:

List<String> fields = new ArrayList<>();
fields.add("product_tmpl_ids");
List categoryIds = (List<Integer>)service.searchByStrParameter("product.public.category", "name", "A - Administration / Office", uid);
List result = (List)service.readModelfields("product.public.category", categoryIds, fields, uid);

Can anybody help? how to extract the fields from the fetched objects?

Avatar
Discard
Author Best Answer

This is the solution to my problem:

public List searchReadModelfields(String modelName, List domain, List<String> fields, Integer uid) throws XmlRpcException {
        return (List)Arrays.asList((Object[])models.execute("execute_kw", Arrays.asList(
                db, uid, password,
                modelName,
                "search_read",
                Arrays.asList(domain),
                new HashMap() {{
                    put("fields", fields);
                }}
        )));
    }


 List<String> fields = new ArrayList<>();
 
  fields.add("product_tmpl_ids");
 
  List domain = Arrays.asList(Arrays.asList("name", "=", "A - Administration / Office"));
 
// return list of hashmaps)
List result = (List)service.searchReadModelfields("product.public.category", domain, fields, uid);

HashMap values;
Integer partner_id;
Object[] category_ids;
for(Object partner_data: result) {
    values  = (HashMap)partner_data;
    partner_id = (Integer) values.get("id");
    category_ids = (Object [])values.get("product_tmpl_ids");

    System.out.printf("{partner_id: %s, ", partner_id);
    System.out.print("category_ids:[");
    for(Object category_id: category_ids) {
        System.out.printf("%s,",
                category_id
        );
    }
    System.out.println("]}");
}
 

Avatar
Discard

You're the best.

Best Answer

Hi Simeon:

You may find the following useful. There are examples of Java code on the right for each of the calls.

https://www.odoo.com/documentation/13.0/webservices/odoo.html

Avatar
Discard
Related Posts Replies Views Activity
1
Dec 22
1382
3
Jan 23
4829
0
Sep 22
2053
1
Mar 22
3579
1
Feb 22
3827