• Greer Banks
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
I am trying to build a test class and want to create/insert a Knowledge Article record. Per the Lightning Knowledge Limitations doc, in lightning you can no longer insert articles using the Article Type field. Is there any other way to insert? I can create a kav record, but its knowledgearticleid is null. I cannot insert a knowledge article itself, nor write to the kav.knowledgearticleid
Hello,

As part of a test method, I need to create an EmailMesage (on a Case) with some Attachments.

When I performing a manual test on the UI, the functionality works fine, i.e. I simply go to a Case record and from the "Emails" Related List, I add an EmailMessage with an Attachment. During such manual process, these are the steps:
1) Adding a new Email on the Emails Related List of the Case;
2) Populate some required fields (Subject)on the email form the "To" field;
3) click the button "Attach File" > click button "Choose File" > Click button "Attach To Email" > Click button "Done".
(the Attachment is now attached to the email, even BEFORE sending that email (I guess the email record with its attachment are in a caché until the email is actually sent;
Surpringly, the EmailMessage has its standard checkbox "HasAttachment" = TRUE, even before commintting the EmailMessage record to the database.
4)  Clic button "Send" (on Email Message).

This manuall process triggers the population of a custom checkbox field "Hidden_Has_Attachment__c" on the Case object.

The problem is when coding the Apex TEST CLASS:

On the Test Class, I create the List of Cases, then the List of EmailMessages and finaly a List of Attachments.

The Attachment is associated to its parent EmailMessage through its field ParentId.

I need to create, in the Test Class, the EmailMessages WITH Attachments. How can I do this?

The TEST CLASS creates records in the following sequence:
1) Creates Case;
2) Creates EmailMessages (without Attachment)
3) Creates Attachments

The problem is: I cannot create EmailMessages WITH Attachments and insert these EmailMessages with Attachments so as to get the trigger to trigger (which would help the apex test class to pass).
The test method in the test class needs to insert EmailMessages that will enter the following if statement:
for (EmailMessage em : trigger.new){
      if (em.HasAttachment == TRUE &&
          em.ParentId.getSobjectType() == Case.SobjectType){
               emailIds.add(em.Id);
               }

When the test is done manually on the Salesforce classic UI, the Email Messages enters the if statement but when the apex test method runs, the EmailMessage is NOT entering the if statement because em.HasAttachment is NOT true.



 

Hello,

 

I have some custom code written that is used to 'help' email to case.  Basically, what happens is that if an email is received for an already "Closed" case (via email-to-case) a trigger on the EmailMessage object does some evaluations and then creates a NEW child case and then creates a clone of the original EmailMessage and relates it to the new case.  I would simply just change the case the original emailMessage is related to, but we don't have access to change the ParentID field on the emailmessage record.

 

My issue is in getting the ATTACHMENTS to the orignal email and relating them to the new email.  When I query in my class for the attachments it returns 0 rows when testing and I KNOW there is an attachment on the email (the original emailMessage shows an attachment as well).  Is this because I am triggering on insert of the EmailMessage record, meaning that the attachment records have not yet been inserted?

 

If that IS the case is there any way I can get these attachments and related them to the correct cloned email at their insertion?  I just don't see how I will be able to hold the ID(s) needed in memory between the two separate DML operations that are happening (insertion of the original Email and then insertion of the Attachments)

 

Any help/ideas would be appreciated.