Tuesday, 3 October 2017

Setting "Print Management" through x++ AX 2012 R3

Some times, if you want to set the "Print Management" for many customer will be a tedious job. In that case you may use the below code and make your life easy. 
The code goes as below. 

///
/// Job to set the PrintManagement 
/// Mallik on 04/10/2017
///
static void  MK_Set_PrintManagement1(Args _args)
{
 
  PrintMgmtSettings             printMgtSettings;
  PrintMgmtDocInstance          printMgtDocInstance;
  SRSPrintDestinationSettings   printDestinationSettings;
  PrintMgmtReportFormat         PrintMgmtReportFormat;
  PrintMgmtDocumentType         PrintMgmtDocumentType;
  PrintMgmtDocInstanceType      PrintMgmtDocInstanceType;
  NoYes                         NoYes;
  SRSReportFileFormat           SRSReportFileFormat;
  SRSPrintMediumType            SRSPrintMediumType;
  str                                  EmailTo;
  CustTable                     lCustTable;
  LogisticsElectronicAddress    elecAddress;
  container                     record;
  int                               totalRecords;
  container                     printerSetting = conNull();
  try
  {
      while select lCustTable where lCustTable.AccountNum == 'C5002'
      {
          totalRecords = totalRecords + 1;
          select firstOnly elecAddress
            where elecAddress.Location == DirPartyLocation::findOrCreate(lCustTable.Party, 0).Location
            && elecAddress.Type == LogisticsElectronicAddressMethodType::Email; 
          if (elecAddress.RecId)
          {
              EmailTo                       =  elecAddress.Locator;
          }

          printDestinationSettings =   new SRSPrintDestinationSettings(printerSetting);
          printDestinationSettings.unpack(printerSetting);
          printDestinationSettings.caption("@SYS131685");
          printDestinationSettings.emailTo(EmailTo);
          printDestinationSettings.printMediumType(SRSPrintMediumType::BTDPA_Process);
          printDestinationSettings.emailSubject('Invoice');
          printDestinationSettings.emailAttachmentFileFormat(SRSReportFileFormat::PDF);
          printDestinationSettings.numberOfCopies(1);
          printMgtDocInstance=PrintMgmtDocInstance::find(lCustTable.RecId, lCustTable.TableId, PrintMgmtNodeType::CustTable,1 ,1);
          ttsBegin;
          
          if (printMgtDocInstance)
          {
              printMgtDocInstance.selectForUpdate(true);
          }
          printMgtDocInstance.Name           = "AutoDelivery";
          printMgtDocInstance.DocumentType   = 1;
          printMgtDocInstance.PrintType      = PrintMgmtDocInstanceType::Original;
          printMgtDocInstance.PriorityId     = 1;
          printMgtDocInstance.Suppress       = NoYes::No;
          printMgtDocInstance.ReferencedTableId  =  lCustTable.TableId;
          printMgtDocInstance.ReferencedRecId    =  lCustTable.RecId;
          printMgtDocInstance.NodeType           =  PrintMgmtNodeType::CustTable;
          
          if (printMgtDocInstance)
                  printMgtDocInstance.update();
          else
                  printMgtDocInstance.insert();
          select firstOnly printMgtSettings order by PriorityID where printMgtSettings.ParentId==printMgtDocInstance.RecId && printMgtSettings.PriorityID==1;
          if (printMgtSettings)
          {
              printMgtSettings.selectForUpdate(true);
          }
          printMgtSettings.ParentId           = printMgtDocInstance.RecId;
          printMgtSettings.ReportFormat       = PrintMgmtReportFormat::findByDescription(1,'SalesInvoice.Report').RecId;
          printMgtSettings.PrintJobSettings   = printDestinationSettings.pack();
          printMgtSettings.NumberOfCopies     = 1;
          printMgtSettings.PriorityId         = 1;
          if (printMgtSettings)
                  printMgtSettings.update();
          else
                  printMgtSettings.insert();
          ttsCommit;
          if(printMgtDocInstance && printMgtSettings)
          {
              info(strFmt("Total recoreds updated: %1",totalRecords));
          }
          else
          {
              error(strFmt("Total recoreds updated: %1",totalRecords));
          }
      }
  }
  catch(Exception::Error)
  {
      Throw(Exception::Error);
  }
  info(strFmt("Total Read Records = %1",totalRecords));
}


Import and Export file from BLOB storage Account(Azure) in D365 F&O using X++

  Import and Export file from BLOB storage Account in D365 F&O using X++ Import: /// <summary> /// MKInventQualityOrderLineService...