Friday, August 6, 2021

x++ code to move files from one folder to another after processing business logic

 public void run()

{

    CommaTextIo     commaTextIo;

    container values;

      FolderName      sourceFolder = @"\\test\Source";

    FolderName      destinationFolder = @"\\test1\Archive";

    System.Array    files;

    int fileCount, i;

    Filename    filepath;

    Filename    filename;

    Filename    fileType;

    // code added by dilip on 8/1/2018

           // str         currentFile;    - Declared the cuurentFile and destinationFile in classDeclaration Method

            #File

         InteropPermission   interopPerm;

         FileIOPermission    filepermission;

         Set                 permissionSet;


            // code ended by dilip on 8/1/2018



    InventJournalTable  inventJournalTable;

    InventJournalTrans  inventJournalTrans;

    InventDim           inventDim;

    JournalCheckPost    journalCheckPost;

    InventJournalName   inventJournalName;

    InventBatch         inventBatch;


    boolean             isFirst;


    new InteropPermission(InteropKind::ClrInterop).assert();


    try

    {

        files = System.IO.Directory::GetFiles(sourceFolder);


        fileCount = files.get_Length();


        for(i = 0; i < fileCount; i++)

        {

            currentFile = files.GetValue(i);

            commaTextIo = new CommaTextIo(currentFile,'R');

            isFirst = true;


            values = commaTextIo.read();

            values = commaTextIo.read();


            ttsBegin;


            while(values != conNull())

            {

                if(isFirst)

                {

                    inventJournalTable.clear();


                    inventJournalName = InventJournalName::find(conPeek(values, 2));


                    if(!inventJournalName)

                    {

                        break;

                    }


                    inventJournalTable.initFromInventJournalName(inventJournalName);

                    inventJournalTable.insert();


                    isFirst = false;

                }


                inventJournalTrans.clear();


                inventJournalTrans.initFromInventJournalTable(inventJournalTable);

                inventJournalTrans.TransDate    = str2Date(conPeek(values, 4), 213);

                inventJournalTrans.ItemId       = conPeek(values, 5);

                inventJournalTrans.initFromInventTable(InventTable::find(inventJournalTrans.ItemId));

                inventJournalTrans.Qty          = str2num(conPeek(values, 11));


                inventDim.InventColorId         = conPeek(values, 6);

                inventDim.InventSizeId          = conPeek(values, 7);

                inventDim.InventLocationId      = conPeek(values, 8);

                inventDim.InventSiteId          = conPeek(values, 9);

                inventDim.inventBatchId         = conPeek(values, 10);


                select firstOnly RecId from inventBatch

                    where inventBatch.inventBatchId ==  inventDim.inventBatchId

                    && inventBatch.itemId ==  inventJournalTrans.ItemId;


                if(!inventBatch.RecId)

            {

                   inventBatch.clear();


                  inventBatch.initValue();

                  inventBatch.itemId = inventJournalTrans.ItemId;

                  inventBatch.inventBatchId = inventDim.inventBatchId;

                  inventBatch.insert();

               }


                inventJournalTrans.InventDimId = InventDim::findOrCreate(inventDim).inventDimId;


                  inventJournalTrans.setCostPrice('', inventJournalTrans.inventDim(),  inventJournalTrans.inventTableModuleInvent());


                inventJournalTrans.insert();


                values = commaTextIo.read();

                  inventJournalTable.NumOfLines += 1;

        inventJournalTable.update();

            }


            //if(inventJournalTable)

            //{

                //journalCheckPost = InventJournalCheckPost::newPostJournal(inventJournalTable);

                //journalCheckPost.run();

            //}


            ttsCommit;


            [filepath, filename, fileType] = fileNameSplit(currentFile);


           // code added by dilip on 8/1/2018

             destinationFile = destinationFolder + "\\" + filename + fileType;


                permissionSet =  new Set(Types::Class);

                permissionSet.add(new InteropPermission(InteropKind::ClrInterop));

                permissionSet.add(new FileIOPermission(destinationFile,'w'));

                permissionSet.add(new FileIOPermission(currentFile,'rw'));

                CodeAccessPermission::assertMultiple(permissionSet);


                    //copy the file from source folder to destination folder

                    WINAPIServer::copyFile(currentFile, destinationFile);


                    //delete the file from source folder

                    commaTextIo.finalize();

                    commaTextIo = null;

                    filepermission = new FileIOPermission(currentFile, 'w');

                    filepermission.demand();

                    System.IO.File::Delete(currentFile);

                    CodeAccessPermission::revertAssert();

        // code ended by dilip on 8/1/2018

        }

    }

    catch

    {

        error("Import process failed");

    }


    CodeAccessPermission::revertAssert();

}

Add , update and delete BOM lines using X++ job

Belo job is used to add, update and delete bom items 

Make sure the import file is .csv 




static void lanMassupdateBOMlines_2019(Args _args)

