jeudi 12 mai 2011

How to avoid inserting duplicated data in a table in dynamics ax using CSV file

Here is a method that illustrates how to insert data that does not exist in a table :

static void import_Dimensions_PIRECO(Args _args)
{
    Dimensions                                  dimensions;

    container                                   c;
    TextIo                                      io;
    str 250                                     fileName;
    int                                         inc;
    TextBuffer                                  b;
    Sysdim                                      num;
    DimensionCode                               DimensionCode;
    int                                         diff,niv1,niv2,niv3,niv4,niv5,pos;

    str InsZero(Sysdim input,int niv)
    {
        pos =0;
        diff = niv - StrLen(input);

        while (pos < diff)
        {
            input = strins(input,"0",1);
            pos++;
        }
            return input;
    }

    ;
     niv1 = 2;
     niv2 = 2;
     niv3 = 4;
     niv4 = 4;
     niv5 = 6;

     fileName = @"D:\AXS\GELBADR\Livrable\ThyProjet_codification_COST_CODE_v2.1.csv";
     b=new Textbuffer();

     io = SysLicenseCodeReadFile::openFile(fileName,'r');
     if (!io)
        throw error(strfmt("@SYS18678",fileName));
     io.inFieldDelimiter(";");
     c = io.read();
     while (io.status() == IO_Status::Ok)
     {
         c = io.read();
         inc++;
         if (io.status() != IO_Status::Ok)
         break;

         if ((conpeek(c,1) != "") && (conpeek(c,2) != ""))
         {
            num = conpeek(c,1);
            num = InsZero(num,niv1);

            DimensionCode = Sysdimension::Niveau_I;
            select firstonly dimensions
                where dimensions.Num           == num
                  &&  dimensions.DimensionCode == DimensionCode;
            if (!dimensions)
            {
                dimensions.Num = conpeek(c,1);
                dimensions.Description = strRem(conpeek(c,2),'"');
                dimensions.DimensionCode = Sysdimension::Niveau_I;
                print conpeek(c,1),"   ",conpeek(c,2);
                dimensions.insert();
            }
         }

         if ((conpeek(c,3) != "") && (conpeek(c,4) != ""))
         {
            num = conpeek(c,3);
            num = InsZero(num,niv2);

            DimensionCode = Sysdimension::Niveau_II;
            select firstonly dimensions
                where dimensions.Num           == num
                  &&  dimensions.DimensionCode == DimensionCode;
            if (!dimensions)
            {
                dimensions.Num = conpeek(c,3);
                dimensions.Description = strRem(conpeek(c,4),'"');
                dimensions.DimensionCode = Sysdimension::Niveau_II;
              //  print conpeek(c,1),"   ",conpeek(c,2);
                dimensions.insert();
            }
         }

         if ((conpeek(c,5) != "") && (conpeek(c,6) != ""))
         {
            num = conpeek(c,5);
            num = InsZero(num,niv3);

            DimensionCode = Sysdimension::Niveau_III;
            select firstonly dimensions
                where dimensions.Num           == num
                  &&  dimensions.DimensionCode == DimensionCode;
            if (!dimensions)
            {
                dimensions.Num = conpeek(c,5);
                dimensions.Description = strRem(conpeek(c,6),'"');
                dimensions.DimensionCode = Sysdimension::Niveau_III;
              //  print conpeek(c,1),"   ",conpeek(c,2);
                dimensions.insert();
            }
         }

         if ((conpeek(c,7) != "") && (conpeek(c,8) != ""))
         {
            num = conpeek(c,7);
            num = InsZero(num,niv4);

            DimensionCode = Sysdimension::Niveau_IV;
            select firstonly dimensions
                where dimensions.Num           == num
                  &&  dimensions.DimensionCode == DimensionCode;
            if (!dimensions)
            {
                dimensions.Num = conpeek(c,7);
                dimensions.Description = strRem(conpeek(c,8),'"');
                dimensions.DimensionCode = Sysdimension::Niveau_IV;
             //   print conpeek(c,1),"   ",conpeek(c,2);
                dimensions.insert();
            }
         }

         if ((conpeek(c,9) != "") && (conpeek(c,10) != ""))
         {
            num = conpeek(c,9);
            num = InsZero(num,niv5);

            DimensionCode = Sysdimension::Niveau_V;
            select firstonly dimensions
                where dimensions.Num           == num
                  &&  dimensions.DimensionCode == DimensionCode;
            if (!dimensions)
            {
                dimensions.Num = conpeek(c,9);
                dimensions.Description = strRem(conpeek(c,10),'"');
                dimensions.DimensionCode = Sysdimension::Niveau_V;
                //print conpeek(c,1),"   ",conpeek(c,2);
                dimensions.insert();
            }
         }

      }
}

Aucun commentaire:

Enregistrer un commentaire