Thursday 5 February 2015

AX 2012 default Print management settings using X++ job to Email?

First get the RECID of the Report for which you want to update the print management settings. Just go to table : PrintMgmtReportFormat and select the report for which you want to import settings. Next get the RecID and replace it in the job below. In my case the RecId is 5637144843. But you need to replace that with your respective RecID.














Next prepare your CSV file, it should look like the following:


Finally use the following job to get the desired settings. Please update the subject as desired. You can also import the email subject from the file but that would require a developer to make the required changes in the job.


static void SOInvoice_Settings(Args _args)
{
#File
    CommaTextIo        commaTextIo;
    FileIOPermission   permission;
    container          containFromRead;
    Query query;
    int                x;
    int                cols;
    int rowNum;

    str Customer,Email;
    int Done,UnDone;

    CustTable custTAble;
    SRSPrintDestinationSettings     destination;
    PrintMgmtSettings               settings1;
    PrintMgmtDocInstance            instance;
    SysDictEnum dictEnum;
    int         enumValue;

    permission = new FileIOPermission(@'C:\Users\Bilal\Desktop\CustEmail.csv',#io_read);


    permission.assert();

    commaTextIo = new CommaTextIO(@'C:\Users\Bilal\Desktop\CustEmail.csv','R');

   // Enter your file path here.

    rowNum = 0;
    Done= 0;
    UnDone= 0;

    containFromRead = commaTextIo.read();

    dictEnum = new SysDictEnum(enumNum(LogisticsElectronicAddressMethodType));

    While(containFromRead)
    {
        if(rowNum>0)
        {
            Customer= any2str(conpeek(containFromRead,1));
            Email= any2str(conpeek(containFromRead,2));

            custTable =CustTable::find(Customer);

    if(custTable)
        {
            destination = new SRSPrintDestinationSettings();
            destination.emailTo(Email);
            destination.printMediumType(SRSPrintMediumType::Email);
            destination.numberOfCopies(1);
            destination.emailSubject("Sales Invoice"); // enter email subject here
            destination.emailAttachmentFileFormat(SRSReportFileFormat::PDF);

            ttsBegin;
            instance = PrintMgmtDocInstance::find(CustTable.RecId, CustTable.TableId, PrintMgmtNodeType::CustTable, PrintMgmtDocumentType::SalesOrderInvoice, 1);

            if(instance)
                instance.selectForUpdate(true);

            instance.DocumentType = PrintMgmtDocumentType::SalesOrderInvoice;
            instance.NodeType = PrintMgmtNodeType::CustTable;
            instance.PrintType = PrintMgmtDocInstanceType::Original;
            instance.PriorityId = 1;
            instance.ReferencedTableId = CustTable.TableId;
            instance.Suppress = NoYes::Yes;
            instance.ReferencedRecId =custTable.RecId ;

            if(instance)
                instance.update();
            else
                instance.insert();

            select firstOnly settings1 order by PriorityId desc where settings1.ParentId == instance.RecId ;

            query = new Query(queryStr(CustInvoiceJour));
            Query.dataSourceTable(tableNum(CustInvoiceJour)).addRange(fieldNum(CustInvoiceJour, InvoiceAccount)).value(CustTable.AccountNum);
            Query.pack();

            settings1.ParentId = instance.RecId;
            settings1.ReportFormat = 5637144843; // enter the recid of the email format here.
            settings1.PrintJobSettings = destination.pack();
            settings1.PriorityId = settings1.PriorityId + 1;
            settings1.NumberOfCopies = 1;
            settings1.Description = strFmt('%1  %2', CustTable.AccountNum, CustTable.name());
            settings1.QueryPacked = Query.pack();
            settings1.insert();

            ttsCommit;
            Done++;
        }
        else
        {
            info("Customer not found " + Customer);
            UnDone++;
        }

        }
        containFromRead = commaTextIo.read();
        rowNum++;
    }
    commaTextIo = null;

    info(strFmt("Inserted : %1 Not inserted: %2", Done, UnDone));
}