Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV forum
Suggested answer

xmlport date returns 2020 instead 2018

Posted on by Microsoft Employee

Hi everyone, something really strange is happening with my xmlport.

From one moment to another, the date read from a file is "2020" instead of "2018" and the field "Ending Date" of my previous record can't be calculated (this field is equal to the last starting date - 1 day).

So I have a text file with the info below

1010100002 11/11/2018 0 11,95 (item no | starting date | quantity | price)

and the idea is to fill the Sales Price table with this information using my dataport with the following code

Sales Price - Import::OnBeforeInsertRecord()
nRecNum += 1;
dlgProgress.UPDATE(3, nRecNum);

"Sales Price".RESET;
"Sales Price".SETRANGE("Sales Price"."Item No.",PRODUTO); 
"Sales Price".SETRANGE("Sales Price"."Sales Type",1);
"Sales Price".SETRANGE("Sales Price"."Sales Code",'ALL');
"Sales Price".SETRANGE("Sales Price"."VAT Bus. Posting Gr. (Price)",'NAC');
"Sales Price".SETRANGE("Ending Date",0D); 
"Sales Price".SETRANGE("Currency Code",''); 
"Sales Price".SETRANGE("Variant Code",'');
"Sales Price".SETRANGE("Unit of Measure Code",'');
//"Sales Price".SETRANGE("Minimum Quantity",QT)

DataInicioText:=COPYSTR(DATAINI,1,8);
dia := COPYSTR(DataInicioText,1,2);
mes := COPYSTR(DataInicioText,4,2);
ano:= COPYSTR(DataInicioText,7,4);

MESSAGE('ano %1',ano);
MESSAGE('data %1',DataInicioText);
EVALUATE(DataInicio,dia+mes+ano);

"Sales Price".SETRANGE("Starting Date",DataInicio);

"Sales Price"."Starting Date":=DataInicio;

IF "Sales Price".FINDFIRST THEN BEGIN
   IF DataInicio < "Sales Price"."Starting Date" THEN
     "Sales Price".DELETEALL;
   END;


"Sales Price"."Item No.":=PRODUTO;
"Sales Price"."Allow Invoice Disc." :=TRUE; 
"Sales Price"."VAT Bus. Posting Gr. (Price)":='NAC'; 
"Sales Price"."Allow Invoice Disc.":=TRUE;

"Sales Price"."Sales Code":='ALL';
"Sales Price"."Sales Type":=1;
"Sales Price"."Currency Code":='';
"Sales Price"."Variant Code":='';
"Sales Price"."Ending Date":=0D;

"Sales Price".VALIDATE("Sales Price"."Sales Code",'ALL');
"Sales Price".VALIDATE("VAT Bus. Posting Gr. (Price)",'NAC');

Item.GET(PRODUTO);
"Sales Price"."Unit of Measure Code":=Item."Base Unit of Measure";


//Quantity

Quantity:=FORMAT(QT); 
EVALUATE("Sales Price"."Minimum Quantity",Quantity);

//Unit Price

Price:=FORMAT(PVP); 
EVALUATE("Sales Price"."Unit Price",Price);

//Unit Price with vat

VATPercentageProd:=Item."VAT Prod. Posting Group";
VATPercentageBus:=Item."VAT Bus. Posting Gr. (Price)";
VATPostingSetup.GET(VATPercentageBus,VATPercentageProd);
VATPerCent:=VATPostingSetup."VAT %";

EVALUATE(PriceVAT,PVP);
"Sales Price"."Unit Price Including VAT":=ConvertPriceToVAT(VATPerCent,PriceVAT);
 
IF NOT "Sales Price".INSERT(TRUE) THEN BEGIN
  "Sales Price".NEXT(-1);
  "Sales Price"."VAT Bus. Posting Gr. (Price)":='NAC';
  "Sales Price"."Ending Date":=DataInicio-1;
  "Sales Price".MODIFY(TRUE);
END;


And this xmlport is used in page 31 (item list) in a page action which have the property "RunObject" with "XMLport ImportTabPrecos".

Anyone can understand this strange thing?

