You need to sign in to do that
Don't have an account?
ExploreForce
CONTENTDOCUMENTLINK: FIELD_INTEGRITY_EXCEPTION, You cannot create a link for a document in a personal library: [ContentDocumentId]
Hi,
I have class for which I am facing small issue to cover my test class:
//My controller class where I am creating ContentDocumntLink
ContentDocumentLink newFileShare = new ContentDocumentLink();
newFileShare.ContentDocumentId = listsfa[0].Secured_Files__r.Content_Document_Id__c;
newFileShare.LinkedEntityId = fileSt.User__c;
if(fileSt.Requested_Access_Type__c == 'Read')
newFileShare.ShareType= 'V';
else if(fileSt.Requested_Access_Type__c == 'Read/Write')
newFileShare.ShareType= 'C';
listContentShares.add(newFileShare);
insert listContentShares;
***************************************************************************************
//TEST CLASS Coverage code
string before = 'Testing base 64 encode';
Blob beforeblob = Blob.valueOf(before);
//Insert contentdocument data
ContentVersion cv = new ContentVersion();
cv.title = 'test content trigger';
cv.PathOnClient ='test';
cv.VersionData =beforeblob;
insert cv;
ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];
ContentDocumentLink newFileShare = new ContentDocumentLink();
newFileShare.contentdocumentid = testcontent.contentdocumentid;
newFileShare.LinkedEntityId = fileSt.User__c;
newFileShare.ShareType= 'V';
insert newFileShare;
In the first attemp I tried with the above SQL query and I m getting below error:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, You cannot create a link for a document in a personal library: [ContentDocumentId]
To resolve the above error: I have come up with solution ,
ContentWorkspace testWorkspace = [SELECT Id FROM ContentWorkspace limit 1];
ContentWorkspaceDoc newWorkspaceDoc =new ContentWorkspaceDoc();
newWorkspaceDoc.ContentWorkspaceId = testWorkspace.Id;
newWorkspaceDoc.ContentDocumentId = testContent.ContentDocumentId;
insert newWorkspaceDoc;
It's throwing error:
System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary.: [ContentWorkspaceId]
Please help!!
I have class for which I am facing small issue to cover my test class:
//My controller class where I am creating ContentDocumntLink
ContentDocumentLink newFileShare = new ContentDocumentLink();
newFileShare.ContentDocumentId = listsfa[0].Secured_Files__r.Content_Document_Id__c;
newFileShare.LinkedEntityId = fileSt.User__c;
if(fileSt.Requested_Access_Type__c == 'Read')
newFileShare.ShareType= 'V';
else if(fileSt.Requested_Access_Type__c == 'Read/Write')
newFileShare.ShareType= 'C';
listContentShares.add(newFileShare);
insert listContentShares;
***************************************************************************************
//TEST CLASS Coverage code
string before = 'Testing base 64 encode';
Blob beforeblob = Blob.valueOf(before);
//Insert contentdocument data
ContentVersion cv = new ContentVersion();
cv.title = 'test content trigger';
cv.PathOnClient ='test';
cv.VersionData =beforeblob;
insert cv;
ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];
ContentDocumentLink newFileShare = new ContentDocumentLink();
newFileShare.contentdocumentid = testcontent.contentdocumentid;
newFileShare.LinkedEntityId = fileSt.User__c;
newFileShare.ShareType= 'V';
insert newFileShare;
In the first attemp I tried with the above SQL query and I m getting below error:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, You cannot create a link for a document in a personal library: [ContentDocumentId]
To resolve the above error: I have come up with solution ,
ContentWorkspace testWorkspace = [SELECT Id FROM ContentWorkspace limit 1];
ContentWorkspaceDoc newWorkspaceDoc =new ContentWorkspaceDoc();
newWorkspaceDoc.ContentWorkspaceId = testWorkspace.Id;
newWorkspaceDoc.ContentDocumentId = testContent.ContentDocumentId;
insert newWorkspaceDoc;
It's throwing error:
System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary.: [ContentWorkspaceId]
Please help!!
Solution:
string before = 'Testing base 64 encode';
Blob beforeblob = Blob.valueOf(before);
//Insert contentdocument data
ContentVersion cv = new ContentVersion();
cv.title = 'test content trigger';
cv.PathOnClient ='test';
cv.VersionData =beforeblob;
insert cv;
ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];
FeedItem feeditem = new FeedItem();
feedItem.Type = 'ContentPost';
feedItem.Body = 'Place holder file. Upload attachmenet using Upload new version.';
feedItem.ContentData = beforeblob;
feedItem.ContentFileName = 'Test File';
feedItem.ParentId = sfa.id;
insert feedItem;
List<ContentDocumentLink > ctd = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :sfa.Id];
if(ctd.size() > 0)
newSecFile.Content_Document_Id__c = ctd[0].ContentDocumentId;
update newSecFile;
//insert new ContentDocumentLink
ContentDocumentLink newFileShare = new ContentDocumentLink();
newFileShare.contentdocumentid = testcontent.contentdocumentid;
newFileShare.LinkedEntityId = fileSt.User__c;
newFileShare.ShareType= 'V';
All Answers
Another thing make sure you have the access to Conten Workspace which you are trying to query.
Hope this helps !!
I have set @IsTest(SeeAllaData=true) in my test class.
I will check whether I have access to Content Workspace.
Thanks.
Solution:
string before = 'Testing base 64 encode';
Blob beforeblob = Blob.valueOf(before);
//Insert contentdocument data
ContentVersion cv = new ContentVersion();
cv.title = 'test content trigger';
cv.PathOnClient ='test';
cv.VersionData =beforeblob;
insert cv;
ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];
FeedItem feeditem = new FeedItem();
feedItem.Type = 'ContentPost';
feedItem.Body = 'Place holder file. Upload attachmenet using Upload new version.';
feedItem.ContentData = beforeblob;
feedItem.ContentFileName = 'Test File';
feedItem.ParentId = sfa.id;
insert feedItem;
List<ContentDocumentLink > ctd = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :sfa.Id];
if(ctd.size() > 0)
newSecFile.Content_Document_Id__c = ctd[0].ContentDocumentId;
update newSecFile;
//insert new ContentDocumentLink
ContentDocumentLink newFileShare = new ContentDocumentLink();
newFileShare.contentdocumentid = testcontent.contentdocumentid;
newFileShare.LinkedEntityId = fileSt.User__c;
newFileShare.ShareType= 'V';
@IsTest
public class TestAttachmentHandler
{
static testMethod void validateHelloWorld()
{
Master_Rate_Deck_XRef__c availableRateDeck = new Master_Rate_Deck_XRef__c(Deck_Type__c = 'Wholesale', RateDeckType__c = 'Std.', Billing_Term_Rate_Type__c = 'LRN');
insert availableRateDeck;
Blob beforeblob=Blob.valueOf('Unit Test Attachment Body');
ContentVersion cv = new ContentVersion();
cv.title = 'test content trigger';
cv.PathOnClient ='test';
cv.VersionData =beforeblob;
insert cv;
ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];
ContentDocumentLink contentlink=new ContentDocumentLink();
contentlink.LinkedEntityId=availableRateDeck.id;
contentlink.ShareType= 'C';
contentlink.LinkedEntityId = availableRateDeck.Id;
contentlink.ContentDocumentId=testcontent.ContentDocumentId;
contentlink.Visibility = 'AllUsers';
insert contentlink
}
}
Error:System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_OR_READONLY, You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary.: []