Copy Field from sales line to header

lavanyaballurgilavanyaballurgi Member Posts: 235
Hey Guys.. its Sunday & I am working to complete my assignment. well here is my issue
I want to concatenate data from first 5 lines of text field named "Bill Detail"(customized field) present in Sales line .. into a text field of sales header named "invnarration". My code works but it doesnt update the last line's data in header field. why?
Here is my code

lin_counter := 0;
lrc_SalesHeader.GET("Document Type", "Document No.");
lrc_SalesLine.SETRANGE("Document Type", "Document Type");
lrc_SalesLine.SETRANGE("Document No.", "Document No.");
lin_SalesLineCounter := lrc_SalesLine.COUNT;
IF lrc_SalesLine.FINDSET THEN
REPEAT
lin_counter += 1;
IF lin_counter < lin_SalesLineCounter THEN
ltx_Narration += lrc_SalesLine."Bill Detail" + ' '
ELSE
ltx_Narration += lrc_SalesLine."Bill Detail";
UNTIL (lrc_SalesLine.NEXT = 0) OR (lin_counter = 6);

MESSAGE(ltx_Narration);// just to check ltx_narration variable.

lrc_SalesHeader.InvNarration := ltx_Narration;
lrc_SalesHeader.MODIFY(FALSE);

Answers

  • kinekine Member Posts: 12,562
    There are few tips for your code:

    1) Why you are using the lin_SalesLineCounter at all?
    2) Why you are getting the SalesHeader, when you already have it in Rec?

    Cleared code:
    lin_counter := 0;
    lrc_SalesLine.SETRANGE("Document Type", "Document Type");
    lrc_SalesLine.SETRANGE("Document No.", "Document No.");
    IF lrc_SalesLine.FINDSET THEN
    REPEAT
      lin_counter += 1;
      IF ltx_Narration <> '' THEN
        ltx_Narration := ltx_Narration + ' ';
      ltx_Narration := ltx_Narration  + lrc_SalesLine."Bill Detail";
    UNTIL (lrc_SalesLine.NEXT = 0) OR (lin_counter = 6);
    
    MESSAGE(ltx_Narration);// just to check ltx_narration variable.
    
    lrc_SalesHeader.InvNarration := ltx_Narration;
    lrc_SalesHeader.MODIFY(FALSE);
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • lavanyaballurgilavanyaballurgi Member Posts: 235
    Well your tips were valid i shud not use extra variable when i cab get table in .Rec.
    By the way using you code I am getting this error

    Microsoft Dynamics NAV Classic
    The Sales Header does not exist.

    Identification fields and values:

    Document Type='Quote',No.=''



    OK
  • kinekine Member Posts: 12,562
    Of course, I made a mistake... look at last two lines. These must be over Rec too. But I expected that you can notice that... sorry, it was not intended...
    InvNarration := ltx_Narration;
    MODIFY(FALSE);
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • lavanyaballurgilavanyaballurgi Member Posts: 235
    I noticed that but then just putting Invnarration will give me an error that its an unknown variable. No?
  • lavanyaballurgilavanyaballurgi Member Posts: 235
    I am still learning about forms... so please dont mind if I ask stupid questions...
  • lavanyaballurgilavanyaballurgi Member Posts: 235
    Or may be I placed the code @ wrong place.... I put it on "Bill Details" - OnValidate().
    right?
  • kinekine Member Posts: 12,562
    It all depends. Depends on when you want to update the value. Of course, there are wrong places and correct places. But this must be decided on whole logic. Of course, question is, if the code should be on form or rather on table, because you want to update the data regardless form you use. And another question is, if you want to update the data in header when line is changed or somewhen else...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • lavanyaballurgilavanyaballurgi Member Posts: 235
    That was a good reply :)
    well I want to update the salesheader.invnarration field everytime someone enter data in salesline."Bill detail".
    Shall I put the code on "Bill Detail" - OnValidate? Or you can propose me a better location?
  • kinekine Member Posts: 12,562
    There is one rule: if possible, not modify other tables from OnValidate trigger. Because it is separate transaction from OnModify, and if OnModify will fail, the modified data in other tables will be still there (commited). Thus it could lead potentially to inconsistency. Of course, if you will call the code from Lines, the Rec will be the line, not header and you need to modify the code, or just call function doing that over header record (add function to header, call it from the line). Of course, you need to think about one thing- if you are reading the lines, you do not see the last change you did, which triggered the change (it is not written into databse yet). Thus you need to go around it somehow... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • lavanyaballurgilavanyaballurgi Member Posts: 235
    Ok... that was a good learning ... And in Header where should I call the function? Invnarration - OnValidate() ?
    If I want to update Invnarration before posting the invoice so where should I put the code? Codeunit 80 or 81?
  • lavanyaballurgilavanyaballurgi Member Posts: 235
    Ok I found a workaround. I created a ASSISTBUTTON on InvNarration field. and put my code there. It works fine but I need to instruct the user to click on assist button before posting invoice. Cant i put this code somewhere where this will update the Invnarration field automatically before posting?
  • kinekine Member Posts: 12,562
    You canput it e.g. into Release function of the document. It is called each time the document is released, e.g. before posting.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • lavanyaballurgilavanyaballurgi Member Posts: 235
    But Invoice can be posted directly without need of releasing. Basically all sales invoices are posted from invoice form & not sales order form.
    Why its not a good idea to put the codes in codeunit which runs after we push "post" button?
  • kinekine Member Posts: 12,562
    Release is done automatically when posting the document (if it is not already released manually), before posting rutine start... ;-) just look into the codeunit... 8)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • lavanyaballurgilavanyaballurgi Member Posts: 235
    Well yes... I could see now that all invoice & orders are released before its posted... and I kept my code in release codeunit & it works =D>
Sign In or Register to comment.