Thursday, December 15, 2022

Sending Email using X++ Code in D365 F&O/ AX 7 in HTML fomat

 I was just researching on the emailing capabilities in D365 and found that SysINetMail, SysMailer and some of the smmOutlook classes are deprecated because these classes used predominantly client-side technologies that are no longer available

Below is the quick code snippet that can be used in D365 to send emails. Please note, this is just a untested sample code, with out any setups configured. please ensure all setups are done and improvise the code accordingly.
SysMailerFactory class internally used SysImailer* classes to send emails.

[Form]
public class Irfantest extends FormRun
{
    str                                 header,mainbody,footer;
    str                                     strBody;
    str                                     strFileName,email;
    SysMailerMessageBuilder mail = new SysMailerMessageBuilder();
//send mail to To address    
void sendmailtousers()
    {
        this.htmlbody();
        Email = "";
        Email ="syedirfanReciever@company.com";
        if(Email != "")
        {
       this.sendmail(Email);
        }
    }
// Create a content for body in  HTML format 
    void htmlbody()
    {
        date subdate;
        str comp,event;
        ;
        header = "" ;
        mainbody = "";
        footer = "";
        comp    = "Ahmed Soliman Al Fahhad (Closed Joint Stock)";
        event   = "test ALERTS";
        header = strfmt("<p><strong><font color=#003399 face=Arial>test alert </font></strong> </p></br>"+
        "<p><strong><font color=#003399 face=Tahoma> االتاشيره  </font></strong> </p></br>"+
          "<TABLE cellSpacing=0 cellPadding=1 width='100%' border=3>"+
       "<tr bgColor=gray> "+
       "<td align=center><font  size=2 face=Arial>الرقم التسلسلي</td>"+//sn
       "<td align=center><font  size=2 face=Arial>‏‏رمز الإجازة</td>"+//leave code
       "<td align=center><font  size=2 face=Arial>الموظف</td>"+//employee
        "<td align=center><font  size=2 face=Arial>اسم الموظف</td>"+//employee
       "<td align=center><font  size=2 face=Arial>رقم الاقامة</td>"+//iqama no.
       "<td align=center><font  size=2 face=Tahoma>تاريخ انتهاء الإقامة</td>"+//iqamaexpire date
       "<td align=center><font  size=2 face=Arial>اسم المشروع</td>"+//project name
      
       "</tr>");

        mainbody  += strfmt("<TR ALIGN='CENTER'>"+
                "<TD><font color=#003399 size=2 face=Arial>" +  num2str(1,3,0,0,0) + "</TD>"+
                "<TD><font color=#003399 size=2 face=Arial>" +  "Leave" + "</TD>"+
                "<TD><p><font color=#003399 size=2 face=Arial>" +  "40157" +"</p></TD>"+
                 "<TD><font color=#003399 size=2 face=Tahoma>" +  "syed irfan gaffoor"+"</TD>"+
                "<TD><p><font color=#003399 size=2 face=Arial>" +  "12235455"+ "</p></TD>"+
                 "<TD><p><font color=#003399 size=2 face=Tahoma>" +  "1447/04/28" +"</p></TD>"+
                "<TD><font color=#003399 size=2 face=Arial>" +  "Head office"+"</TD>"+
                 
                "</TR>" );
        footer = strfmt( "</TABLE></br><font color=#cccccc size=2 face=Arial><p>_______________________________________________________________________ </p>"+
       "</font><b><font color=#003399 face=Arial><p>Alert details<font color=#003399 size=2 face=Arial></font></p>"+
       "</font></b><font color=#003399 size=2 face=Arial></font><font color=#cccccc size=2><p>"+
       "<table border= 5 cellSpacing=1 cellPadding=3>"+
       "<tbody>"+
       "<tr> <td><p align=left><b><font  size=2 face=Arial>Date:</font></p></td> <td><p><font face=Arial><font color=#003399 size=2>"+date2str(today(),123,2,4,3,4,4)+"</font></font></p></td></tr>"+
       "<tr> <td><p align=left><b><font size=2><font  face=Arial>Company:</font></font></p></td><td><font face=Arial><font color=#003399><font size=2> "+comp+" </font> </font></font></td></tr>"+
       "<tr> <td><font face=Arial><b><font size=2 face=Arial>Event:</font></font></td><td><font face=Arial><font color=#003399 size=2 face=Arial>"+event+"</font></font></td></tr>"+
       "</tbody> </table>") ;
    }
//call SysMailerMessageBuilder  for send mail
    void sendmail(str to)
    {
        strBody =header+mainbody+footer;
        try
        {
            mail.setBody(strBody, true);
            mail.setSubject("Email Test from D365");
            mail.addTo(to);
            mail.setFrom("sender@company.com");
            //SysMailerFactory::sendInteractive(mail.getMessage()); // with dialog box 
            SysMailerFactory::sendNonInteractive(mail.getMessage());//without diloag box
            
        }
        catch (Exception::Error)
        {
            throw error("@SYS33567");
        }
    }
//form button click event
    [Control("Button")]
    class FormButtonControl1
    {
        public void clicked()
        {
            super();
            element.sendmailtousers();
        }
    }
}

