Using Runmodal in OnInsert Trigger
KSP_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.
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.
0
Answers
-
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àsLaura Nicolàs
Author of the book Implementing Dynamics NAV 2013
Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.0 -
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.0 -
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."0 -
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àsLaura Nicolàs
Author of the book Implementing Dynamics NAV 2013
Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.0 -
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).0
-
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, anyway0 -
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àsLaura Nicolàs
Author of the book Implementing Dynamics NAV 2013
Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.0 -
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.0 -
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 ](*,) ?0
-
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àsLaura Nicolàs
Author of the book Implementing Dynamics NAV 2013
Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.0 -
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?0 -
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àsLaura Nicolàs
Author of the book Implementing Dynamics NAV 2013
Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.0 -
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?0 -
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àsLaura Nicolàs
Author of the book Implementing Dynamics NAV 2013
Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.0 -
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 Consulting0 -
Frank - I tried as you suggested but it is not working
. It displays error stating that "The instance already exists in table 18 Customer". ](*,) 0 -
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!0 -
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.0
-
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!0 -
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".0
-
:oops: Forgot that one. :oops: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".Regards,Alain Krikilion
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!0 -
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

Form - OnInsertRecord(BelowxRec : Boolean) : Boolean
RecInserted := TRUE;
Form - OnTimer()
IF RecInserted AND ("No." <> '') THEN BEGIN
RecRef.GETTABLE(Rec);
TemplateMgt.UpdateFromTemplateSelection(RecRef);
InsertRec := FALSE;
END;0 -
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!0 -
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 Consulting0 -
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.0 -
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;0 -
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
. 0
Categories
- All Categories
- 75 General
- 75 Announcements
- 66.7K Microsoft Dynamics NAV
- 18.8K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 610 NAV Courses, Exams & Certification
- 1.9K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 251 Dynamics CRM
- 103 Dynamics GP
- 6 Dynamics SL
- 1.5K Other
- 991 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 28 Design Patterns (General & Best Practices)
- Architectural Patterns
- 9 Design Patterns
- 4 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1K General Chat
- 1.6K Website
- 77 Testing
- 1.2K Download section
- 23 How Tos section
- 249 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions
