Thursday, January 7, 2010

SO confirmation: Add email with pdf attachment to batch processing

Requirement:

  1. During sales order confirmation, if the print management settings is email and format is PDF, the system uses the client outlook to send the PDF as mail attachment. Instead, it is required to add this PDF file to the Email batch process (similar to the alerts) in SysOutgoingEmailTable & SysOutgoingEmailData tables so that when the Batch processing is performed, the email will be sent. The email template will be setup in parameters and the email id should be picked from SO.

  2. A new E-mail template shall be created according to standard functionality. This template is then set as a new parameter the form “CustFormletterParameters” named “E-mail template”.

Solution:

In the class SalesFormLetter_Confirmation, create a new method setConfirmEmailId() and call this in printJournal()

void setConfirmEmailId()
{
salesTable salesTablelocal
container conSysEmail;
boolean ok = true;
salesTablelocal = salesTable::find(custConfirmJour.SalesId);

//container: parameters 1 -> to identify if called from sales
// parameter 2 -> to get the sales email id

conSysEmail = conins(conSysEmail,1,NoYes::Yes,salesTablelocal.Email);
//set the container value in the info class. A new get/set method to store the container.
infolog.parmSOPOConfirmEmail(conSysEmail);
}

public void printJournal()
{
if (journalList.len() > 0)
{
if (printFormletter)
{
this.setConfirmEmailId();
custConfirmJour.printJournal(this, journalList);
}
if (salesParmUpdate.Proforma)
this.removeProforma();
}
}

In the Info class add the following code to reportSendMail() method to send mail using batch for pdf instead of outlook client.
void reportSendMail(PrintJobSettings p1)
{

    boolean pdfFormat;
    Map mappings;
    SysEmailParameters emailParams;
    Filename filePath;
    Filename filenamelocal;
    Filename fileExtension;
    FilenameOpen attachmentFilename;
    str attachmentsFolder;
    SysEmailId emailId;
    SysEmailTable table;
    LanguageId languageId;
    NoYes callerSales;
    int i;
    SysINetMail m = new SysINetMail();
    str fileName = 'axaptareport';
    ;

    if (p1.format() == PrintFormat::ASCII p1.format() == PrintFormat::TEXTUTF8)
        fileName = fileName + '.txt';
    else if (p1.format() == PrintFormat::RTF)
        fileName = fileName + '.rtf';
    else if (p1.format() == PrintFormat::HTML)
        fileName = fileName + '.htm';
    else if (p1.format() == PrintFormat::PDF p1.format() == PrintFormat::PDF_EMBED_FONTS)
    {
        fileName = fileName + '.pdf';
        pdfFormat = true;
    }
    //check if this is called from SO confirmation
    //conSysEmail = infolog.parmSOPOConfirmEmail();
    
if (conlen(conSysEmail)>1 && pdfFormat)
    {
        //The file name will be in the local client temp folder.
        attachmentFilename = p1.fileName();
        [filePath, filenamelocal, fileExtension] = Global::fileNameSplit(attachmentFilename);
        attachmentsFolder = Global::strReplace(emailParams.AttachmentsPath,'//','/');
        if (substr(attachmentsFolder, strlen(attachmentsFolder), 1) != '\\')
        attachmentsFolder = attachmentsFolder + "\\";
        if (attachmentsFolder)
        {
            //Copy the file to the server folder which should be shared and should
            //be on the network. This folder should be setup in the Email parameters.
            
attachmentsFolder = attachmentsFolder + filenamelocal + ".pdf";
            info::copyFile(attachmentFilename,attachmentsFolder);
            callerSales = conpeek(conSysEmail,1);
            if (callerSales == NoYes::Yes)
                emailId = CustFormletterParameters::find().CAP_ConfirmationEmailTemplate;
            else
                emailId = VendFormletterParameters::find().CAP_ConfirmationEmailTemplate;
            table = SysEmailTable::find(emailId);
            if (table)
            {
                languageId = table.DefaultLanguage;
                mappings = new Map(Types::String, Types::String);
                //mappings.insert("SubscriptionId","S001");
                
for (i = 2; i <= conlen(conSysEmail); i = i + 2)
                    SysEmailTable::sendMail(table.EmailId,languageId,conpeek(conSysEmail,i),mappings,attachmentsFolder,"",true,curuserid());
            }
            else
                    infolog.add(Exception::Error,"Email template parameter not available");
        }
        else
            infolog.add(Exception::Error,"Attachment folder for sending email is not setup in the parameters");
        }
    else
        m.sendMailAttach(p1.mailTo(),p1.mailCc(), p1.mailSubject(),'axapta report', true, p1.fileName(), fileName);
}
---------------------------

container CAP_parmSOPOConfirmEmail(container _conSysEmail = conSysEmail)
{
    conSysEmail = _conSysEmail;
    return conSysEmail;
}
---------------------------
server static void copyFile(FilenameOpen _attachmentFilename,
str _attachmentsFolder)
{
    Set permissionSet;
    ;
    // Revert permissions
    CodeAccessPermission::revertAssert();

    permissionSet = new Set(Types::Class);
    permissionSet.add(new FileIoPermission(_attachmentFilename,'r'));
    permissionSet.add(new FileIoPermission(_attachmentsFolder,'w'));

    CodeAccessPermission::assertMultiple(permissionSet);
    
// Move to processed folder
    // BP Deviation Documented

    winAPI::copyFile(_attachmentFilename,_attachmentsFolder,true);
    CodeAccessPermission::revertAssert();
}
---------------------------