Relationship between Account and Opportunity via API 4

Hi,
I’m having trouble creating a relationship between account and opportunity via API. The request response always returns failed.
sent parameters:

array(7) {
   ["session"]=>
   string(26) "ic9eeo3d45j2qot91r6j5biv3p"
   ["module_name"]=>
   string(8) "Accounts"
   ["module_id"]=>
   string(36) "6da29182-bbf6-ed8b-828d-64e5014d3af1"
   ["link_field_name"]=>
   string(22) "accounts_opportunities"
   ["related_id"]=>
   string(36) "91a44b43-4a56-a624-3ff1-661028836526"
   ["name_value_list"]=>
   string(0) ""
   ["deleted"]=>
   string(1) "0"
}
return:
object(stdClass)#53 (3) {
   ["created"]=>
   int(0)
   ["failed"]=>
   int(1)
   ["deleted"]=>
   int(0)
}

All parameters have already been reviewed a few times. Account id exists, opportunity id exists. The method used is set_relationship.
I noticed this problem in other default relationships, but I managed to get around it by creating a custom relationship like it. However, the opportunity account is mandatory.

Thanks.
Fábio E Pereira
fabio@ideiamais.com.br

Hi @ideiamais

Isn’t the Accounts-Opportunities a many to many relationship, right? :bulb: Or do you have any custom changes to the relationship? :confused:

Relationship definition from metadata/accounts_opportunitiesMetaData.php

$dictionary['accounts_opportunities'] = array(
  'table' => 'accounts_opportunities',
  'fields' => [........],
  'indices' => [.........],
  'relationships' =>
  array(
    'accounts_opportunities' =>
    array(
      'lhs_module' => 'Accounts',
      'lhs_table' => 'accounts',
      'lhs_key' => 'id',
      'rhs_module' => 'Opportunities',
      'rhs_table' => 'opportunities',
      'rhs_key' => 'id',
      'relationship_type' => 'many-to-many',
      'join_table' => 'accounts_opportunities',
      'join_key_lhs' => 'account_id',
      'join_key_rhs' => 'opportunity_id',
    ),
  ),
);

For many to many relationships you need to call the set_relationship from the V4 API.

V4 API set_relationship details from /service/v4_1/rest.php

    /**
     * Set a single relationship between two beans.  The items are related by module name and id.
     *
     * @param String $session -- Session ID returned by a previous call to login.
     * @param String $module_name -- name of the module that the primary record is from.  This name should be the name the module was developed under (changing a tab name is studio does not affect the name that should be passed into this method)..
     * @param String $module_id - The ID of the bean in the specified module_name
     * @param String link_field_name -- name of the link field which relates to the other module for which the relationship needs to be generated.
     * @param array related_ids -- array of related record ids for which relationships needs to be generated
     * @param array $name_value_list -- The keys of the array are the SugarBean attributes, the values of the array are the values the attributes should have.
     * @param integer $delete -- Optional, if the value 0 or nothing is passed then it will add the relationship for related_ids and if 1 is passed, it will delete this relationship for related_ids
     * @return Array - created - integer - How many relationships has been created
     *               - failed - integer - How many relationsip creation failed
     * 				 - deleted - integer - How many relationships were deleted
     * @exception 'SoapFault' -- The SOAP error, if any
     */
    Method [  public method set_relationship ] {
      

      - Parameters [7] {
        Parameter #0 [  $session ]
        Parameter #1 [  $module_name ]
        Parameter #2 [  $module_id ]
        Parameter #3 [  $link_field_name ]
        Parameter #4 [  $related_ids ]
        Parameter #5 [  $name_value_list ]
        Parameter #6 [  $delete ]
      }
    }

So, to add the relationship, you need to do something like:

$parameters = [
    'session' => $session_id,
    'module_name' => 'Accounts',
    'module_id' => $account_id,
    'link_field_name' => 'opportunities',
    'relate_ids' => [$idOportunities],
    'name_value_list' => [],
    'delete' => 0
];

$set_entry_result_account = call("set_relationship", $parameters, $url);

Hope this helps solve the issue.