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

Parents
  • 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
  • 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

Reply
  • 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

Children
No Data