Menu

Examples

Pieter van der Merwe

Creating an OpenERP session and ObjectAdapter

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());
}

Simple read of customers

Reads customer name and email addresses.

FilterCollection filters = new FilterCollection();
filters.add("customer","=",true);
RowCollection partners = partnerAd.searchAndReadObject(filters, new String[]{"name","email"});

Displaying values from a RowCollection

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"));
}

Update email address of a partner

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");

Creating a new partner

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());

Related

Wiki: Getting Started

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.