Search code examples
sugarcrm

SugarCRM Rest API set_relationship between Contacts and Documents


I am trying to (link/set_relationship) between a document and a contact on SugarCRM. I am not sure how to construct the "name_value_list" specifically for this. At least that is what I believe to be wrong.

I have tried the following:

1.

'name_value_list': []

2.

'name_value_list' : [{
            'name': "documents_contacts",
            'value': 'Other',
                    }],

3.

'name_value_list': [{'table': "%s_%s" % (ModuleName, LinkedModuleName)},
                                {'fields': [
                                        {"id": str(uuid.uuid1())},
                                        {"date_modified": str(datetime.datetime.now())},
                                        {"deleted":  '0'},
                                        {"document_id": RecordID},
                                        {"contact_id": LinkedRecordID},
                                            ]

4.

'name_value_list':[{"%s_%s" % (ModuleName, LinkedModuleName): 'Other',
                                "id": str(uuid.uuid1()),
                                "date_modified": str(datetime.datetime.now()),
                                "deleted":  '0',
                                "document_id": RecordID,
                                "contact_id": LinkedRecordID
                                }]

SugarCRM CE Version 6.5.20 (Build 1001)

SugarCRM v4_1 Rest API Documentation:

* 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 ]
      }
    }

Python 3.7


def SetRelationship(self, ModuleName, ModuleID, LinkFieldName, RelatedID):
    method = 'set_relationship'
    data = {
        'session':self.SessionID,
        'module_name':ModuleName,
        'module_id':ModuleID,
        'link_field_name':LinkFieldName,
        'related_ids':[RelatedID, ]
            }
    response = json.loads(self.request(method, data))

SetRelationship('Documents', 'e9d22076-02fe-d95d-1abb-5d572e65dd46', 'Contacts', '2cdc28d8-763e-6232-2788-57f4e19a9ea0')

Result: {'created': 0, 'failed': 1, 'deleted': 0}

Expected Result: {'created': 1, 'failed': 0, 'deleted': 0}


Solution

  • You probably meant to call

    SetRelationship('Documents', 'e9d22076-02fe-d95d-1abb-5d572e65dd46', 'contacts', '2cdc28d8-763e-6232-2788-57f4e19a9ea0')
    

    Notice the lowercase contacts here, as the API expects the name of the link field in Documents, not the name of the module.

    If that still doesn't fix the issue, check the sugarcrm.log and the php log for errors.