0

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]);
console.log('1---'+result1[0].ContentDocumentId);
var contentDocumentLink          = new sforce.SObject('ContentDocumentLink');
contentDocumentLink.ContentDocumentId = result1[0].ContentDocumentId;
contentDocumentLink.LinkedEntityId ='a0F2800000D7cXF';
contentDocumentLink.ShareType = 'I';
results = sforce.connection.create([contentDocumentLink]);
console.log('2---'+results);

But every time it throws exception.

Log:

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?

2

Please find my below code: -

   <apex:page >
 <script type="text/javascript">
        var __sfdcSessionId = '{!GETSESSIONID()}';
    </script>
    <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.
                                                    V
                                                    Viewer premission. The user can explicitly view but not edit the shared file.
                                                    C
                                                    Collaborator permission. The user can explicitly view and edit the shared file.
                                                    I
                                                    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]);
            alert('results---------'+results);
    }              
</script>
<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;"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

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.

| improve this answer | |
  • Thank you. I guess the problem was with the js versions. I made it same as the page version, it started working. – Saumya Ranjan Satapathy Apr 25 '17 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.