Module Relationships load_relationship()

Hi all,

been busy learning lots about sugar and made masses of progress with moving from Salesforce to sugar CRM but just hit another snag and wanted to check if my login or understanding was correct.

We have two custom modules Holokote (holo_holokote) and Feature Keys (holo_featurekey), these should have a relation ship of one holokote can have many feature-keys.

Our original sugar integrators did this by a custom field in holokote to store the id of the feature-key, this means only one feature key per holokote and we cant see the relationships in sugar.

to fix this i created a one to many relationship in sugars holokote module, which created  `holo_holokote_holo_featurekey_1_c` table, all good

I now need to alter the api method to use this relationship when saving a feature key instead of using the custom field

    $fkey = BeanFactory::newBean('holo_FeatureKey');
    $fkey->name               = $args['name'];
    $fkey->status             = $args['status'];
    $fkey->holokotetype       = $args['type'];

    // OLD Relationships by custome fields
    $fkey->s_serialnumber_id_c                    = $args['serialNumber'];
    $fkey->holo_holokote_id_c                     = $args['holokote'];
    $fkey->holo_featurekey_contactscontacts_ida   = $args['contact'];
   
    $fkey->save();

    // bean relationship
    if ($fkey->load_relationship('holo_Holokote')) {
      $fkey->holo_Holokote->add($fkey->holo_holokote_id_c);
    }

trying to use the load_relationship to create this relationship but this isn't getting saved

we use sugar 9 cloud

  • i created myself a quick api method to pass a feature key ID and get the relationship holokote

        public function relTest($api, $args) {
          $result = [
            'totalSize'   => 0,
            'done'        => true,
            'success'     => false,
            'errors'      => array('Feature Key not created'),
          ];

          $fkey = BeanFactory::retrieveBean('holo_FeatureKey', $args['id']);
          $result['fkey'] = $fkey->id;;
          if ($fkey->load_relationship('holo_holokote_holo_featurekey_1')) {
            // $fkey->holo_Holokote->add($fkey->holo_holokote_id_c);
            // $hk = $fkey->holo_holokote_holo_featurekey_1->get();
            $hk = $fkey->holo_holokote_holo_featurekey_1->getBeans();
            return $hk;
          }
          return $result;
        }

    if i use line 13 the get method postman returns the related id

    [
        "31de5684-db94-11e9-a43a-06a63d65978a"
    ]

    if i then use line 14 the getBeans method, postman returns nothing, I thought getBeans returned an array of all related beans with full details, I checked i was using the correct name in load_relationship() and have includes a screen shot of the relationship in sugar

  • "Our original sugar integrators did this by a custom field in holokote to store the id of the feature-key"

    If your integrator stored the fkey ID on the holokote, you won't be able to get the holokote key from fkey.  You'd need to find the holokote record that has the current fkey ID stored in the custom field.

    But this: 

    $fkey->holo_holokote_id_c

    Suggests that the holokote key is stored on the fkey module.  Is it on both?

    If you do have that key on your fkey module, you can use line 12.

          if ($fkey->load_relationship('holo_holokote_holo_featurekey_1')) {
            $fkey->holo_Holokote->add($fkey->holo_holokote_id_c);

    Problem is that you're not using the the correct syntax in line 12.  It should be:

          if ($fkey->load_relationship('holo_holokote_holo_featurekey_1')) {
            $fkey->holo_holokote_holo_featurekey_1
    ->add($fkey->holo_holokote_id_c);
          }

    You can't use the module name, you have to use the name of the relationship.

  • Ken McCartney sorry just had another thought, our admin team have asked if I can create a button for then to click to generate the relationship if needed as the mass data import has holes in the date. Anyways I created an extra option in the feature key dropdown to call the following .js, would this be the right way to go about doing this, I didn't want to use a save hook as they would want to do the without needing to edit the feature key.

        initialize: function (options) {
            this._super('initialize', [options]);
            this.context.on('button:link_holokote:click', this.linkHolokote, this);
        },

        linkHolokote: function(model) {
          var id          = this.model.get('id'),
            fkeyBean      = App.data.createBean('holo_FeatureKey', {id : id});

          fkeyBean.fetch({
            success: function(model, data) {
              // console.log(model, data, fkeyBean);
              App.api.getOAuthToken();
              url = App.api.buildURL('holo_Holokote/'+data.holo_holokote_id_c+'/link/holo_holokote_holo_featurekey_1/'+data.id);
              console.log(url);
              App.api.call('POST', url, null, {
                success: function(data) {}
              });
            }
          });
        },