Not able to relate contacts and notes custom code

Hello

I am trying to create a note and related it to a given contact, but I am getting this error message

Notice: Undefined property: Note::$contacts in /custom_Notes.php on line 55

Fatal error: Call to a member function add() on null in \custom_Notes.php on line 55

And I think I am relating the beans correctly

  $notas = BeanFactory::getBean('Notes');
      $notas->name = $name;
      $notas->description = $desc;
      $notas->id = create_guid();
      $notas->new_with_id = true;
      $notas->filename = $name.'.pdf';
      $notas->tipo_documento_c = $tipoDoc;

      $notas->load_relationship('contacts');
      $notas->contacts->add($beanContact);
      global $current_user;
      $notas->assigned_user_id=$current_user->id;
      $notas->save();

Is there any other way to relate the note created to the $beanContact??

I am using SugarCRM CE 6.5

Thanks a lot

  • Hi 

    You only can call load_relationship on an existing record, additionally the link on Notes to Contacts is 'contact', so the code may looks like that:

    global $current_user;

    $notas
    = BeanFactory::getBean('Notes');
    $notas->name = $name;
    $notas->description = $desc;
    $notas->id = create_guid();
    $notas->new_with_id = true;
    $notas->filename = $name.'.pdf';
    $notas->tipo_documento_c = $tipoDoc;
    $notas->assigned_user_id=$current_user->id;
    $notas->save();$notas->load_relationship('contact');
    $notas->contact->add($beanContact);

    Regards

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Thanks a lot André

    Another question that I have is if I would be able to call the "load_relationship" between contacts and a custom module in a logic_hook.

    I mean, I am creatig a record in a custom module that has a 1:1 relationship with "Contacts", so when I create the record of the custom module, I select the contact I want to realte with.

    Then I call an "after_save" logic_hook to run some actions against the contact related.... but never get to load such contact

    Here is the code:

    if ($bean->load_relationship('contacts'))
    {
        //Fetch related beans
        $relatedBeans = $bean->contacts->getBeans();
    }
    $existe =null;
    Tools::LogWrite("Num total related records:".sizeof($relatedBeans));

    I have tried to use "contacts" and "contact", but "relatedBeans is always empty.

    How can I recover this related record in this logic_hook?. AS it is an after_save, the custom record must be created already right?

    Thanks a lot

  • If you created the relationship through studio then the link name will not be just "contact" or "contacts", instead it will be a bigger string which concatenates both custom module and Contacts.

    Take a look at custom/Extension/modules/Contacts/Ext/Vardefs/. Look for a file whose name looks like the name of your custom module. Open up it and search for a field whose type is "link" . That will be the link name to be passed as parameter of method "load_relationship".

    Regards

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Thanks!.... I dont know how I could forget about this!!!....hours trying to find the error!

    Thanks a lot!

  • There is NO real relation betweeen contacts and notes.You can see that in the database, there is no table contacts_notes or notes_contacts.

    The contact is related by the flex relate field "parent" which is implemented by the two fileds parent_type and parent_id.

    So you only have to set

    $notas->parent_type = 'Contacts';

    $notas->parent_id = $contactid; // id of the contact to be linked

    That means the contact must exist before you create the note and set the parent link.

    Harald Kuske

    Harald Kuske
    Principal Solution Architect – Professional Services, EMEA
    hkuske@sugarcrm.com
    SugarCRM Deutschland GmbH

  • Did you try the logic_hooks for the events after_relationship_add resp. after_relationship_delete?

    They are called on each change of a relationship by both modules, an excellent place to add relation related customizations. You find the parts of the relationship in the 3rd paramter array of the logic_hook function.

    Harald Kuske
    Principal Solution Architect – Professional Services, EMEA
    hkuske@sugarcrm.com
    SugarCRM Deutschland GmbH