{

 CommaTextIo     commaTextIo;

    container values;

    FolderName      sourceFolder = @"C:\MassUpdate";


    System.Array    files;

    int fileCount, i, intdel, intadd;

    real BOMQty;

    Filename    filepath;

    Filename    filename;

    Filename    fileType;

    str         currentFile,itemgroup,colorid,sizeid,bomcolor,bomsize,action,flushprinciple;


        InventTable inventtable;

    BOMVersion bomVersion;

    BOM bom;


    BOMTable bomTable;

  ItemId itemID, bomitemID;

    InventDim           inventDim,inventdimnew,inventdimBOM;

    InventDimCombination inventdimcombination;


    boolean             isFirst, ignore;


    new InteropPermission(InteropKind::ClrInterop).assert();


    try

    {

        files = System.IO.Directory::GetFiles(sourceFolder);


        fileCount = files.get_Length();


        for(i = 0; i < fileCount; i++)

        {

            currentFile = files.GetValue(i);

            commaTextIo = new CommaTextIo(currentFile,'R');

            isFirst = true;


            values = commaTextIo.read();



            ttsBegin;


            while(values != conNull())

            {

    itemID = any2str(conPeek(values, 1));

                  itemgroup = any2str(conPeek(values, 2));

                // get Item master

                    inventtable = InventTable::find(itemID);

  colorid =  any2str(conPeek(values, 4));

                       sizeid =  any2str(conPeek(values, 5));

                             bomitemID = any2str(conPeek(values, 7));

                       bomcolor = any2str(conPeek(values, 8));

                              bomsize =  any2str(conPeek(values, 9));

                BOMQty = str2num(conPeek(values, 10));

                  flushprinciple = any2str(conPeek(values, 11));

                  action =  any2str(conPeek(values, 12));

                 if(colorid == 'ANY' && sizeid == 'ALL' && action == 'DELETE')

                   {

                                 // get list of BOM versions associated to the Item and Site 'CAC'

                  while select * from bomVersion

                      where bomVersion.itemId == inventtable.ItemId && bomVersion.Name like '*CA'


                  {


                          select forUpdate bom  where bom.BOMId == bomVersion.BOMId

                          && bom.ItemId == bomitemID;

                      if (bom)


                          {

                                 ttsBegin;

                                bom.delete();

                              intdel++;


                                ttsCommit;

                          }


                       }


                       }

                else

                       if(colorid != 'ANY' && sizeid == 'ALL' && action == 'DELETE')

                {

                             // get list of BOM versions associated to the Item and Site 'CAC'

                  while select * from bomVersion

                      where bomVersion.itemId == inventtable.ItemId && bomVersion.Name like '*CA'


                  {

                            select * from inventdim where inventDim.inventDimId == bomVersion.InventDimId;


                       //check if BOM line already exists on the BOM Version

                   //select * from bom where bom.ItemId == bomitemID && bom.BOMId == bomVersion.BOMId && bom.BOMQty == 1.02;


                  //  if(!bom)

                  //  {


                        // if no BOM exists create new BOM line



                       if(inventDim.InventColorId == colorid)


                       {


                          select forUpdate bom  where bom.BOMId == bomVersion.BOMId

                          && bom.ItemId == bomitemID;

                      if (bom)


                          {

                                 ttsBegin;

                                bom.delete();

                              intdel++;


                                ttsCommit;

                          }



                      }


                      }

                }


            if(colorid == 'ANY' && sizeid == 'ALL' && action == 'ADD')

                {


                      // get list of BOM versions associated to the Item and Site 'CAC'

                  while select * from bomVersion

                      where bomVersion.itemId == inventtable.ItemId && bomVersion.Name like '*CA'


                  {

                            select * from inventdim where inventDim.inventDimId == bomVersion.InventDimId;


                       //check if BOM line already exists on the BOM Version

                   select * from bom where bom.ItemId == bomitemID && bom.BOMId == bomVersion.BOMId

                   && bom.BOMQty == BOMQty;


                    if(!bom)

                    {


                        // if no BOM exists create new BOM line


                       if(inventDim.InventColorId == colorid || colorid == 'ANY')


                       {

                            bom.clear();

                            inventdimnew.clear();

                            bom.initValue();


                            bom.BOMId = bomVersion.BOMId;

                            bom.ItemId = bomitemID;

                            bom.BOMQty = str2num(conPeek(values, 10));

                            bom.OprNum = 0;

                            bom.UnitId = 'EA';

                            inventdimnew.InventColorId = bomcolor;

                            inventdimnew.InventSiteId = 'CAC';


                               if (bomsize == 'ALL')

                               {

                             inventdimnew.InventSizeId = inventDim.InventSizeId;

                               }

                               else

                               {

                                    inventdimnew.InventSizeId = bomsize;

                               }


                           // inventdimnew.InventLocationId =  inventDim.InventLocationId;

                            inventdimnew.InventLocationId =  'CA';


                            bom.InventDimId = InventDim::findOrCreate(inventdimnew).inventDimId;

                           if (flushprinciple == 'Manual')

                           {

                            bom.ProdFlushingPrincip = 1;

                           }

                           else

                           {

                              bom.ProdFlushingPrincip = 3;

                           }

                                // validate the BOM

                            if(bom.validateWrite())

                            {


                                 bom.insert();

                                 intadd++;


                            }

                            }

                      }

                      }

                    }

                    else


                    {


              if(colorid != 'ANY' && sizeid == 'ALL' && action == 'ADD' )

                {


                      // get list of BOM versions associated to the Item and Site 'CAC'

                  while select * from bomVersion

                      where bomVersion.itemId == inventtable.ItemId && bomVersion.Name like '*CA'


                  {

                            select * from inventdim where inventDim.inventDimId == bomVersion.InventDimId;


                       //check if BOM line already exists on the BOM Version

                 select * from bom where bom.ItemId == bomitemID && bom.BOMId == bomVersion.BOMId && bom.BOMQty == BOMQty;


                if(!bom)

                {


                        // if no BOM exists create new BOM line


                       if(inventDim.InventColorId == colorid)


                       {

                            bom.clear();

                            inventdimnew.clear();

                            bom.initValue();


                            bom.BOMId = bomVersion.BOMId;

                            bom.ItemId = bomitemID;

                            bom.BOMQty = str2num(conPeek(values, 10));

                            bom.OprNum = 0;

                            bom.UnitId = 'EA';

                            inventdimnew.InventColorId = bomcolor;

                            inventdimnew.InventSiteId = 'CAC';


                               if (bomsize == 'ALL')

                               {

                             inventdimnew.InventSizeId = inventDim.InventSizeId;

                               }

                               else

                               {

                                    inventdimnew.InventSizeId = bomsize;

                               }


                            //inventdimnew.InventLocationId =  inventDim.InventLocationId;

                           inventdimnew.InventLocationId =  'CA';

                            bom.InventDimId = InventDim::findOrCreate(inventdimnew).inventDimId;

                           if (flushprinciple == 'Manual')

                           {

                            bom.ProdFlushingPrincip = 1;

                           }

                           else

                           {

                              bom.ProdFlushingPrincip = 3;

                           }

                                // validate the BOM

                            if(bom.validateWrite())

                            {


                                 bom.insert();

                                 intadd++;

                            }

                            }


                      }

                     }


                   }


                }



                values = commaTextIo.read();


            }



            ttsCommit;

          //  Box::info("Success, Imported BOM lines for New Fit HangTag");

            [filepath, filename, fileType] = fileNameSplit(currentFile);


 info(strfmt("Total number of Boms deleted are - %1", intdel));

                 info(strfmt("Total number of BOMs added are - %1", intadd));

        }

    }

    catch

    {

        error("Import process failed");

    }


    CodeAccessPermission::revertAssert();

}