Categories:
  • Suggested answer
    ShanAbeywicrema Profile Picture
    ShanAbeywicrema 940 on at
    RE: xmlport date returns 2020 instead 2018

    Hi,

    You cannot say right or wrong in the code, all depends on the experience and practice. Sometimes we do not need to write more code just simple few line work is done. If we can follow good practices then quality of code and work is high.

    In your code,   "Sales Price".NEXT(-1) is useless, With your filters, you are trying to take last record then try to take one recode before last if I am not wrong, So you could try another way.  But if you think, you can simplify this code.

  • Hannes Holst Profile Picture
    Hannes Holst 5,763 on at
    RE: xmlport date returns 2020 instead 2018

    Hello,

    In development there is no "right" and "wrong".

    Just "good" and "bad" :-)

    I personally would store the Primary Key of the previous record in variables or leave a pointer on another record-variable.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: xmlport date returns 2020 instead 2018

    Hmmm ok I get it.. So I need to setfitler before doing the findlast in order to get that record.

    I've used this and worked.. What you think? Is not a right way? I Know that i want always the previous line of what i'm inserting so I add this "Sales Price".NEXT(-1); to my code

    IF "Sales Price".INSERT(TRUE) THEN BEGIN
      "Sales Price".RESET;
      "Sales Price".SETRANGE("Sales Price"."Item No.",PRODUTO); 
      "Sales Price".SETRANGE("Sales Price"."Sales Type",1);
      "Sales Price".SETRANGE("Sales Price"."Sales Code",'ALL');
      "Sales Price".SETRANGE("Sales Price"."VAT Bus. Posting Gr. (Price)",'NAC');
      "Sales Price".SETRANGE("Ending Date",0D); 
      "Sales Price".SETRANGE("Currency Code",''); 
      "Sales Price".SETRANGE("Variant Code",'');
      IF "Sales Price".FINDLAST THEN BEGIN
         MESSAGE('data 1 %1 ',"Sales Price"."Starting Date");
        "Sales Price".NEXT(-1);
        "Sales Price"."Ending Date":=DataInicio-1;
        "Sales Price".MODIFY(TRUE);
      END;
    END ELSE BEGIN
      "Sales Price".MODIFY(TRUE);
    END;
    

    6840.price.png

    And worked...

  • Hannes Holst Profile Picture
    Hannes Holst 5,763 on at
    RE: xmlport date returns 2020 instead 2018

    Hi,

    Read a little bit about Primary Keys.

    The Primary Key of the Sales Price-table is "Item No.,Sales Type,Sales Code,Starting Date,Currency Code,Variant Code,Unit of Measure Code,Minimum Quantity"

    That means, it is not a given fact that the order in which you import, is the order in which NAV stores the data.

    The NEXT-command does select the data based on the Primary Key (and not on the order in which you did the import).

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: xmlport date returns 2020 instead 2018

    But the field "Starting date" of the previous record should be filled with a date also. In this case should have the value 12-11-2018 (Starting Date readed from file- 1);

    But my problem now is I tried this code

    IF "Sales Price".INSERT(TRUE) THEN BEGIN
    "Sales Price".RESET;
    IF "Sales Price".FINDLAST THEN BEGIN
    MESSAGE('data 1 %1 ',"Sales Price"."Starting Date");
    "Sales Price".NEXT(-1);
    MESSAGE('data 2 %1 ',"Sales Price"."Starting Date");
    "Sales Price"."Ending Date":=DataInicio-1;
    "Sales Price".MODIFY(TRUE);
    END;


    But the previous record is not being modified... And the message shows the a wrong previous record like the image below...

    I need to fill the field Ending Date right next to the date 08-11-2018 to close the last product price.

    price.png


    My idea is when inserting a new record I always need to fill the Ending Date of my previous record. If I'm modifyng a record already inserted in the system, the idea is to modify the existing record understand?

  • Hannes Holst Profile Picture
    Hannes Holst 5,763 on at
    RE: xmlport date returns 2020 instead 2018

    Why doing a FINDLAST?

    After the INSERT, the pointer still exists on the record.

    It doesn't make really sense to do it like this, because you can assign

    "Sales Price"."Ending Date":="Sales Price"."Starting Date"-1;


    before the INSERT.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: xmlport date returns 2020 instead 2018

    My problem with the 2020 date was here DataInicioText:=COPYSTR(DATAINI,1,8). I solved it with DataInicioText:=COPYSTR(DATAINI,1,10);

    But my problem now is I tried this code 

    IF "Sales Price".INSERT(TRUE) THEN BEGIN
      "Sales Price".RESET;
      IF "Sales Price".FIND('-') THEN BEGIN
        MESSAGE('check %1',DataInicio-1);
        "Sales Price"."Ending Date":=DataInicio-1;
        "Sales Price".MODIFY(TRUE);
      END;
    END;

    But the previous record is not being modified... And the message shows the data correctly...

    shelf-label.png

    I need to fill the date to close the last product price.
  • Hannes Holst Profile Picture
    Hannes Holst 5,763 on at
    RE: xmlport date returns 2020 instead 2018

    Hi,

    Maybe this will do the trick:

    Sales Price - Import::OnBeforeInsertRecord()
    nRecNum += 1;
    dlgProgress.UPDATE(3, nRecNum);
    
    IF NOT Item.GET(PRODUTO) THEN
      currXMLpot.SKIP;
    
    "Sales Price".RESET;
    "Sales Price".SETRANGE("Sales Price"."Item No.",PRODUTO); 
    "Sales Price".SETRANGE("Sales Price"."Sales Type",1);
    "Sales Price".SETRANGE("Sales Price"."Sales Code",'ALL');
    "Sales Price".SETRANGE("Sales Price"."VAT Bus. Posting Gr. (Price)",'NAC');
    "Sales Price".SETRANGE("Ending Date",0D); 
    "Sales Price".SETRANGE("Currency Code",''); 
    "Sales Price".SETRANGE("Variant Code",'');
    "Sales Price".SETRANGE("Unit of Measure Code",'');
    //"Sales Price".SETRANGE("Minimum Quantity",QT)
    
    DataInicioText:=COPYSTR(DATAINI,1,8);
    dia := COPYSTR(DataInicioText,1,2);
    mes := COPYSTR(DataInicioText,4,2);
    ano:= COPYSTR(DataInicioText,7,4);
    
    MESSAGE('ano %1',ano);
    MESSAGE('data %1',DataInicioText);
    EVALUATE(DataInicio,dia+mes+ano);
    
    "Sales Price".SETRANGE("Starting Date",DataInicio);
    
    //"Sales Price"."Starting Date":=DataInicio;
    
    IF "Sales Price".FINDFIRST THEN BEGIN 
    // this doesn't make sense to me, because you are filtering with DataInicio already.
    // that means the DELETEALL will never be executed 
    //   IF DataInicio < "Sales Price"."Starting Date" THEN 
    //     "Sales Price".DELETEALL;						  
       END;
    
    "Sales Price".RESET;
    "Sales Price".INIT;
    "Sales Price"."Starting Date":=DataInicio;
    "Sales Price"."Item No.":=PRODUTO;
    "Sales Price"."Allow Invoice Disc." :=TRUE; 
    "Sales Price"."VAT Bus. Posting Gr. (Price)":='NAC'; // why assigning data here and validiting below with the same value?
    "Sales Price"."Allow Invoice Disc.":=TRUE;
    
    "Sales Price"."Sales Code":='ALL'; // why assigning data here and validiting below with the same value?
    "Sales Price"."Sales Type":=1;
    "Sales Price"."Currency Code":='';
    "Sales Price"."Variant Code":='';
    "Sales Price"."Ending Date":=0D;
    
    "Sales Price".VALIDATE("Sales Price"."Sales Code",'ALL');
    "Sales Price".VALIDATE("VAT Bus. Posting Gr. (Price)",'NAC');
    
    "Sales Price"."Unit of Measure Code":=Item."Base Unit of Measure";
    
    
    //Quantity
    
    Quantity:=FORMAT(QT); 
    EVALUATE("Sales Price"."Minimum Quantity",Quantity);
    
    //Unit Price
    
    Price:=FORMAT(PVP); 
    EVALUATE("Sales Price"."Unit Price",Price);
    
    //Unit Price with vat
    
    VATPercentageProd:=Item."VAT Prod. Posting Group";
    VATPercentageBus:=Item."VAT Bus. Posting Gr. (Price)";
    VATPostingSetup.GET(VATPercentageBus,VATPercentageProd);
    VATPerCent:=VATPostingSetup."VAT %";
    
    EVALUATE(PriceVAT,PVP);
    "Sales Price"."Unit Price Including VAT":=ConvertPriceToVAT(VATPerCent,PriceVAT);
     
    IF NOT "Sales Price".INSERT(TRUE) THEN BEGIN
      "Sales Price".NEXT(-1);
      "Sales Price"."VAT Bus. Posting Gr. (Price)":='NAC';
      "Sales Price"."Ending Date":=DataInicio-1;
      "Sales Price".MODIFY(TRUE);
    END;


    If not, consider that the structure of the element which represents the date might change throughout the file you want to import.
    You could/should also consider to use seperate record-variables for the different jobs. E.g. "SalesPriceCheck [Record : Sales Price]" for the filtering in the beginning and "SalesPriceNew [Record : Sales Price]" to insert the new records.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 288,584 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,864 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans