Skip to content Skip to sidebar Skip to footer

Ocolumn With Odoo Functional In Odoo Mobile Framework Not Working

I have this OColumn partner_name = new OColumn('Partner', OVarchar.class).setLocalColumn(); in my sale order model class with odoo functional method that depends on partner_id colu

Solution 1:

Define the partner_name field like below:

@Odoo.Functional(method="storePartnerName", store=true, depends={"partner_id"})
OColumn partner_name = newOColumn("Partner name", OVarchar.class)
                       .setLocalColumn();

publicStringstorePartnerName(OValues values) {
    try {
        if (!values.getString("partner_id").equals("false")) {
            JSONArray partner_id = newJSONArray(values.getString("partner_id"));
            return partner_id.getString(1);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return"false";
}

You can simply get the partner_name using:

row.getString("partner_name")

EDIT:

Note that database is created when you first time run your application, or when you clean your data from app setting. You need to clean application data everytime when you update your database column.

If the column was added after the database creation, it will not be added to the corresponding table. This is because the database is not upgraded. To fix this issue you can:

  • Clean application data to update your database column

  • Remove user account (This will delete database) or reinstall the application to recreate the database.

  • Or you can change DATABASE_VERSION in odoo/datas/OConstants then override onModelUpgrade method in sale order model and upgrade the table manually (alter sale order table and add the partner name column using SQL query: ALTER TABLE sale_order ADD partner_name VARCHAR(100)). When a new sale order is created and synchronized, the partner name should be computed and stored automaticaly.

    I noticed that the partner name was not set for existing records after synchrinization so I added another SQL query to compute and set the value of partner name for old records.

    Example:

    @OverridepublicvoidonModelUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
              db.execSQL("ALTER TABLE sale_order ADD partner_name VARCHAR(100)");
              db.execSQL("UPDATE sale_order SET partner_name = (SELECT name from res_partner WHERE _id=partner_id) WHERE partner_name IS NULL AND partner_id IS NOT NULL");
      }  
    

Edit (config): using the new configuration you will get the following error (which will prevent creating fields using annotations):

W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Class.isAssignableFrom(java.lang.Class)'on a nullobject reference
W/System.err:     at com.odoo.core.orm.OModel.compatibleField(OModel.java:349)

CODE:

if (type.getDeclaringClass().isAssignableFrom(Odoo.api.class)) {

Try to remove .getDeclaringClass()

Edit: not all partner names are shown

There is a org.json.JSONException error that happens when it try to convert partner_id string to a JSON array.

W/System.err: org.json.JSONException: Unterminated arrayatcharacter12of [114.0, UH PARTNER]

The error happens when it try to convert names containing spaces. To avoid that you can cast partner_id string to a list of objects.

In partnerName method, replace the following code:

JSONArray partner_id = newJSONArray(values.getString("partner_id"));
return partner_id.getString(1);

With:

List<Object> partner_id = (ArrayList<Object>) values.get("partner_id");
return partner_id.get(1) + "";

Post a Comment for "Ocolumn With Odoo Functional In Odoo Mobile Framework Not Working"