Tuesday, 20 June 2017

Import the data from Excel to D365/Ax7

using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.ExcelPackage;
using OfficeOpenXml.ExcelRange;

class RunnableClass1
{      
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {  
        System.IO.Stream            stream;
        ExcelSpreadsheetName        sheeet;
        FileUploadBuild             fileUpload;
        DialogGroup                 dlgUploadGroup;
        FileUploadBuild             fileUploadBuild;
        FormBuildControl            formBuildControl;
        TableTest                          test;
        Dialog                      dialog = new Dialog("Import the data from Excel");

        dlgUploadGroup          = dialog.addGroup("@SYS54759");
        formBuildControl        = dialog.formBuildDesign().control(dlgUploadGroup.name());
        fileUploadBuild         = formBuildControl.addControlEx(classstr(FileUpload), 'Upload');
        fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);

        fileUploadBuild.fileTypesAccepted('.xlsx');

        if (dialog.run() && dialog.closedOk())

        {
            FileUpload fileUploadControl     = dialog.formRun().control(dialog.formRun().controlId('Upload'));

            FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();

            if (fileUploadResult != null && fileUploadResult.getUploadStatus())

            {

                stream = fileUploadResult.openResult();

                using (ExcelPackage Package = new ExcelPackage(stream))

                {

                      int                         rowCount, i;
                   
                      Package.Load(stream);
                   
                      ExcelWorksheet  worksheet   = package.get_Workbook().get_Worksheets().get_Item(1);
                   
                      OfficeOpenXml.ExcelRange    range       = worksheet.Cells;
                   
                      rowCount                  = (worksheet.Dimension.End.Row) - (worksheet.Dimension.Start.Row) + 1;
                    //rowCount = 1;
                    //i=Range.Rows;

                    for (i = 2; i<= rowCount; i++)

                    {

                        test.AccountNum = range.get_Item(i, 1).value;

                        test.AccountName = range.get_Item(i, 2).value;
                        test.insert();

                    }

                }

            }

            else

            {

                error("Error ");

            }
            info("Done");

        }

       

    }

}

Friday, 16 June 2017

Design Permissions for Fields in a Table

You can use the AOT to design permissions for the fields in a table. By changing the EffectiveAccess property in permissions for each of the fields you can control the application user access to those fields. For example, you can control whether the application user can view or edit some of the fields on a form based on the security role assigned to the application user.

Prerequisites

To understand this walkthrough topic, you first need to understand the following areas:

Preliminary Environment

This topic assumes that several AOT items already exist, or that you can imagine them. The items are as follows:
  • Table – Person table, with fields CityName, and Zip.
  • Form – FieldsForm form.
  • Data source – Person table as the data source for FieldsForm form.
  • Menu – Home > Common menu, which might already exist.
  • Menu Item – FieldsMenuItem menu item, with its ObjectType property set to Form, and its Object property set to FieldsForm.
  • Security > Privilege – TestFieldPrivilege privilege.
  • Privilege > TestFieldPrivilege > Entry Point – FieldsMenuItem, with its ObjectType property set to MenuItemDisplay.
    You can test with different values for the AccessLevel property, but start with Update.
The following image displays a project that contains almost everything in the preceding list. In the next section you create the node AOT > Security > Privileges > Permissions > Tables > Person, and the field nodes under it.
AOTSecurityFieldsPermProject
The project that you create

Create Field Permissions

You can create field permissions for TestFieldPrivilege by following these steps:

  1. Add the Person table to the TestFieldPrivilege privilege. Do this by dragging the node
     AOT > Data Dictionary > Tables > Person
    onto the node at
     AOT > Security > Privileges > TestFormPrivilege > Permissions > Tables.
    TipTip
    Drag operations are easier when you have two AOT windows open. You can drag from one AOT to the other.
  2. On the new Person node, set the EffectiveAccess property to Update.
  3. At Data Dictionary > Tables > Person > Fields, highlight all fields and drag them onto the TestFieldPrivilege > Permissions > Tables > Person node.
  4. Set the EffectiveAccess property for each new field node as follows:
    • City – Update
    • Name – Read
    • Zip – NoAccess