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
Prady01Prady01 

Test class on content object for trigger based on contentversion

Hello People, Thanks in advance for any help i could get... I am writing a test class for a trigger on content object and when i insert a new record on content version and then try to set the custom field on that object but i get an error saying FIELD_INTEGRITY_EXCEPTION, You cannot set custom fields or tags on a document published into a personal library. Fields set: Opportunity: [], plz any help would be greatly appreciated, thanks...

 

 

@istest

private class ProjectName_test {
    
    static testMethod void testProjectName(){
    
             Employee__c e1 = new Employee__c(First_Name__c = 'test11', Last_Name__c = 'test11', Start_Date__c = Date.valueOf('2009-10-05'));
            insert e1;
    
    
        Profile p = [select id from profile where name='System Administrator'];
        User u = new User(alias = 'standt', email='standarduser1@testorg.com',
                        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
                        localesidkey='en_US', profileid = p.Id,SFDC_EmployeeId__c = e1.id,
                        timezonesidkey='America/Los_Angeles', username='standarduser1@testorg.com');
    
    
        account a = new account();
        a.name='Test name';
        insert a;
        
      Project__c dp = new Project__c();
        insert dp;
        
        Opportunity op = new Opportunity();
         op.Accountid = a.id;
         op.Name = 'Swarm Test Opp';
         op.StageName = 'Different Stage';
         op.CloseDate = Date.newInstance(2010, 03, 26);
         op.Amount = 555;
         op.Project__c = dp.id;
         insert op;
        
        
           
        ContentVersion cv = new ContentVersion();
          
       
        
        
        //cv.FirstPublishLocationId='068Q00000006bsz';
        cv.title='Test title';
       
        cv.VersionData=blob.valueof('New Bitmap Image.bmp');
         cv.Account__c = a.id;
     
        cv.Opportunity__c=op.id;
       // cv.RecordtypeId='012400000009Bj8AAE';
     // cv.RecordtypeId='058400000004CY3';//sales documents record type id
     //  cv.RecordtypeId ='00h40000000zGVa';
        insert cv;
       
        ContentVersion cov = [select id,Project__c,Opportunity__c,RecordTypeId from ContentVersion where id=:cv.id];
        cov.Opportunity__c = op.id;
        
     
       update cov;
        system.assertequals(cov.DreamTeam_Project__c,op.DreamTeam_Project__c);
       
        
        }
        
 }

 

Best Answer chosen by Admin (Salesforce Developers) 
Prady01Prady01

This will help anyone if they having probles with content object... All thanks to EchoEcho

public ID setupContent(){ 
RecordType ContentRT = [select Id FROM RecordType WHERE Name='Sales Documents'];
 ContentVersion testContentInsert =newContentVersion(); 
 testContentInsert.ContentURL='http://www.google.com/'; 
 testContentInsert.Title ='Google.com'; 
 testContentInsert.RecordTypeId = ContentRT.Id; 
 insert testContentInsert; 
 ContentVersion testContent = [SELECT ContentDocumentId FROM ContentVersion where Id = :testContentInsert.Id]; 
 ContentWorkspace testWorkspace = [SELECT Id FROM ContentWorkspace WHERE Name='Opportunity Documents ']; 
 ContentWorkspaceDoc newWorkspaceDoc =newContentWorkspaceDoc(); 
 newWorkspaceDoc.ContentWorkspaceId = testWorkspace.Id; 
 newWorkspaceDoc.ContentDocumentId = testContent.ContentDocumentId; 
 insert newWorkspaceDoc;
 testContent.iPad_Content_Views__c =0; 
 update testContent;
 return testContentInsert.Id;