Hello! I want to make a codeunit, after pushing a button. How can I do it? At first I only want to write this codeunit a Hello World message! Please help me!
Hi, you can call a codeunit in the Button.OnPush trigger (when you highlight the button, press F9 to get to the Code behind the Form).
First you have to create your Codeunit (e.g. Codeunit 50000 MyTestcodeunit), and there you create a new Function, where you put your code (e.g. MESSAGE(‘Hello World!’).
You then create a Variable (either local or global) of DataType Codeunit and Subtype MyTestcodeunit in the Form (or the Button of the Form), and call your Function in the OnPush Trigger. Alternatively, you can program your Message in the OnPush Trigger directly.
Yeah, thank you, I know this solution. But my quetion is, that can I do, directly a codeunit by push the button. So creating the codeunit in the lines of code at the button. I hope you understand me. It’s important for my lately excercise.
I think it is not possible.
Hi Szabo and Welcome to Dynamics User Group,
My understanding of your question: You want to create a codeunit by pressing a button in a form. Lets say you don’t have a codeunit in your database by ID 80001 and you want to create on pressing a button in form.
If that is the case, YES this can be done. For this, you should have good experience with nav development and be comfortable in working with TEST file of objects.
For example the following code on the button will create a entry in Object Designer for your codeunit, but this is not complete code:
where Object is Recode with subtype as Object.
On www.Mibuso.com in download section, you can find Dataport Generator, have a look on that that can give you insight into your task. I think even on this site (DUG), there was a discussion and some relevant code, though I couldn’t find it now.
the above logic is OK for
I assume this codeunit creation and writing “hello world” is purely for learning / exploring purpose. Might be user wants to know whether this can be really done or not in NAV. And if user wants to display “Hello World” message from that codeunit, then this can be done, you might call it Hard coding or static purpose of code. But I just want to emphasize that this can be done.
In addition to this, objects can be deleted also from a form. For how to do that, refer following link:
@ Dhan Raj Bansal:
After adding the Codeunit, Can we design or run the codeunit?
Answer is NO, and that is why I wrote following also in that post.
“For example the following code on the button will create a entry in Object Designer for your codeunit, but this is not complete code:”
Agreed… can be delete
Okay, now I can create a codeunit with a button, but how can I write code in that codeunit? For example Message(‘Hello World’);
Can you be able to design or run the created codeunit manually?
No, it writes, that the codeunit is empty. But it isn’t good for me, if I write code in the codeunit, after I create it, I have to write the code into it, when I create with pushing the button at the same time.
I wrote following in my previous post:
“On www.Mibuso.com in download section, you can find Dataport Generator, have a look on that that can give you insight into your task. I think even on this site (DUG), there was a discussion and some relevant code, though I couldn’t find it now.”
Did you study that Dataport generator, if not then following is the link to download that file:
Thanks alot Dhan Raj for providing the link…
i thought to try something like this tonight [;)]
Maybe I’m stupid, but I don’t how, how I can do this with Dataport Generator. It is the very begining of my excercise, but it is an important point. I try everything, but I can’t write code to that codeunit, what I make. Of course the excercies isn’t that write a Hello World codeunit, I just want to try write anything in that codeunit. But I don’t know how this Dataport Generator helps me.
If you a beginner in NAV Programming, then you should not try doing this. This is a bit complicated. But if your intention is just to create a codeunit from scratch fro “Hello World” then that is very simple. See the following link by MVP David Singletone:
But if you have are very good in C/AL, then please go ahead with this task of creating codeunit from form’s button.