output:






Thursday, March 25, 2021

X++ code to print QR code and Barcode on Report in AX

  • QR code on a report in AX 2009.

Image


In order to do this,

1.Create a new display method on the report. The below code can be used to generate QR code.

display container  qrcode()

{
    Bindata                 bindata = new Bindata();
    Image                   Imgobj;
    System.Drawing.Image    img;
    System.Drawing.Bitmap   obj;
    Filepath                sourcePath;
    container               con;
    InteropPermission interopPerm;
    FileIoPermission _perm;
    Microsoft.Dynamics.QRCode.Encoder encoder;
    System.Exception               ex;
    Set permissionSet;
;
    sourcePath = @"C:\temp\newQRCode.bmp";
    permissionSet = new Set(Types::Class);
    permissionSet.add(new FileIOPermission(sourcePath,'rw'));
    permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
    CodeAccessPermission::assertMultiple(permissionSet);

    encoder   = new Microsoft.Dynamics.QRCode.Encoder();
    obj = new System.Drawing.Bitmap(encoder.Encode("SYED IRFAN QR CODE TEST"));

    obj.Save(sourcePath,System.Drawing.Imaging.ImageFormat::get_Bmp());
    bindata.loadFile(sourcePath);
    con = bindata.getData();
    return con;
}

2.Add the image control on the report based on the display method. 



3.QR code can be seen on the report on running it.





  • Barcode  on a report in AX 2009.




1.Create a new display method on the report. The below code can be used to generate Barcode .


display BarCodeString barCode()
{
    Barcode barcode;
    ;
 
    barcode = Barcode::construct(BarcodeType::Code39);
    barcode.string(true, "SYED IRFAN BARCODE TEST");
    barcode.encode();
 
    return barcode.barcodeStr();
}

2. Set font properties  on the Barcode string control.

                                                                

3.Run the report and send the output to the screen.











Wednesday, October 2, 2019

performFormLookup

public void performFormLookup(FormRun _form)
{
    FormDataSource      formDataSource;
    Query               formquery;
;
    formDataSource  = _form.objectSet();
    formquery       = formDataSource.query();


    formquery.dataSourceNo(1).addRange(fieldnum(ProjTable, ProjGroupId)).value(queryValue(ProjGroupIdFilter.text()));
    super(_form);

}

Thursday, July 4, 2019

Insert Record level security with query value in ax 2012 by code (X++)

