function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mattatrenetmattatrenet 

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

BritishBoyinDCBritishBoyinDC

I'd try changing this line:

contentVersion.FirstPublishLocationId = library;

 

to 

 

contentVersion.FirstPublishLocationId = library.id;

mattatrenetmattatrenet

Thanks for the suggestion, BritishBoyinDC, but no go. I still get the same error

 

--------------------------------------------------------------------------------

Expression cannot be assigned

--------------------------------------------------------------------------------

 

Larry LeonidasLarry Leonidas

You need to first create a WorkSpaceDoc and insert, then you can insert the ContentVersion with FirstPublishLocation being the library id.

Arun KumarArun Kumar
Hi Mattatrenet, 
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.