You need to sign in to do that
Don't have an account?
Create ContentVersion with custom field for test method
I have custom fields on the ContentVersion object and I need to test querying ContentVersion based on these custom fields in my Apex test method.
I've tried creating a new ContentVersion in my test method:
--------------------------------------------------------------------------------
ContentVersion contentVersionObj = newContentVersion();
contentVersionObj.ContentURL = 'http://www.google.com';
contentVersionObj.title = 'Google';
contentVersionObj.Enablement_Area__c = 'Acct Mgmt';
insert contentVersionObj;
--------------------------------------------------------------------------------
However, when I try to test that, I got an error:
--------------------------------------------------------------------------------
15:55:36.756 (756244000)|EXCEPTION_THROWN|[136]|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, You cannot set custom fields or tags on a document published into a personal library. Fields set: Enablement Area: []
15:55:36.757 (757794000)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, You cannot set custom fields or tags on a document published into a personal library. Fields set: Enablement Area: []
--------------------------------------------------------------------------------
Ok, so based on the ContentVersion docs (http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_contentversion.htm) I need to assign a value to FirstPublishedLocationId in order to have the ContentVersion not be assigned to a personal library.
--------------------------------------------------------------------------------
ContentWorkspace library = [SELECT id FROMContentWorkspace LIMIT 1];
ContentVersion contentVersionObj = newContentVersion();
contentVersionObj.ContentURL = 'http://www.google.com';
contentVersionObj.title = 'Google';
contentVersionObj.Enablement_Area__c = 'Acct Mgmt';
contentVersion.FirstPublishLocationId = library;
insert contentVersionObj;
--------------------------------------------------------------------------------
Only, when I run that, I get another error:
--------------------------------------------------------------------------------
Expression cannot be assigned
--------------------------------------------------------------------------------
So, I guess I can't assign to the FirstPublishLocationId field...
Is there any way to create a ContentVersion object with a custom field in an Apex test method?
I'm doing this all in a sandbox and using the Force.com IDE if it matters.
Thanks,
Matt
I'd try changing this line:
contentVersion.FirstPublishLocationId = library;
to
contentVersion.FirstPublishLocationId = library.id;
Thanks for the suggestion, BritishBoyinDC, but no go. I still get the same error
--------------------------------------------------------------------------------
Expression cannot be assigned
--------------------------------------------------------------------------------
You need to first create a WorkSpaceDoc and insert, then you can insert the ContentVersion with FirstPublishLocation being the library id.
Give a try to below code:
ContentVersion testContentInsert = new ContentVersion();
testContentInsert.ContentURL='http://www.google.com/';
testContentInsert.Title = 'Google.com';
testContentInsert.RecordTypeId = ContentRT.Id;
testContentInsert.Account__c = acc.Id;
testContentInsert.Opportunity__c = opp.Id;
insert testContentInsert;
ContentVersion testContent = [SELECT ContentDocumentId FROM ContentVersion where Id = :testContentInsert.Id];
ContentWorkspaceDoc newWorkspaceDoc = new ContentWorkspaceDoc();
newWorkspaceDoc.ContentWorkspaceId = library.Id;
newWorkspaceDoc.ContentDocumentId = testContent.ContentDocumentId;
insert newWorkspaceDoc;
It will automatically updated the Library on the ContentVersion record.