static void AXCopyUsergroupRLSBytableWithVlaue(Args _args)
{

    SysRecordLevelSecurity  SysRecordLevelSecurity,insertRLS,copyvalue,updatevalues;
    dialogfield srcdf,destdf;
    dialog dialog;
    userGroupid dest;

    sysQueryRun queryRun;
    Query query = new Query() ;

    QueryRun        rlsqueryRun;
    int             dataSourceNo;
    queryBuildDataSource queryBuildDataSource;
    queryBuildRange queryBuildRange;
    int i;





    ;
    dialog = new dialog("copying user group's Record Level security ");

    destdf = dialog.addField(typeid(userGroupid),"Destination");

    if(dialog.run())
    {
     
        dest = destdf.value();
     
      if( dest !="")
        {
          ttsbegin;
       //insert new table in RLS table to destination user group;       
   
            insertRLS.tabId   = 18174;
            insertRLS.groupId = dest;
            insertRLS.companyId = curext();
            insertRLS.insert();     


          //fetch same query values from another table;

            select * from copyvalue
            where copyvalue.groupId == dest
            && copyvalue.tabId == 624;//example project table

            rlsqueryRun = new QueryRun(copyvalue.Restriction);
            dataSourceNo = rlsqueryRun .query().dataSourceCount();
            queryBuildDataSource          = rlsqueryRun .query().dataSourceNo(dataSourceNo);

               for(i = 1;i <= queryBuildDataSource.rangeCount();i++)
               {
                     queryBuildRange      = queryBuildDataSource.range(i);

                   
               }


          select forupdate  updatevalues  where updatevalues.groupId == dest 
           && updatevalues.tabId == 18174;
            if (updatevalues.Restriction)
            {
                queryRun = new SysQueryRun(updatevalues.Restriction);
            }

            else
            {
                query.addDataSource(updatevalues.TabId);
                queryRun = new SysQueryRun(query);
            }

            queryRun.query().dataSourceTable(updatevalues.tabId).addRange(fieldnum(PAYFDocumentMoveTracking,PROJID)).value(queryBuildRange.value());
            updatevalues.restriction = queryRun.pack();
            updatevalues.doUpdate();

            ttscommit;
            info("User groups RLS has been copied!");
        }
    }
    else
    {
        info("Canceled by user");
    }


}

Tuesday, January 8, 2019

Create weekly periods between two dates











Method:

void GenerateOneYearWeekPeriods()
{
    date d1,d2,d3,d4,wkst,wkend,i,k;
    integer                 lineNum = 0;

    int findlastrecid()
    {

        select * from  FAHWeeklyTimeSheetPeriods order by Recid Desc;
        return FAHWeeklyTimeSheetPeriods.LineNum;


    }






    ;
    d1 = Stratdate.dateValue();
    d2 = Enddate.dateValue();
    lineNum = findlastrecid();


    if(!FAHWeeklyTimeSheetPeriods::exist(d1))
    {
        for(i = d1 ; i <= d2 ; i++)
        {
           if(mthofyr(i) == 1)
           {
               if(dayofwk(i) == 6 || i == d1 )//sat && start of the month
               {
               wkst = i;
               }

               if(dayofwk(i) == 5  || i == endmth(i))//fri && end of the month
               {
               wkend  = i;

                     lineNum++;

                     FAHWeeklyTimeSheetPeriods.LineNum = lineNum;
                     FAHWeeklyTimeSheetPeriods.Weekcode = NumberSeq::newGetNum(ProjParameters::numRefFAHWeekCode(),true).num();
                     FAHWeeklyTimeSheetPeriods.PeriodId = mthofyr(i);
                     FAHWeeklyTimeSheetPeriods.Month = global::dateStartMth(i);
                     FAHWeeklyTimeSheetPeriods.Year  = year(FAHWeeklyTimeSheetPeriods.Month);

                     FAHWeeklyTimeSheetPeriods.StratDate = wkst;
                     FAHWeeklyTimeSheetPeriods.StratDay = dayName(dayofwk(wkst));
                     FAHWeeklyTimeSheetPeriods.EndDate  = wkend;
                     FAHWeeklyTimeSheetPeriods.EndDay   = dayName(dayofwk(wkend));
                     FAHWeeklyTimeSheetPeriods.Status  = WeekPeriodStatus::open;
                     FAHWeeklyTimeSheetPeriods.insert();
               }
            }

            if(mthofyr(k) != mthofyr(i))
            {
             d3 = i ; d4 = endmth(i);

               for( k = d3 ; k <= d4; k++)
               {
                   if(dayofwk(k) == 6 || k == d3 )//sat && start of the month
                   {
                   wkst = k;
                   }

                   if(dayofwk(k) == 5  || k == d4 )//fri && end of the month
                   {
                   wkend  = k;

                     lineNum++;

                     FAHWeeklyTimeSheetPeriods.LineNum = lineNum;
                     FAHWeeklyTimeSheetPeriods.Weekcode = NumberSeq::newGetNum(ProjParameters::numRefFAHWeekCode(),true).num();
                     FAHWeeklyTimeSheetPeriods.PeriodId = mthofyr(i);
                     FAHWeeklyTimeSheetPeriods.Month = global::dateStartMth(i);
                     FAHWeeklyTimeSheetPeriods.Year  = year(FAHWeeklyTimeSheetPeriods.Month);
                     FAHWeeklyTimeSheetPeriods.StratDate = wkst;
                     FAHWeeklyTimeSheetPeriods.StratDay = dayName(dayofwk(wkst));
                     FAHWeeklyTimeSheetPeriods.EndDate  = wkend;
                     FAHWeeklyTimeSheetPeriods.EndDay   = dayName(dayofwk(wkend));
                     FAHWeeklyTimeSheetPeriods.Status  = WeekPeriodStatus::open;
                     FAHWeeklyTimeSheetPeriods.insert();
                   }
               }
              k = i ;
            }
        }
      }

    FAHWeeklyTimeSheetPeriods_ds.executeQuery();

}






