How to link document to opportunity API REST

Version 7.11.13
Sugar Version 6.5.25 (Build 344)
API v4

Hello, I want to relate an existing document in the crm with an existing opportunity too, here I have my example of how I am trying, but it does not work.

$set_entry_parameters_document = array(
                "session" => $session_id,
                "module_name" => "Documents",
                "name_value_list" => array('id' => $document_id, 'opportunity_id' => $idOportunidad
                ),
            );
            $set_entry_result_document = call("set_entry", $set_entry_parameters_document, $url);

Could someone tell me where I went wrong or what is the correct solution.

Hi @oAnbin

I think that the Documents-Opportunities is a many to many relationship, right? Or do you have any custom changes to the relationship?

Relationship definition from documents_opportunitiesMetaData.php
$dictionary["documents_opportunities"] = array(
  'true_relationship_type' => 'many-to-many',
  'relationships' =>
  array(
    'documents_opportunities' =>
    array(
      'lhs_module' => 'Documents',
      'lhs_table' => 'documents',
      'lhs_key' => 'id',
      'rhs_module' => 'Opportunities',
      'rhs_table' => 'opportunities',
      'rhs_key' => 'id',
      'relationship_type' => 'many-to-many',
      'join_table' => 'documents_opportunities',
      'join_key_lhs' => 'document_id',
      'join_key_rhs' => 'opportunity_id',
    ),
  ),
  'table' => 'documents_opportunities',

For many to many relationships you need to use 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' => 'Documents',
    'id' => $document_id,
    'link_field_name' => 'opportunities',
    'relate_ids' => [$idOportunidad],
    'name_value_list' => [],
    'delete' => 0
];

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

Hope this helps

2 Likes