0

I'm querying records of an object called "ContentVersionHistory" in my code.

In my test method, I tried to create some test records of this object but I received error "DML operation INSERT not allowed on ContentVersionHistory".

I checked the document: https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_contentversionhistory.htm

It says: Use this read-only object to query the history of a document version.

Then how can I test my code?

global class CE_GetMp4Attendees implements Schedulable {
global void execute(SchedulableContext sc) {    
    //Get the existing training session Names,IDs
    Map<String,CE_Training_Session__c> MapCETrainingSessions=new Map<String,CE_Training_Session__c>();
    List<CE_Training_Session__c> lsttrainingsThisQuarter = [select Id,Conf_Name__c from CE_Training_Session__c where Start_Time__c>:CE_Utilities.quarterStart];
    For(CE_Training_Session__c cts : lsttrainingsThisQuarter){
        MapCETrainingSessions.put(cts.Conf_Name__c,cts);
    }
    //key: CE Training sessionId
    //value: a set of userId
    Map<String,Set<String>> MapSessionUsers = new Map<String,Set<String>>();
    For(CE_Attendee_Infor__c cai : [select Attendee__c,CE_Training_Session__c from CE_Attendee_Infor__c where CE_Training_Session__c in :lsttrainingsThisQuarter ])
    {
        if(MapSessionUsers.containsKey(cai.CE_Training_Session__c)){
            MapSessionUsers.get(cai.CE_Training_Session__c).add(cai.Attendee__c);
        }
        else{
            MapSessionUsers.put(cai.CE_Training_Session__c,new Set<String>{cai.Attendee__c});
        }
    }
    //create a Map of contentversionId to CE training session Id
    //Get the contentversion Ids
    List<ContentVersion> lstContentVersions = [select Id,Title from ContentVersion where createddate > LAST_FISCAL_QUARTER and Title like 'CE Sales%'];
    Map<String,String> MapcontentversionCESession = new Map<String,String>();
    For(ContentVersion cv : lstContentVersions){

        String TruncatedTitle = cv.Title;
        if(cv.Title.indexOf('-')!=-1)
        {
            TruncatedTitle = cv.Title.substringBefore('-');
        }

        if(MapCETrainingSessions.containsKey(TruncatedTitle))
        {
            MapcontentversionCESession.put(cv.Id, MapCETrainingSessions.get(TruncatedTitle).Id);
        }
    }
    System.debug(MapcontentversionCESession);
    //get all the Mp4 attendees
    //Key: ID  Value:  Key: Email, Value: CE_Attendee_Infor__c
    Map<String,Map<String,CE_Attendee_Infor__c>> MapSessionMapAttendees = new Map<String,Map<String,CE_Attendee_Infor__c>>();
    For(String cvId : MapcontentversionCESession.keySet())
    {
        MapSessionMapAttendees.put(MapcontentversionCESession.get(cvId),new Map<String,CE_Attendee_Infor__c>());
        List<ContentVersionHistory> lstContentVersionHistory = [select CreatedById,CreatedBy.Name,CreatedBy.Email from ContentVersionHistory where Field = 'contentVersionDownloaded' and ContentVersionId =: cvId];
        if(lstContentVersionHistory.size()>0){
            For(ContentVersionHistory cvh: lstContentVersionHistory){
                CE_Attendee_Infor__c info = new CE_Attendee_Infor__c(Attendee_Email__c = cvh.CreatedBy.Email,Attendee__c=cvh.CreatedById,Type_of_Attendence__c='Mp4',CE_Training_Session__c=MapcontentversionCESession.get(cvId));
                if(MapSessionUsers.containsKey(info.CE_Training_Session__c)&&!MapSessionUsers.get(info.CE_Training_Session__c).contains(info.Attendee__c)&&!MapSessionMapAttendees.get(info.CE_Training_Session__c).containsKey(info.Attendee_Email__c)) 
                {
                    MapSessionMapAttendees.get(info.CE_Training_Session__c).put(info.Attendee_Email__c,info);
                }
            }
        }
    }
    For(Map<String,CE_Attendee_Infor__c> mapAttendess : MapSessionMapAttendees.values()){
        insert(mapAttendess.values());
    }
}
}

This is the code I want to test:

List<ContentVersion> lstContentVersions = [select Id,Title from ContentVersion where createddate > LAST_FISCAL_QUARTER and Title like 'CE Sales%'];
For(String cvId : MapcontentversionCESession.keySet())
    {
List<ContentVersionHistory> lstContentVersionHistory = [select CreatedById,CreatedBy.Name,CreatedBy.Email from ContentVersionHistory where Field = 'contentVersionDownloaded' and ContentVersionId =: cvId];
}
1

I've encountered this scenario in one of my tasks. What I did is move up the levels and what I mean by that is finding what dependency the ContentVersionHistory has to other objects.

I find out that all you have to do in your test is to create and insert a FeedItem with the ContentData populate with a blob. Salesforce will automatically populate the ContentDocument, ContentVersion and ContentDocumentHistory (if you will do an update in your test class of the file you've uploaded).

To find out how they relate to each other, I used workbench and inserted a file through chatter and then I queried for various salesforce ids. I've also found a diagram of the content objects.

Hope it helps

| improve this answer | |
  • In my case, which object should I use to set the parentId for the FeedItem? – sfdcnoob Aug 4 '15 at 22:46
  • It depends on what object you have the custom code. If you have it for example on account, you need to create an accountfeed record, I believe the same applies for contact and case. If you have a custom you need to set the parentid to the of the record you previously inserted in the test. Have a look at standard objects with feed developer.salesforce.com/docs/atlas.en-us.api.meta/api/… – Mihai Neagoe Aug 4 '15 at 22:49
  • I'm not sure if my code has anything to do with Chatter. – sfdcnoob Aug 4 '15 at 22:53
  • Edit your question and share some code if you want, we might help you better – Mihai Neagoe Aug 4 '15 at 22:55
  • Maihi, I updated the question, thank you for your help. – sfdcnoob Aug 4 '15 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.