First row on XmlPort

ozkanozkan Member Posts: 5
Hi everyone,
I have a little problem with C/Side coding on XMLport.
What should I do for call to first row from Sales Header and Sales Line
this is my code for Manuel order number
"Sales Header".SETRANGE("Document Type","Sales Header"."Document Type"::Order);
"Sales Header".SETRANGE("Sales Header"."No.",'109003');

Best Answer

Answers

  • ridz_robbinridz_robbin Member Posts: 3
    You can use GET function if you want to get first row only.
    Example:
    "Sales Header".SETCURRENTKEY("No. ");
    IF "Sales Header".GET(109003) THEN
    BEGIN
    //your code
    END;
    
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    edited 2017-01-26
    As @ridz_robbin said if you want to get only first row (one record) then GET is the right way to do. Before using GET make sure to clear your variable and use SETCURRENTKEY.

    Usually, SETRANGE and SETFILTER are used when you want to get more than one record.

    You can get record via SETRANGE as:
    "Sales Header".SETRANGE("Document Type","Sales Header"."Document Type"::Order);
    "Sales Header".SETRANGE("Sales Header"."No.",'109003');
    IF "Sales Header".FINDFIRST THEN
     BEGIN
      // do something
     END;
    
    

    Hope this helps.
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • zaidtariqzaidtariq Member Posts: 52
    This is not really a good idea to use SETRANGE if you are sure that there is always a single record. Use SalesHeader.GET (109003) to get single record or

    "Sales Header".SETRANGE("Sales Header"."No.",'109003');
    IF "Sales Header".FINDFIRST THEN
    BEGIN
    // do something
    END;

    Hope this would be helpful
    Best Regards:
    Zaid Tariq
    Dynamics NAV/365 BC Developer at Dynamics 360

    please like / agree / verify my answer, if was helpful.
  • vaprogvaprog Member Posts: 1,116
    You can use GET function if you want to get first row only.
    Example:
    "Sales Header".SETCURRENTKEY("No. ");
    IF "Sales Header".GET(109003) THEN
    BEGIN
    //your code
    END;
    

    To get a Sales Header you need 2 parameters, corresponding to the fields in the primary key in the correct order (and with correct types)
    "Sales Header".GET("Sales Header"."Document Type"::Order,'109003');
    
    For Sales Line you need 3 parameters to GET.
    zohaibu95 wrote: »
    Before using GET make sure to clear your variable and use SETCURRENTKEY.
    No need, neither for CLEAR nor for SETCURRENTKEY to GET a record. GET ignores filters and does not sort the one row it returns. Those statements don't hurt, but neither do they help.

  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    vaprog wrote: »
    You can use GET function if you want to get first row only.
    Example:
    "Sales Header".SETCURRENTKEY("No. ");
    IF "Sales Header".GET(109003) THEN
    BEGIN
    //your code
    END;
    

    To get a Sales Header you need 2 parameters, corresponding to the fields in the primary key in the correct order (and with correct types)
    "Sales Header".GET("Sales Header"."Document Type"::Order,'109003');
    
    For Sales Line you need 3 parameters to GET.
    zohaibu95 wrote: »
    Before using GET make sure to clear your variable and use SETCURRENTKEY.
    No need, neither for CLEAR nor for SETCURRENTKEY to GET a record. GET ignores filters and does not sort the one row it returns. Those statements don't hurt, but neither do they help.

    Its a good approach to clear the variable and set the current key because we never know may be we are using that variable above for some other filters as well.
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • ReinhardReinhard Member Posts: 249
    GET ignores filters and keys.
    https://msdn.microsoft.com/en-us/library/dd301056.aspx
    "This function always uses the primary key for the table and ignores any filters."

    This means that you won't need to CLEAR or SETRANGE or SETCURRENTKEY, as these will have no effect on GET.
    Also, GET will return TRUE/FALSE depending on if a record is found.

    IF "Sales Header".GET("Sales Header"."Document Type"::Order,'109003') THEN
    //do stuff
  • ozkanozkan Member Posts: 5
    Thanks for all but it should be a loop and call first row and next row with order number already can I calling any order I want call all with a loop pre order new XmlFile this is my Codeunit code
    "Sales Header".SETRANGE("Document Type","Sales Header"."Document Type"::Order);
    "Sales Header".SETRANGE("Sales Header"."No.",'109003'); <<<<<<<<<< This should be a Loop and call all Lines from tables Sales Header and Sales Line
    IF "Sales Header".FINDSET THEN BEGIN
    REPEAT
    filename := 'C:\Temp\' + FORMAT("Sales Header"."No.") + '.xml';
    ClientFileName := FORMAT("Sales Header"."No.") + '.xml';
    XmlFile.CREATE(filename);
    XmlFile.CREATEOUTSTREAM(XmlOutStream);

    XMLPORT.EXPORT(55667,XmlOutStream,"Sales Header");


    XmlFile.CLOSE;

    UNTIL "Sales Header".NEXT = 0;
    END;
    MESSAGE('Saved');
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    edited 2017-01-27
    @ozkan it should be like this:
    CLEAR("Sales Header");
    CLEAR("Sales Line");
    "Sales Header".SETCURRENTKEY("Document Type", "No.");
    IF "Sales Header".GET("Document Type"::Order, 109003) THEN
    BEGIN
      "Sales Line".SETFILTER("Document No.", Your order no); //this will be your Sales Header document no.
       IF "Sales Line".FINDSET THEN
       REPEAT
          //your code
       UNTIL "Sales Line".NEXT = 0;
    END
    


    You can also set range if you want multiple orders in that range.

    Also if you want to get order from 109003 onwards than do this
    "Sales Header".SETFILTER("No.", '109003..'); 
    
    IF "Sales Header".FINDSET THEN REPEAT
    // this will get the orders from 109003 till end;.
    UNTIL "Sales Header".NEXT = 0;
    

    Please accept my answer if it helps.
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • ozkanozkan Member Posts: 5
    zohaibu95 Thank you but this is not my problem, i have trouble on XmlPort side my XmlPort Calling every Orders till end I tried so many filtering under <Sales Header> - Export::OnAfterGetRecord() trigger no function what did you mean should I do Filter on Codeunit side or on XmlPort?

    My XmlPort Should do: Call first row from Sales Header and Sales Line and after that next row till last Order, with codeunit save all orders as name ordernumer.xml to on Server look like this
    dffuj6knrrex.png
    g8hmjqqdzuet.png
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    edited 2017-01-27
    Okay then simply all you need is to do this:
    IF "Sales Header".FINDSET THEN
    REPEAT
      "Sales Line".SETFILTER("Document No.", "Sales Header"."No."); 
       IF "Sales Line".FINDSET THEN
       REPEAT
          //your xml port code
       UNTIL "Sales Line".NEXT = 0;
    UNTIL "Sales Header".NEXT = 0;
    
    the outer loop will iterate the whole sales header and the inner loop will iterate over the sales line of that sale order.
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • RockWithNAVRockWithNAV Member Posts: 1,139
    As @vaprog Suggested you need not have to do anything if you are writing a GET Statement.

    P.S - Writing a Code is not a big deal, you may get your desired output by doing many manipulations on the code but one thing i always suggest is to keep in mind is from Performance perspective. Always give priority to LOC(Line of Codes), dont write baseless lines even it's not doing anything to your outcome because one thing that's its doing for sure is it's increasing the time of the compiler to give the desired outcome.
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    As @vaprog Suggested you need not have to do anything if you are writing a GET Statement.

    P.S - Writing a Code is not a big deal, you may get your desired output by doing many manipulations on the code but one thing i always suggest is to keep in mind is from Performance perspective. Always give priority to LOC(Line of Codes), dont write baseless lines even it's not doing anything to your outcome because one thing that's its doing for sure is it's increasing the time of the compiler to give the desired outcome.

    Its always a good and safe approach to initialize/clear the variable! PEACE!
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • ozkanozkan Member Posts: 5
    Hi Everyone, I have my problem solved with this code
    SalesHeader.RESET;
    SalesHeader.SETCURRENTKEY("Document Type","No.");
    SalesHeader.SETRANGE("Document Type",SalesHeader."Document Type"::Order);
    SalesHeader.SETRANGE("No.",SalesHeader."No.");
    IF SalesHeader.FINDSET THEN BEGIN
      REPEAT
        FileName := 'C:\Temp' + SalesHeader."No." + '.xml';
        XmlFile.CREATE(FileName);
        XmlFile.CREATEOUTSTREAM(XmlOutStrm);
        XMLPORT.EXPORT(55667,XmlOutStrm,SalesHeader);
        XmlFile.CLOSE;
        UNTIL SalesHeader.NEXT = 0;
        END;
        MESSAGE(Text001);
    
    on Page Action
    I will be coding a function for this.

    Thanks for everything and keep on Coding :smile:
  • zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    You are welcome. :)
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
  • zaidtariqzaidtariq Member Posts: 52
    Great :smile:
    Best Regards:
    Zaid Tariq
    Dynamics NAV/365 BC Developer at Dynamics 360

    please like / agree / verify my answer, if was helpful.
  • ozkanozkan Member Posts: 5
    Hello again me :) my code is not efficient how can I do it more efficiently.


    Codeunit Code

    OnRun()

    GetFilterNo(SalesHeader : Record "Sales Header")
    SalesHeader.SETRANGE("No.",SalesHeader."No.");
    FileName := 'C:\Temp\' + SalesHeader."No." + '.xml';
    XmlFile.CREATE(FileName);
    XmlFile.CREATEOUTSTREAM(XmlOutStrm);
    XMLPORT.EXPORT(55667,XmlOutStrm,SalesHeader);
    XmlFile.CLOSE;

    Page Action Code

    <Action1000000017> - OnAction()
    CLEAR(ExportToXml);

    ExportToXml.GetFilterNo(Rec);
Sign In or Register to comment.