set_relationship returns 500 in v7.11. Is backward compatibility not supported?

My use case is simple. I'm creating a Contact, I'm creating a Case and I associate that Case with the contact. When we initially built the integration, only api v4_1 was available.

When use API Key of SugarCRM v7.11, Contact creation and Case creation are succeeding. However, when the method `set_relationship` is invoked, the SugarCRM server is returning a 500.

The same code works for v6.5

How do you suggest we alter the integration to support all versions of SugarCRM for our use case?

  • Hi Arjun Mayilvaganan,

    I use this endpoint: 

    /<module>/:record/link POST

    This document describes the approach I use successfully.

    I hope this helps!

  • Hi Patrick,

    Thank you for responding. But, the page you've linked to pertains to the REST API v11. We're using v4.1 and are a little hesitant to upgrade to v10-v11 at the moment since only this particular call is failing.

    So, the method we're using is set_relationship.
    Our sample request with input_type JSON is:

    {
      "session": <sessionId>,
      "module": "Contacts",
      "module_name": "Contacts",
      "module_id": <contactId>,
      "link_field_name": "cases",
      "related_ids": [
        <caseId>
      ],
      "delete": 0
    }


    PS: There are two properties above `module` and `module_name`, because without either of those I'm getting a 500.
    If both are provided, I'm getting a 200 with the following response:

    {"created":0,"failed":1,"deleted":0}

    when response_type is JSON.

    Everything seems to be working fine for SugarCRM release 6.x.
    Issue is for the latest release v7.11

  • Hi Arjun Mayilvaganan,

    SugarCRM last released new code (not counting defect fixes) for v4.1 at the end of 2012. All new development efforts in the years since have focused on v10 and v11. I recommend making the switch.

    That being said, this v4.1 endpoint worked fine for me on a SugarCRM-hosted 7.11.1.0 Enterprise instance on PHP 7.1.14

    I used these two documents as guides:

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.11/Integration/Web_Services/v1_-_v4.1/… 

    and

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.11/Integration/Web_Services/v1_-_v4.1/… 

    Here is my code:

    <?php
    $url = "https://<instance_url>/service/v4_1/rest.php";
    $username = "<username>";
    $password = "<password>";
    //function to make cURL request
    function call($method, $parameters, $url)
    {
    ob_start();
    $curl_request = curl_init();
    curl_setopt($curl_request, CURLOPT_URL, $url);
    curl_setopt($curl_request, CURLOPT_POST, 1);
    curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($curl_request, CURLOPT_HEADER, 1);
    curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
    $jsonEncodedData = json_encode($parameters);
    $post = array(
    "method" => $method,
    "input_type" => "JSON",
    "response_type" => "JSON",
    "rest_data" => $jsonEncodedData
    );
    curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
    $result = curl_exec($curl_request);
    curl_close($curl_request);
    $result = explode("\r\n\r\n", $result, 2);
    $response = json_decode($result[1]);
    ob_end_flush();
    return $response;
    }
    //login ----------------------------------------------
    $login_parameters = array(
    "user_auth" => array(
    "user_name" => $username,
    "password" => md5($password),
    "version" => "1"
    ),
    "application_name" => "RestTest",
    "name_value_list" => array(),
    );
    $login_result = call("login", $login_parameters, $url);
    /*
    echo "<pre>";
    print_r($login_result);
    echo "</pre>";
    */
    //get session id
    $session_id = $login_result->id;
    //relate --------------------------------------------
    $relationshipParams = array(
    //session id
    'session' => $session_id,
    //The name of the module.
    'module_name' => 'Contacts',
    //The ID of the specified module bean.
    'module_id' => '<contacts.id>',
    //The relationship name of the linked field from which to relate records.
    'link_field_name' => 'cases',
    //The list of record ids to relate
    'related_ids' => array(
    '<cases.id>',
    ),
    //Sets the value for relationship based fields
    'name_value_list' => array(
    array(
    //'name' => 'contact_role',
    //'value' => 'Other'
    )
    ),
    //Whether or not to delete the relationship. 0:create, 1:delete
    'delete'=> 0,
    );
    // set the relationship
    $relationshipResult = call('set_relationship', $relationshipParams, $url);
    echo "Create Relationship Result<br />";
    echo "<pre>";
    print_r($relationshipResult);
    echo "</pre>";
  • Thank you so much Patrick McQueen! That was helpful. Got it working.

    Patrick McQueen wrote:

    Here is my code:

    'name_value_list' => array(
    array(
    //'name' => 'contact_role',
    //'value' => 'Other'
    )
    )

    If possible, can you please help understand the purpose of input of the above value?


    The documentation says:

    An array specifying relationship fields to populate. An example of this is contact_role between Opportunities and Contacts.

    But, I still don't get it.

    Also,

    SugarCRM last released new code (not counting defect fixes) for v4.1 at the end of 2012. All new development efforts in the years since have focused on v10 and v11. I recommend making the switch.

    Like I mentioned. We're maintaining a SugarCRM integration for HappyFox Chat. Our Customers are using SugarCRM releases 6.x and 7.x. I'm not able imagine how making a switch to v11 will help. Are you suggesting to use both v4.1 and v11 in that case. Because, I'm think I can't use v11 to talk to Release 6.x.

  • Hi Arjun Mayilvaganan,

    The relationship between Contacts and Opportunities has a unique field value representing the Contact Role. Therefore, the set_relationship Endpoint has this as a parameter.

    Is the version of PHP the instance is hosted on 7.1.x? You can download the phpinfo() file from Admin > Diagnostic Tool in the instance to confirm this.

    SugarCRM has documented defect #80605, which states that the v41 API endpoints without all parameters will result in a 500 fatal error in instances hosted on PHP 7.1.x. The defect is viewable in the support portal for anyone with access.

    I hope this helps.