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
saad mechiche alamisaad mechiche alami 

Error when insterting contentversion in a unit test

hello , 

I am currently runing a test where i need to query the contentversion object , in my test i have added the following lines : 

ContentVersion cv = new ContentVersion(contentdocumentid='069L00000000xEI');
		insert cv;

however , when i run my test i get the following error : 

User-added image

Could you please help , many thanks
  
saad mechiche alamisaad mechiche alami
the error message says: 

SystemDmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference
Abhi_TripathiAbhi_Tripathi
Hey Saad,

you cannot insert record like this, in your test class.

I assume you are new to test Classes
Read this Post : http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html

R
egards,
Abhi Tripathi || Salesforce Certified Developer
Join My Blog : http://abhithetechknight.blogspot.in/
saad mechiche alamisaad mechiche alami
Hello , 

Thanks for you answer , and yes i am new to test classes , but i didn't know exactly what to do when i went to your tutorial , could you please elaborate ?
Thanks for your help :)
Abhi_TripathiAbhi_Tripathi
Saad,

In my tutorial shows how to write the test class, in perfect manner.

You just need to create a dummy/ test record of the Object ContentVersion, for this go to the Object get the Required fields name, and those who satisfiy the condition in your class.

Like this one below , inserting Account object record in test class

//Insert Account
Account account = new Account( Name = 'Test', Website = 'CoudIsFuture', Phone=987654321);
insert account;

then you need to call you Apex Class controller, when you call that, test class will run the your controller class, with Testing Data. 

Regards,
Abhi



saad mechiche alamisaad mechiche alami
Have a look at this test 

@IsTest public with sharing class termofusecontrollerTest 

{
   @IsTest(SeeAllData = true)
   
   public static void test_term_of_use_accepted(){
    Profile profile = [select id from profile where name = 'CTD EMEA - Administrator' ]; 
    
    User user = new User(alias = 'usertst1', email='usertst1' + '@xyz.com', 
		emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US', 
		localesidkey='en_US', profileid = profile.Id , CT_CTD_Company__c='Spain' ,
		Terms_of_use_accepted__c=true,timezonesidkey='Europe/London',
		username='user1' + '@xyz.com');
		insert user;
		Terms_of_Use__c Terms = new Terms_of_Use__c(Spain__c='1234',Server__c='c.cs8');
		insert Terms;
		
		ContentVersion cv = new ContentVersion(contentdocumentid='1234');
		insert cv;
		
		system.runAs(user){
		termOfUseController touc = new termOfUseController();
		
	    
		touc.init();
		
		
		
   }
   
   }
   
}

When I run the test , i get the following error message: 

FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: SetupOwnerId duplicates value on record with id: 00DL00000029G1m:


Abhi_TripathiAbhi_Tripathi
Hey Saad,

This is how its done

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;


And for user, you dont need to insert user, just use this UserInfo.getUserId();  method, this will get you the Id of current logged in User.

For more detail go for this link : https://developer.salesforce.com/forums/ForumsMain?id=906F00000008lcnIAA

I
f this answer helps you out, mark it as the best answer for the help of others.

Regards,
Abhi Tripathi || Salesforce Developer
Join My Blog : http://abhithetechknight.blogspot.in/