Rest Api Relate Account to Contact

Hi,
Strange issue… I can’t relate contact to account with this code :


if ($id_account  != 0 ) {
            $set_relationships_parameters = array(
               "session" =>  $session_id,
               "module_name" => 'Accounts',
               "module_id" =>  $id_account,
               "link_field_name" => 'contacts',
               "related_ids" =>  array($id_contact) ,
            );
    
        $set_relationships_result = call("set_relationship", $set_relationships_parameters, $url);

}   

Is there some person have the good “code” for relate account to contact ?

Regards

Hi,

Just set account_id when you “set_entry” the contact.
The relationship will be created automatically.

1 Like

The answer blqt gave is the most practical.

But I also have this code that works to relate and Account to a Contact:


            $account = BeanFactory::getBean('Accounts')->retrieve_by_string_fields('someAccountName');
            $person->load_relationship('accounts');
            $person->accounts->add($account->id);

$person is a Contact bean.

Hi pgr,
your code is for “not for api”… logic-hook or other. I use that for a special form and it’s work.

My code is for api rest… v4,1

Hi blqt,

so ?


            $set_entry_parameters = array(
                 "session" => $session_id,
                 "module_name" => "Contacts",
                 "account_id" => $id_account,
                 "name_value_list" => array(

Regards

Oh I see now, you’re right. I never programmed for the API so I didn’t recognize the difference, sorry.

I was wondering how you were asking that question, I was pretty sure you knew how to do that better than me… :slight_smile:

No. That should be in name_value_list like your other contacts fields.

Benoit

1 Like

Hi Benoit,

really sometimes we look hard way and not the easy way.

This must be available on all module… this easy way to relate.

Thanks.

No, it’s only true for account_id of Contacts.
For other relationships, you need to use set_relationship.

Benoit