Solved

How to attach a file programmatically when opening Email Activity window

  • 21 February 2023
  • 6 replies
  • 372 views

Userlevel 4
Badge +1

How can I attach a file programmatically when opening Email Activity window using  

throw new PXRedirectRequiredException(graph, true, "Email") { Mode = PXBaseRedirectException.WindowMode.NewWindow };

I was able to set From , To, CC, BCC & Body by using graph.Message.Current . Is there any way to attach a file when opening this Email window?

 

icon

Best answer by andriikravetskyi35 21 February 2023, 10:52

View original

6 replies

Userlevel 5
Badge +1

Hi, 

it is very usual question, here is short example, Acumatica saves information about attachment in one table, and file as BIN array in another table, investigate UploadFileMaintenance graph in Acumatica’s source code and you will find tables.

Example:

  public class CREmailActivityMaintExt : PXGraphExtension<CREmailActivityMaint>
{
public void TEST()
{
byte[] allFilesInByte = new byte[1000]; // here must be your file in BIN data

FileInfo oneFileInfo = new FileInfo(Guid.NewGuid(), "OneFile.txt", null, allFilesInByte);
UploadFileMaintenance fileUploadGraph = PXGraph.CreateInstance<UploadFileMaintenance>();

fileUploadGraph.SaveFile(oneFileInfo, FileExistsAction.CreateVersion);
PXNoteAttribute.AttachFile(Base.Message.Cache, Base.Message.Current, oneFileInfo);
fileUploadGraph.Persist();
this.Base.Persist();
throw new PXRedirectToFileException(oneFileInfo.UID, false); // it is redirection to file attachments screen
}
}

 

Userlevel 4
Badge +1

@andriikravetskyi35 Hi, This opens Email activity. But I got nothing attached to Files when trying this code snippet. Any idea?

CREmailActivityMaint emailGraph = PXGraph.CreateInstance<CREmailActivityMaint>();   

PXNoteAttribute.AttachFile(emailGraph.Message.Cache, emailGraph.Message.Current, uploadFileInfo);

throw new PXRedirectRequiredException(emailGraph, "Email");

Userlevel 5
Badge +1

your object uploadFileInfo must have information about guid number, file name and file data as array in BIN format.

If you don’t have this information and invoke methods, then your attachment will be empty.

emailGraph.SaveFile(uploadFileInfo, FileExistsAction.CreateVersion); // this method persist file in DB

PXNoteAttribute.AttachFile(emailGraph.Message.Cache, emailGraph.Message.Current, uploadFileInfo); // this method attach file to needed document

you need invoke two methods and also attach debugger to code and check all iteration step by step.

Userlevel 4
Badge +1

@andriikravetskyi35  Yes, uploadFileInfo has the information about guid number, file name and file data as array in BIN format. This is the entire code snippet that I tried. But the emailGraph.Message.Current is null. Can it be the issue?


                UploadFileMaintenance uploadGraph = PXGraph.CreateInstance<UploadFileMaintenance>();

                if (uploadGraph.SaveFile(uploadFileInfo, FileExistsAction.CreateVersion))
                {
                    uploadGraph.Persist();

                    //Attach file to email activity
                    CREmailActivityMaint emailGraph = PXGraph.CreateInstance<CREmailActivityMaint>();

                    PXNoteAttribute.AttachFile(emailGraph.Message.Cache, emailGraph.Message.Current, uploadFileInfo);

                    emailGraph.Persist();

                    throw new PXRedirectRequiredException(emailGraph, "Email");

                }

 

 

Userlevel 5
Badge +1

try change emailGraph.Message to emailGraph.SMMail in code and this:

CRSMEmail email = new CRSMEmail();

email.Subject = "Test Email";

email.MailTo = "example@example.com";

email.Body = "This is a test email created through code.";

this.Base.Email.Current = this.Base.Email.Insert(email);

this.Base.Actions.PressSave();

 

Userlevel 4
Badge +1

@andriikravetskyi35 Thanks. You saved my day.  However I used the code as this.

CRSMEmail email = new CRSMEmail();

email.Subject = "Test Email";

email.MailTo = "example@example.com";

email.Body = "This is a test email created through code.";

emailGraph.Message.Current = emailGraph.Message.Insert(email);



PXNoteAttribute.AttachFile(emailGraph.Message.Cache, emailGraph.Message.Current, uploadFileInfo);
emailGraph.Persist();

 

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved