How to update the documents_account table from REST api

When upload document for certain account, the documents_accounts table stores both account id and document id. When upload the file from REST api, need to store the account_id and document_id in document_accounts table, but i can't store those values.(Want to relate the accounts and documents)

here is my sample code

$set_entry_parameters = array(

        "session" => $_SESSION['id'],

        "module_name" => "documents_accounts",

        "name_value_list" => array(

        array("name" => "document_id", "value" => $some_id),

        ),

        );

      

        //var_dump($set_entry_parameters);

        $set_entry_result = call("set_entry", $set_entry_parameters, $url);

  • To set a relationship in the legacy web services you must use a set_relationship call as described in

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_6.5/Application_Framework/Web_Services/M…

    set_entry can only be used for new entries in a module.

    Harald Kuske
    Principal Solution Architect – Professional Services, EMEA
    hkuske@sugarcrm.com
    SugarCRM Deutschland GmbH

  • Thanks for your reply.

    tried like this

    $relationshipProductBundleQuoteParams = array(

            'session' => $_SESSION['id'],

            'module_name' => 'Documents',// and also use Accounts

            'module_id' => $set_entry_result->id,

            'link_field_name' => 'document_id',

            'related_ids' => array(

               $set_entry_result->id,

            ),

            'name_value_list' => array('account_id'=>'1')

        );

           $relationshipProductBundleQuoteResult = call('set_relationship', $relationshipProductBundleQuoteParams, $url);

    but shows error like:

    stdClass Object

    (

      [created] => 0

      [failed] => 1

      [deleted] => 0

    )

  • Try this piece of code (change acount_id and document_id to values in your environment):

    <?php

    $sugarUrl = 'http://localhost/sugarce65/service/v4_1/rest.php';

    $user = 'admin';

    $pass = 'admin';

    $loginParams = array(

      'user_auth' => array(

        'user_name' => $user,

        'password' => md5($pass),

        ),

      'application_name' => 'MY APP',

      'name_value_list' => array(),

    );

    $loginResult = rest_call($sugarUrl,'login',$loginParams);

    $sessionID = $loginResult->id;

    echo "SESSION ID=".$sessionID."<br>";

    $docid = 'b301090f-c88a-edc0-da03-56852dbee4cf';

    $accId = '620602ff-f6b7-6a05-99b9-5491650b3b18';

    $setParams = array(

      'session' => $sessionID,

      'module_name' => 'Documents',

      'module_id' => $docid,

      'link_filed_name' => 'accounts',

      'related_ids' => array($accId),

      'name_value_list' => array(),

      'delete' => 0,

    );

    $linkResult = rest_call($sugarUrl,'set_relationship',$setParams);

    if ($linkResult->created) echo "LINK created<br>";

    if ($linkResult->deleted) echo "LINK deleted<br>";

    if ($linkResult->failed)  echo "LINK failed<br>";

    echo("done");

    function rest_call($sugarUrl,$method,$params)

    {

      $curl = curl_init($sugarUrl);

      curl_setopt($curl, CURLOPT_POST, true);

      curl_setopt($curl, CURLOPT_HEADER, false);

      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $Json = json_encode($params);

        $PostArgs = array(

        'method' => $method,

        'input_type' => 'JSON',

        'response_type' => 'JSON',

        'rest_data' => $Json,

        );

       

        curl_setopt($curl, CURLOPT_POSTFIELDS, $PostArgs);

        $Response = curl_exec($curl);

        curl_close($curl);

        $Result = json_decode($Response);

      return $Result;

    }

    ?>

    Harald Kuske
    Principal Solution Architect – Professional Services, EMEA
    hkuske@sugarcrm.com
    SugarCRM Deutschland GmbH

  • You try to set a relationship between Documents and Documents.

    If $set_entry_result->id is the id of the document, you must tell the REST service that the relationship name is "accounts" and add the id of the account, not again the id of the document.

    Harald Kuske
    Principal Solution Architect – Professional Services, EMEA
    hkuske@sugarcrm.com
    SugarCRM Deutschland GmbH