Options

Using Runmodal in OnInsert Trigger

KSP_NAVKSP_NAV Member Posts: 68
I have a requirement that while inserting Customer (f3) we need to open the Data Template List (Form 8620) and user will select the required Data template for updating the Customer Fields based on the data template setup.

So i inserted the below code in Customer table oninsert trigger

RecRef.GETTABLE(Rec);
TemplateMgt.UpdateFromTemplateSelection(RecRef);

where RecRef is a recordref global variable and TemplateMgt is golbal variable for Codeunit 8612 (Template Management).

when i try to insert new customer record, the Data template list is opening and after selecting the required template, it is showing error as "Customer No. does not exists" etc... I have not done any other customizations other than this 2 lines of code.

Has anyone already worked on this type of requirement? Your valuable suggetsion are highly appreciated.

Thanks in advance.

Answers

  • Options
    kitikkitik Member Posts: 230
    I assume you placed your code at the end of the OnInsert trigger.

    Try using COMMIT just before your code, so it works like the standard: first the customer is created (and commited) and then a template is applied.

    Salut!
    Laura Nicolàs
    Laura Nicolàs
    Author of the book Implementing Dynamics NAV 2013
    Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
  • Options
    KSP_NAVKSP_NAV Member Posts: 68
    Hi kitik,

    Thanks for your reply.

    Already, I tried using COMMIT before my codes but the result is same :( (still getting the same errors)

    Yes, i have placed my coding at the end of Oninsert trigger.
  • Options
    einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    Take a look at the online help.
    This trigger executes before default insert behavior occurs. The new record is not inserted if an error occurs in the trigger code.

    COMMIT at that point is definitely no good programming. Wouldn't it be easier if you create a contact first and than use the function Create As -> Customer? In this way you can apply a template by standard.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • Options
    kitikkitik Member Posts: 230
    This trigger executes before default insert behavior occurs. The new record is not inserted if an error occurs in the trigger code.

    That's the reason why he is getting the error "The customer does not exist" and that's the reason why I proposed to use COMMIT. Only if the new code was placed at the end of the trigger, so if an error occurs before this, the transaction is not commited.

    Salut!
    Laura Nicolàs
    Laura Nicolàs
    Author of the book Implementing Dynamics NAV 2013
    Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
  • Options
    KSP_NAVKSP_NAV Member Posts: 68
    einsTeIn.NET - yes, you are correct. But, is there any other possibilities to acheive my requirement? Because the Customer want to select the Data Template while inserting the new record (F3).
  • Options
    BeliasBelias Member Posts: 2,998
    the onvalidate trigger of "no." field will probably do the trick -never tried- (be sure that you place your code at the end of standard code. in this way, the normal "no." field creation will already be executed)

    P.S.: einstein solution IS the easiest way to proceed, anyway
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • Options
    kitikkitik Member Posts: 230
    einsTeIn.NET you were right.
    I tried to use COMMIT on the OnInsert trigger... and it is not working. The transaction does not get commited because an error occurs on the trigger... even if it occurs AFTER the commit sentence.

    I thought COMMIT worked always... so thanks for the tip!

    Salut!
    Laura Nicolàs
    Laura Nicolàs
    Author of the book Implementing Dynamics NAV 2013
    Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
  • Options
    KSP_NAVKSP_NAV Member Posts: 68
    The "Onvalidate" trigger would not help in this case since we are creating new customers (and not validating).

    As you know, in Customer Card we can apply template using Functions -> Apply Template. In my case, customer doesn't want to click (and even do any other actions) the function to select templates :( . Customer needs this template has to be listed after inserting the Customer No. (Customer No. are inserted using No. Series) and then they will select the required one.

    Thats why i asked is there any other possibilities to complete this requirement.
  • Options
    BeliasBelias Member Posts: 2,998
    :-k
    well, i think that if the user presses f3, then arrowdown to validate the no. and this will pop up the template list it would be not so bad...am i missing something in your post?
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • Options
    KSP_NAVKSP_NAV Member Posts: 68
    Hmm.. the Validate trigger will not get executed. And therefore i tried in "OnInsert" but this is also not possible as we discussed above. Again, any solutions or thoughts ](*,) ?
  • Options
    BeliasBelias Member Posts: 2,998
    if the user presses f3 and then arrowdown, the onvalidate IS executed, no matter what.
    am i misunderstanding something in your post? :-k
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • Options
    kitikkitik Member Posts: 230
    Try to do it the other way around: first select the template, then create the customer.

    On the template form you have the button Functions -> Create Instance.
    Take a look at the code to see how it works.

    Salut!
    Laura Nicolàs
    Laura Nicolàs
    Author of the book Implementing Dynamics NAV 2013
    Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
  • Options
    KSP_NAVKSP_NAV Member Posts: 68
    The Functions -> Create Instance works as similar like Functions -> Apply template in Customer Card. As i said earlier, this cannot be used.

    Also, the Customer No's are inserted using No. Series (Default and NOT Manual) and therefore until some validation performed in "No." field, the "OnValidate" trigger will not be executed. As the requirement is while inserting Customer, the template has to be listed and from which user will select the required one and the customer fields has to updated accordingly.

    Any other workarounds?
  • Options
    kitikkitik Member Posts: 230
    The Function "Create Instance" creates the customer using NoSeries, so this is not a problem.

    Create a function on the customer card called "create cust. from template", accessible from any free key combination. Tell users not to use the F3, but the new key you defined.

    From that function write code to open the template form, let the user select the template and then call the function used on the standard "Create Instance" option.

    Salut!
    Laura Nicolàs
    Laura Nicolàs
    Author of the book Implementing Dynamics NAV 2013
    Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
  • Options
    KSP_NAVKSP_NAV Member Posts: 68
    Thanks for your reply.

    I am aware of this function but do you think changing the key in the Customer Card and asking the user not to use F3 for inserting record is good practice?
  • Options
    kitikkitik Member Posts: 230
    I did not say that the ShortCutkey for creating a customer should be changed.
    I proposed to create a new fucntion with a new ShortCutKey.

    Salut!
    Laura Nicolàs
    Laura Nicolàs
    Author of the book Implementing Dynamics NAV 2013
    Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
  • Options
    FDickschatFDickschat Member Posts: 380
    You need to code it in the OnInsertTrigger of the Form.

    Do something like
    Codeunit.GetCustTemplate(Rec)

    In the function GetCustTemplate have the Cust Parameter by VAR. Open the Template window and have the user select one. Update the fields of the parameter. You do not even need to run a modify on the parameter.
    Frank Dickschat
    FD Consulting
  • Options
    KSP_NAVKSP_NAV Member Posts: 68
    Frank - I tried as you suggested but it is not working :( . It displays error stating that "The instance already exists in table 18 Customer". ](*,)
  • Options
    krikikriki Member, Moderator Posts: 9,090
    FDickschat wrote:
    You need to code it in the OnInsertTrigger of the Form.

    Do something like
    Codeunit.GetCustTemplate(Rec)

    In the function GetCustTemplate have the Cust Parameter by VAR. Open the Template window and have the user select one. Update the fields of the parameter. You do not even need to run a modify on the parameter.

    Give it a try with the OnNewRecord-trigger of the form.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • Options
    KSP_NAVKSP_NAV Member Posts: 68
    I tried already in the OnNewRecord trigger and the result is the same. But, it displayed different error like "The Number Series has not been set for table 18" because at this point we will not have Customer record initiated.
  • Options
    krikikriki Member, Moderator Posts: 9,090
    You can initiate it yourself by copying some code from the OnInsert-trigger.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • Options
    KSP_NAVKSP_NAV Member Posts: 68
    If i am correct, we cannot make any transaction here. i.e. i think, it will display error as "you cannot make any changes until transaction started" something like that if we are modifying using "onnewrecord".
  • Options
    krikikriki Member, Moderator Posts: 9,090
    KSP_NAV wrote:
    If i am correct, we cannot make any transaction here. i.e. i think, it will display error as "you cannot make any changes until transaction started" something like that if we are modifying using "onnewrecord".
    :oops: Forgot that one. :oops:
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • Options
    dinhdinh Member Posts: 4
    This might not be an elegant solution, but if nothing else can do the job then I would suggest you use the OnTimer trigger. It works for me :lol:

    Form - OnInsertRecord(BelowxRec : Boolean) : Boolean
    RecInserted := TRUE;

    Form - OnTimer()
    IF RecInserted AND ("No." <> '') THEN BEGIN
    RecRef.GETTABLE(Rec);
    TemplateMgt.UpdateFromTemplateSelection(RecRef);
    InsertRec := FALSE;
    END;
  • Options
    krikikriki Member, Moderator Posts: 9,090
    A more beautiful solution:
    Create a menuitem under Button Customer with Shortcut F3 (to intercept the F3 key). In that function you ask for the template and create a new customer-record and have the customer card form position on that new record before giving back control to the user.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • Options
    FDickschatFDickschat Member Posts: 380
    My solution works but is not based on CU8612 but on my own CU. The main issue is the modify.

    The problem with CU8612 and my solution is that CU8612 modifies the RecRef assuming that the record already exists. But in the OnNew Trigger of the form it does not.

    Just to try comment the lien RecRef.modify. If it works create a Fu in CU8612 with a parameter NoModify and change the code so that the RecRef.modify is not executed if NoModify is true. As the RecRef is transferred to CU8612 by VAR and also in all Fu of CU8612 this should work.
    Frank Dickschat
    FD Consulting
  • Options
    KSP_NAVKSP_NAV Member Posts: 68
    Great Idea Dinh and Kriki =D> .

    Dinh - The only problem i noticed that the fields in the customer are not updated immediately. Refreshing problem is there otherwise works well. If we go out of form and then reopen it again, then the values are updated else NOT.

    Kriki - You idea works very well.

    Thanks a lot guys for your answers. Have a nice weekend.
  • Options
    dinhdinh Member Posts: 4
    I totally agree that Kriki provided a much better solution. As I said before, using OnTimer is not very elegant. It just came up to my mind because I often use OnTimer to synchronize data between multiple grids on one Form.
    With my solution you can just add a CurrForm.UPDATE:

    Form - OnTimer()
    IF RecInserted AND ("No." <> '') THEN BEGIN
    RecRef.GETTABLE(Rec);
    TemplateMgt.UpdateFromTemplateSelection(RecRef);
    InsertRec := FALSE;
    CurrForm.UPDATE;
    END;
  • Options
    KSP_NAVKSP_NAV Member Posts: 68
    Hi Dinh, I just wanted to highlight you the refreshing problem with your previous code and already i tried with Currform.update too and it works like a charm =D> . And, i too agree that using "ontimer" is not an elegant way and so i opt Kriki's way.

    Thanks a lot for your help :D .
Sign In or Register to comment.