Set relationship dropdown via javascript controller

Hi everyone,

I'm trying to set a relationship dropdown on the "One" side (One-To-Many relationship) in the create javascript controller.

With relate fields I'm able to simply do this:

this.model.set('account_c', accountName);

this.model.set('account_id_c', accountId);

and the dropdown updates to show the account name.

When using a proper relationship however this doesn't work. I can still set account_id_c, which will be saved, but setting account_c or account_name_c does not update the dropdown to reflect the change (keeps saying "Select account"). None of those appear in the model.attributes object anyway.

Is there another way to do this? I'm on Sugar 7.8.2.0 btw.

Best Regards,

Robin.

  • Go to the your modules' vardef extension:

    custom/<module>/Ext/Vardefs/vardef.ext.php

    Find the relationship in question.

    There should be two fields associated with that relationship, one for type relate and one for type link.

    For example, I have:

    $dictionary["wcont_WolframContracts"]["fields"]["wcont_wolframcontracts_accounts"] = array (
      'name' => 'wcont_wolframcontracts_accounts',
      'type' => 'link',
      'relationship' => 'wcont_wolframcontracts_accounts',
      'source' => 'non-db',
      'module' => 'Accounts',
      'bean_name' => 'Account',
      'side' => 'right',
      'vname' => 'LBL_WCONT_WOLFRAMCONTRACTS_ACCOUNTS_FROM_WCONT_WOLFRAMCONTRACTS_TITLE',
      'id_name' => 'wcont_wolframcontracts_accountsaccounts_ida',
      'link-type' => 'one',
    );
    $dictionary["wcont_WolframContracts"]["fields"]["wcont_wolframcontracts_accounts_name"] = array (
      'name' => 'wcont_wolframcontracts_accounts_name',
      'type' => 'relate',
      'source' => 'non-db',
      'vname' => 'LBL_WCONT_WOLFRAMCONTRACTS_ACCOUNTS_FROM_ACCOUNTS_TITLE',
      'save' => true,
      'id_name' => 'wcont_wolframcontracts_accountsaccounts_ida',
      'link' => 'wcont_wolframcontracts_accounts',
      'table' => 'accounts',
      'module' => 'Accounts',
      'rname' => 'name',
    );

    Look at the relate definition and use the name and id_name values as the field names to set the id and name respectively

    In my example: 

    this.model.set('wcont_wolframcontracts_accountsaccounts_ida', id)
    this.model.set('wcont_wolframcontracts_accounts_name', name);

    HTH

    FrancescaS

  • Hi Francesca,

    Thanks for your response, works perfectly.

    Best regards,

    Robin.

  • So how do you CLEAR a relationship field that has a value in it?  I tried

    this.model.set('wcont_wolframcontracts_accountsaccounts_ida', '')
    this.model.set('wcont_wolframcontracts_accounts_name', '');

    but that doesn't really work, it seems to leave the name in place.  If I d
    this.model.set('wcont_wolframcontracts_accountsaccounts_ida', '')
    this.model.set('wcont_wolframcontracts_accounts_name', ' ');

    (see space in the name), that clears the name but doesn't really reset the field.