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
Aidel BruckAidel Bruck 

Insert Contentversion - The argument is null or invalid

This is my trigger And the test class
When running the test class I get 100% coverage yet the test fails and I get the following error:

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_ARGUMENT_TYPE, The argument is null or invalid.: []

Trigger

trigger NewFileAlert on ContentDocument (after insert) 
{
// Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = 
  new List<Messaging.SingleEmailMessage>();
  
  for (ContentDocument newDoc : Trigger.new) 
  {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = 
      new Messaging.SingleEmailMessage();
    
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(list of all addresses)
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setReplyTo('myaddress');
      mail.setSenderDisplayName('Aidel Bruck');

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('New Document Added');
      String body = 'Please note: A new document has been added/n  ';
      body += 'File name: '+ newdoc.title+'/n' ;
      body += 'Created By: '+ newdoc.createdbyid+ '/n';
      body += 'Created Date: '+ newdoc.CreatedDate+ '/n';
      body += 'link to file: '+ System.URL.getSalesforceBaseUrl().getHost()+newdoc.id;
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
}

Test 
@isTest
public class TestNewFileAlert 
{

    public static testmethod void MyUnitTest()
    {
        test.startTest();
    Account acct = new Account(Name='TEST_ACCT');
    insert acct;
    ContentVersion contentVersionobj = new ContentVersion();
    contentVersionobj.ContentURL='http://www.google.com/';
    contentVersionobj.Title = 'Google.com';            
    insert contentVersionobj;
    
        test.stopTest();
    }

}

 
Raj VakatiRaj Vakati
Try this
 
@isTest
public class TestNewFileAlert 
{
    
    public static testmethod void MyUnitTest()
    {
      //  test.startTest();
        
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        UserRole r = [SELECT Id FROM UserRole WHERE Name='COO'];
        User futureUser = new User(firstname = 'Future', lastname = 'User',
                                   alias = 'future', defaultgroupnotificationfrequency = 'N',
                                   digestfrequency = 'N', email = 'test@test.org',
                                   emailencodingkey = 'UTF-8', languagelocalekey='en_US', 
                                   localesidkey='en_US', profileid = p.Id, 
                                   timezonesidkey = 'America/Los_Angeles',
                                   username = 'futureasdasdasuser@test.org',
                                   userpermissionsmarketinguser = false,
                                   userpermissionsofflineuser = false, userroleid = r.Id);
        insert(futureUser);
        System.runAs(futureUser){
            
            Account acct = new Account(Name='TEST_ACCT');
            insert acct;
            
            ContentVersion contentVersion = new ContentVersion(
                Title = 'Penguins',
                PathOnClient = 'Penguins.jpg',
                VersionData = Blob.valueOf('Test Content'),
                IsMajorVersion = true
            );
            insert contentVersion; 
            Test.setCreatedDate(contentVersion.Id, DateTime.now());
            
            List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
            
            //create ContentDocumentLink  record 
            ContentDocumentLink cdl = New ContentDocumentLink();
            cdl.LinkedEntityId = acct.id;
            cdl.ContentDocumentId = documents[0].Id;
            cdl.shareType = 'V';
            insert cdl;
            Test.setCreatedDate(cdl.Id, DateTime.now());
            
          //  test.stopTest();
        }
    }
    
}

 
Krishnaveni A 2Krishnaveni A 2
Hi ,

 
Krishnaveni A 2Krishnaveni A 2
I tried the above code, I didnt get any errors. Can u please explain in detail 
Raj VakatiRaj Vakati
You need to set the Create date in test class .. 

In your code you are refering the create date so ... 


            Test.setCreatedDate(cdl.Id, DateTime.now());

i set like above and i set ruunAs users also .. its 100% code covergae 
Aidel BruckAidel Bruck
I ran the new test code and I am still getting the error!