I have followed this to create a content version from vf page using sforce ajax and it is working fine.

To link the uploaded content to one of my custom object I added the below code;

var result1 = sforce.connection.retrieve("id,ContentDocumentId", "ContentVersion", [results[0].id]);
var contentDocumentLink          = new sforce.SObject('ContentDocumentLink');
contentDocumentLink.ContentDocumentId = result1[0].ContentDocumentId;
contentDocumentLink.LinkedEntityId ='a0F2800000D7cXF';
contentDocumentLink.ShareType = 'I';
results = sforce.connection.create([contentDocumentLink]);

But every time it throws exception.


2---{errors:{fields:'LinkedEntityId', message:'You cannot create a link for this type of entity through the api: Linked Entity ID', statusCode:'FIELD_INTEGRITY_EXCEPTION', }, id:null, success:'false', }

Is there a way to create ContentDocumentLink from javascript?

1 Answer 1


Please find my below code: -

   <apex:page >
 <script type="text/javascript">
        var __sfdcSessionId = '{!GETSESSIONID()}';
    <script src="/soap/ajax/33.0/connection.js" type="text/javascript"></script>
    <script src="/soap/ajax/33.0/apex.js" type="text/javascript"></script>
<script type="text/javascript">
    function CreatecontentDocumentLink()
            var contentDocumentLink = new sforce.SObject('ContentDocumentLink');//Initialize the contentDocumentLink
            contentDocumentLink.ContentDocumentId = '06990000001zDCnAAM'; //Document Id
            contentDocumentLink.LinkedEntityId ='00690000002FBX2';//Linked Object Id , in My case it is opportunity record Id
            contentDocumentLink.ShareType = 'V'; //Required. The permission granted to the user of the shared file in a library. This is determined by the 
                                                    permission the user already has in the library. This field is available in API version 25.0 and later.
                                                    Viewer premission. The user can explicitly view but not edit the shared file.
                                                    Collaborator permission. The user can explicitly view and edit the shared file.
                                                    Inferred permission. The user’s permission is determined by the related record. For shares with a library, this is defined by the permissions the user has in that library.
            results = sforce.connection.create([contentDocumentLink]);
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons location="bottom">
<apex:commandLink styleClass="btn" style="text-decoration:none;padding:4px;" value="New Case" id="clink" onclick="CreatecontentDocumentLink(); return false;"/>

The code is running perfectly. Just check below things : - 1. version of API 2. then check share Type. 3. After that the permission on object record.

  • Thank you. I guess the problem was with the js versions. I made it same as the page version, it started working. Apr 25, 2017 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.