AX 2012 R3 - SSRS install

 


steps to install SSRS extensions:


- Install SQL server SSRS services first

- Using Reporting services configuration Manager, configure the reporting server, database and URL as shown below


Configure SQL Server Reporting Service 2016 (SSRS).

 Once the SSRS feature is installed successfully, you should configure it manually by doing the following:

Open Reporting Service Configuration Manager.



Connect to the Report Server Instance.



Review Report Server Status and make sure it's started.



In "Service Account", you will find the report server service account that runs the Report Server Service.



Go first to the "Database" section to configure the database that holds the all report server content and application data.



Note: in case you have deleted a report in reporting service, you can't restore it! the available options are

  • you have a copy of the deleted report on your local machine so you can upload it again.
  • you have a backup from the report database so you can restore it.

Click Change database to create the report server database for the first time.

Check Create a new report server database.



Make sure the database server is correct.

Set the authentication type as (Integrated User or SQL Server account). 

Note: this account must have permission to connect to the database server. 

Provide the correct account credentials.



Test the connection and make sure that the connection is established successfully.



Specify the database name as you prefer.



Specify the credentials of an existing account that the report server will use to connect to the report server database.



Review the summary information, and make sure that everything is set properly before proceeding.



Wait while the report server database wizard configures the database then click Finish.



Go back to configure the "Report Server Web Service URL".

  • Set the Report Server Web Service URL virtual directory.
  • Set the Report Server web service site identifications based on your requirements then click Apply.



Test the Report Server Web Service URL that should be now browsed properly.



Go now to the "Report Manager URL", specify the virtual directory as you prefer then click Apply to configure it.



Once the Report Manager Completed Successfully, Click the Report Manager URL to test it.



Great, it should be now browsed properly.


  Note

If you got a permission issue to access report manager, please check SSRS 2016: You are not allowed to view this folder. Contact your administrator to obtain the necessary permissions. Jump Jump

Go now to the "Encryption keys".



Take a backup of the symmetric key that used to encrypt sensitive data in the report server database like connection strings, credentials ...etc. it will be helpful in case you migrate or move the report server installation to another server, you can restore this key to regain access to the encrypted content.