Skip to main content
Solved

How to attach a file programmatically when opening Email Activity window


Forum|alt.badge.img+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?

 

Best answer by andriikravetskyi35

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
        }
    }

 

View original
Did this topic help you find an answer to your question?

6 replies

andriikravetskyi35
Jr Varsity I
Forum|alt.badge.img+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
        }
    }

 


Forum|alt.badge.img+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");


andriikravetskyi35
Jr Varsity I
Forum|alt.badge.img+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.


Forum|alt.badge.img+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");

                }

 

 


andriikravetskyi35
Jr Varsity I
Forum|alt.badge.img+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();

 


Forum|alt.badge.img+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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings