Event on new sales line

Hey Dynamics users,

i'm working on a customization, that needs to be implemented using events. to keep it short, i have a list of items that has a relation to one item. For example, itemA has a relation to ItemB etc.  on a sales order, when a choose ItemA, i also want Item be to appear below it. I'm am a litte bit in doubt on which event i need to subscribe. OnAfterInsert or OnAfterValidate.  I hope someone will guide me to a solution. Thank you.

 

  • lynix_mo

    Hey Dynamics users,

    i'm working on a customization, that needs to be implemented using events. to keep it short, i have a list of items that has a relation to one item. For example, itemA has a relation to ItemB etc.  on a sales order, when a choose ItemA, i also want Item be to appear below it. I'm am a litte bit in doubt on which event i need to subscribe. OnAfterInsert or OnAfterValidate.  I hope someone will guide me to a solution. Thank you.

     

     

    Try on No. - OnValidate()

  • Hi Lynix,
    You should subscribe to OnAfterModify.

    The OnAfterInsert is executed after the Sales Line has been inserted, which happens when the primary key has been initiated. Even if no Item No. has been entered. So will not work.

    The OnAfterValidate for No. would work, except if there should be a relation between ItemA and ItemB Quantity , then you would also subscribe to OnAfterValidate for Quantity.

    If you do it by subscribing to OnAfterModify, then you can do it all, and ensure to update/add ItemB line , in one step.

    Remember also to have code that removes the ItemB line, if ItemA line is deleted (or Item No. just changed to ItemC).
  • In reply to Erik P. Ernst:

    Hey Erik, thanks for the advice. I actually did it with onAfterInsert. now i can insert ItemB and so on. But now my issue is when updating the values. if i update "qty to ship" field of itemA, ItemB does not update. I spent alot of time with it but cannot find a solution. my code for updating looks like this:

    SalesLine2.SETRANGE("Document No.",Rec."Document No.");
    SalesLine2.SETRANGE("Document Type",Rec."Document Type");
    SalesLine2.SETRANGE(LineRef,Rec."Line No.");
    IF SalesLine2.FINDSET THEN
    REPEAT
    salesLine2."Qty. to Ship" := Rec."Qty. to Ship";
    UNTIL SalesLine2.NEXT = 0;

    i have multiple Items that has a relation to ItemA. When i change the value of itemA, other lines does not change. I subscribet to OnAfterModify event. Hope somebody have a solution of a code example on how to solve this issue.

    NB: LineRef is a field to reference to ItemA.
  • In reply to lynix_mo:

    Hi Lynix,

    lynix_mo
    I actually did it with onAfterInsert. now i can insert ItemB and so on.

    If you had put your code on onAfterModifiy instead, then you would not have had this issue.

    lynix_mo
    But now my issue is when updating the values. if i update "qty to ship" field of itemA, ItemB does not update. I spent alot of time with it but cannot find a solution. my code for updating looks like this:

    SalesLine2.SETRANGE("Document No.",Rec."Document No.");
    SalesLine2.SETRANGE("Document Type",Rec."Document Type");
    SalesLine2.SETRANGE(LineRef,Rec."Line No.");
    IF SalesLine2.FINDSET THEN
    REPEAT
    salesLine2."Qty. to Ship" := Rec."Qty. to Ship";
    UNTIL SalesLine2.NEXT = 0;

    You need a SalesLine2.MODIFY; If you also want to update

    And would you not need a similar function subscribing to OnAfterVerify on Quanity to update any quantity changes? A single subscription to OnAfterModify could handle them all.

    lynix_mo

    i have multiple Items that has a relation to ItemA. When i change the value of itemA, other lines does not change. I subscribet to OnAfterModify event. Hope somebody have a solution of a code example on how to solve this issue.

    NB: LineRef is a field to reference to ItemA.

    Why use a new field for this? Field 80 "Attached to Line No." is already there. You would just need to make sure it's an item line. It's also used for Extended Text lines.

Related