creating documents under case via REST api

HI all,
i am facing a problem here , when i try creating documents under case via REST api , documents get created but not under Cases. Documents and Cases have many-many relationship with each other and the link field to pass while creating documents under case is "case_id".


{"session":"cc7kjujeogmm1pkpua2pesv4l3",
"module_name":"Documents",
"name_value_list":[
{"name":"document_name","value":"document 2"},
{"name":"revision","value":"1"},
{"name":"case_id","value":"d9220283-2fcc-ce12-0e10-550fa302d010"}
]}
the above is the JSON body request for creating documents under case , when i hit it documents get created but not under case whose id is provided and when i search for error log it says "undefined Index: case_id not found".
  • Hi Abhishek, 

    I have struggled in the past with a similar request, where I was able to work around it by creating three different calls within the same file. The first call, creates the Document. The second call, will establish the relationship between the Case and the Document, and the third call will attach the file in the Document Record. 

    Here is a sample of the code I used at that point in time: 


    $url = $base_url . "/Documents";

    $doc_arguments = array(
        "name" => "Test Doc",
    );

    $doc_response = call($url, $oauth2_token_response->access_token, 'POST', $doc_arguments);

    echo "<pre>";
    print_r($doc_response);
    echo "</pre>";

    //Upload File - POST /<module>/:id/file/:field
    $url1 = $base_url . "/Documents/{$doc_response->id}/link/cases/{your-case-id-here}";
    $url2 = $base_url . "/Documents/{$doc_response->id}/file/filename";

    echo "url: ".$url1."\n";

    $file_arguments = array(
        "delete_if_fails" => true,
        "oauth_token" => $oauth2_token_response->access_token,
         'filename' => "/1.txt",
    );

    $relate_response = call($url1, $oauth2_token_response->access_token, 'POST');
    $file_response = call($url2, $oauth2_token_response->access_token, 'POST', $file_arguments, false);

    echo "<pre>";
    print_r($file_response);
    print_r($relate_response);
    echo "</pre>";

    Hopefully this helps.

    Kind Regards,
    Nicole Xirakia
    SugarCRM Support

    Learning Resources: http://support.sugarcrm.com | http://university.sugarcrm.com | http://community.sugarcrm.com