Workflow Mass Reassign users

static void AXworkflowMassReassign(Args _args)
{
    userid src,dest;
    name comment;
    WorkflowWorkItemSubject subject;
    WorkflowConfigurationName confignme;
    WorkflowSequenceNumberActive congifId;
    dialogfield srcdf,destdf,comtdf,subjectDf,congifIdDf,confignamedf;
    dialog dialog;
    WorkflowWorkItemTable workitems;
    WorkflowTrackingStatusTable WorkflowTrackingStatusTable;

    WorkflowConfigurationTable WorkflowConfigurationTable;
    boolean isFieldModified;





    int i;
    ;
    dialog = new dialog("Mass Re-assign workflow ");
    srcdf = dialog.addField(typeid(userid),"Source");
    destdf = dialog.addField(typeid(userid),"Destination");
    comtdf = dialog.addField(typeid(name),"Comments");
    congifIdDf = dialog.addField(typeid(WorkflowSequenceNumberActive),"Configuration Id");
   // confignamedf = dialog.addFieldValue(typeid(name),"","Configuration name");
    subjectDf = dialog.addField(typeid(WorkflowWorkItemSubject),"Subject");
    comtdf.displayLength(50);
  //  confignamedf.allowEdit(false);




     if(dialog.run())
    {
        src = srcdf.value();
        dest = destdf.value();
        comment =comtdf.value();
        congifId = congifIdDf.value();
        subject = subjectdf.value();
        WorkflowConfigurationTable = WorkflowConfigurationTable::findSequenceNumber(congifId,false);

        if(src!="" && dest !="")
        {

            if(subject == "" && congifId == "" )
            {
            while select workitems where workitems.Status == WorkflowWorkItemStatus::Pending
            && workitems.UserId == src

                {
                    WorkflowWorkItem::delegateWorkItem(workitems.Id, dest, comment);
                    i++;
                }
            }


            if(congifId != "" && subject == "")
            {
              while select workitems where
              workitems.Status == WorkflowWorkItemStatus::Pending &&
               workitems.UserId == src
              && workitems.ConfigurationId == WorkflowConfigurationTable.ConfigurationId
                {
                    WorkflowWorkItem::delegateWorkItem(workitems.Id, dest, comment);
                    i++;
                }
            }




            if(subject != "" && congifId == "" )
            {
                while select workitems where workitems.Status == WorkflowWorkItemStatus::Pending && workitems.UserId == src
                 && workitems.subject == subject
                {
                    WorkflowWorkItem::delegateWorkItem(workitems.Id, dest, comment);
                    i++;
                }
             }



            info(strFmt("Pending workflow workitems  re-assigned: %1",i));
        }
    }
    else
    {
        info("Canceled by user");
    }


}

Progress bar for lengthy operation on data source execute query in ax



Write this following method any on class:

Public static client SysOperationProgress showWaitDialog(str _text = "Extracting record for you, please wait...")
{
    #AviFiles
    SysOperationProgress waitDialog;

    waitDialog = new SysOperationProgress(1, true, strLen(_text));

    waitDialog.setCaption("Processing...");
  //  waitDialog.setTotal(100);
    waitDialog.setCount(1);
    waitDialog.setAnimation(#AviStopWatch);
    waitDialog.setText(_text);

    return waitDialog;
}



Call from datasource executequery() method:

public void executeQuery()
{
   SysOperationProgress waitdialog;
   ;
   waitdialog = global::showWaitDialog();  // startLengthyOperation();
   super();
   waitdialog.hide();  //  endLengthyOperation();
}

Sending Email using X++ Code in D365 F&O/ AX 7 in HTML fomat

  I was just researching on the emailing capabilities in D365 and found that SysINetMail, SysMailer and some of the smmOutlook classes are d...