Each of the examples uses a Session object and ObjectAdapter from the OpenERP Java Api.
Session openERPSession = new Session("openerp1", 8069, "demo_database", "admin", "admin"); try { // startSession logs into the server and keeps the userid of the logged in user openERPSession.startSession(); ObjectAdapter partnerAd = openERPSession.getObjectAdapter("res.partner"); ////// ////// Example code snippet goes here ////// } catch (Exception e) { System.out.println("Error while reading data from server:\n\n" + e.getMessage()); }
Reads customer name and email addresses.
FilterCollection filters = new FilterCollection(); filters.add("customer","=",true); RowCollection partners = partnerAd.searchAndReadObject(filters, new String[]{"name","email"});
Continuing on from the previous example, you could read the partners like this:
for (Row row : partners){ System.out.println("Row ID: " + row.getID()); System.out.println("Name:" + row.get("name")); System.out.println("Email:" + row.get("email")); }
In this code snippet, the customer Agrolait's email address is changed to testemail@gmail.com.
FilterCollection filters = new FilterCollection(); filters.add("name","=","Agrolait"); RowCollection partners = partnerAd.searchAndReadObject(filters, new String[]{"name","email"}); // You could do some validation here to see if the customer was found Row AgrolaitRow = partners.get(0); AgrolaitRow.put("email", "testemail@gmail.com"); // Tell writeObject to only write changes ie the name isn't updated because it wasn't changed. boolean success = partnerAd.writeObject(AgrolaitRow, true); if (success) System.out.println("Update was successful");
Row newPartner = partnerAd.getNewRow(new String[]{"name", "ref"}); newPartner.put("name", "New Customer"); newPartner.put("ref", "Reference Number1"); partnerAd.createObject(newPartner); System.out.println("New Row ID: " + newPartner.getID());