0

I'm practicing an apex unit test and getting an insert fail on ContentDocumentId and expecting a reconstruction.
Any idea of this?

enter image description here

@isTest
public static void testUpdateResponse() {

  ContentVersion cv = new ContentVersion();
  cv.VersionData = EncodingUtil.base64Decode('iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII=');
  cv.Title = 'testCV1';
  cv.PathOnClient = 'testCV1';
  cv.IsMajorVersion = false;
  insert cv;

  ContentDocumentLink cdl = new ContentDocumentLink(); // HERE
  cdl.ContentDocumentId = cv.ContentDocumentId;
  insert cdl;

  account b = new account();
  b.Name = 'myAccount2';
  insert b;

  saveBase64_File testA = new saveBase64_File();

  Map<String, Object> inputMap = new Map<String, Object>();
  inputMap.put('base64', 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII=');
  inputMap.put('fileName', 'user_image_1');
  inputMap.put('accountId', b.id);
  inputMap.put('contentDocumentId', cdl.id);
  Map<String, Object> outputMap = new Map<String, Object>();

  testA.invokeMethod('updateImage', inputMap, outputMap, new Map<String, Object>());
}

1 Answer 1

2

This is a common problem

 ContentDocumentLink cdl = new ContentDocumentLink(); // HERE
 cdl.ContentDocumentId = cv.ContentDocumentId;
 insert cdl;

The cv.ContentDocumentId has to be queried for after you insert the ContentVersion.

insert cv;
cv = [SELECT ContentDocumentId FROM ContentVersion
                               WHERE Id = :cv.Id][0];
.. insert ContentDocumentLink here
1
  • Thanks @cropredy!
    – Ian Lee
    Jul 13 at 16:56

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.