0

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 column. I would like to search the partner_name in my list using that column partner_name, but I'm a little confused on how to achieve this. Please needed some help.

This is what I've tried:

BaseFragment

  @Override
  public void onViewBind(View view, Cursor cursor, ODataRow row) {
      getPartnerIds(row);
      OControls.setText(view, R.id.partner_name,  row.getString("partner_name")); // displays false
      ....
    }
}

  private void getPartnerIds(ODataRow row){
      OValues oValues = new OValues();
      oValues.put("partner_id", row.get("partner_id"));
      saleOrder.storeManyToOne(oValues);
}

updated:

I noticed that even though I created

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

no column was created.

Updated: partner_name column with odoo functional

Edit: Just place the 'if (type.isAssignableFrom(Odoo.Functional.class)' before the 'if (type.getDeclaringClass().isAssignableFrom(Odoo.api.class))' to have the correct values.

plexus
  • 101
  • 1
  • 11

1 Answers1

1

Define the partner_name field like below:

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

public String storePartnerName(OValues values) {
    try {
        if (!values.getString("partner_id").equals("false")) {
            JSONArray partner_id = new JSONArray(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:

      @Override
      public void onModelUpgrade(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 null object 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 array at character 12 of [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 = new JSONArray(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) + "";
Kenly
  • 24,317
  • 7
  • 44
  • 60
  • I also tried doing that but when I put row.getString("partner_name") in onViewBind(), it will show as false instead the partner name and when I tried adding partner_name in onCreateLoader() for me to search keywords related to partner_name an error occured stating that 'no such partner_name column'. Also, do I need to call storePartnerName() in BaseFragment class? – plexus Jul 15 '20 at 01:10
  • You can check how they used partner name in [Sales](https://github.com/Odoo-mobile/crm/blob/master/app/src/main/java/com/odoo/addons/sale/Sales.java) list and how they declare it in [SaleOrder](https://github.com/Odoo-mobile/crm/blob/master/app/src/main/java/com/odoo/addons/sale/models/SaleOrder.java#L78) model. Maybe something is missing in the list view. – Kenly Jul 15 '20 at 01:42
  • I added these lines in onViewBind() -> if row.getString("partner_name").equals("false")) { OControls.setGone(view, (R.id.partner_name)); } else { OControls.setVisible(view, R.id.partner_name); OControls.setText(view, R.id.partner_name, row.getString("partner_name")); } but it seems that setGone is triggered and doesn't show the partner name, trying also to onCreateLoader no such column partner_name error occured – plexus Jul 15 '20 at 02:30
  • If you added the partner name after you installed the application, the column will not be created, check my edit. – Kenly Jul 17 '20 at 04:39
  • The ''no such column: partner_name" error was still shown though I reinstalled the app/clear caches/clean app's data and also adding the lines from the example onModelUpgrade() and changing the database version but still an error "no such column: partner_name" appeared. – plexus Jul 17 '20 at 08:49
  • Please share a minimum example to reproduce that issue. – Kenly Jul 17 '20 at 12:25
  • I added the new OColumn just like the above examples. I also added your examples in onModelUpgrade() and database version. After all I did those changes, I uninstall and reinstall the app. After the data was synced in list view, the part where the data should display the partner name showed as false. I tried also adding partner name in onCreateLoader() like this where += " client_order_ref like ? or partner_name like ?"; and when I try to search using partner name an error occured stating that "no such column: partner_name" – plexus Jul 20 '20 at 02:03
  • If you reinstall the application, you don't need to override `onModelUpgrade()`. Declaring the `partner_name` column in models is enough to add the column to the database. – Kenly Jul 20 '20 at 14:58
  • If I don't add a the odoo functional in partner_name column just a normal declaration, a column was being created with null values but when I add the odoo functional, there's no column being created/added. – plexus Jul 21 '20 at 00:57
  • I added the `partner_name` as described above and the column was created and I could use the filter. When you use `setLocal()`, it means that the column will not be available on the server and it will be excluded when handling the result of the synchronization. Please try to add the same column on a clean version of the framework – Kenly Jul 21 '20 at 01:38
  • I tried using the clean version of the framework from github, add the partner_name column and it's functional but row.getString("partner_name") in listview still showed as false, I checked the table but there's no column created for partner_name. I reinstall again and add the onModelUpgrade() example hoping it will work but still the same output. I don't know why it's not working and what causes it not to work. – plexus Jul 23 '20 at 01:44
  • Please share the clean version, I want to reproduce the same bug. – Kenly Jul 23 '20 at 02:34
  • This is the v2.3.0 of the framework https://github.com/Odoo-mobile/framework.git – plexus Jul 23 '20 at 02:42
  • I need the version you modified, to try it with your modification. I used the same version to add a similar field and it worked. – Kenly Jul 23 '20 at 02:44
  • https://github.com/Plexus-byte/sample this repo has two branches, the master branch is still using lower versions of compiledSdkVersions and classpath dependencies, the partner_name column was created but only few are shown as partner name mostly it shows false, the second branch is where the versions are upgraded and I enabled android.enableJetifier & android.useAndroidX, but when I tried running it, no column was create. Is this because of upgrades to newer versions that's why no column was created while using odoo ffunctional? – plexus Jul 27 '20 at 01:17
  • In my recent commits, there's no partner_name column created, sqlite error for partner_name. – plexus Jul 28 '20 at 03:46
  • You did not update the OModel compatibleField method, you should get the same error. – Kenly Jul 28 '20 at 03:56
  • I've seen the edits in master branch files, when I tried running the app, got the same error, no such column: partner_name. Is the odoo functional only work in older versions? – plexus Jul 28 '20 at 11:20
  • I have made the changes to the `implementation_gradle` branch. The `partner_name` was created successfully. – Kenly Jul 28 '20 at 12:03
  • It worked! a partner_name was created but did all the partner names successfully shown in your partner_name when it was created? In my case some were false. I updated above to show how it looks like. – plexus Jul 30 '20 at 02:04
  • Some partner names are shown as `false`, check my edit. – Kenly Jul 31 '20 at 10:30