Codeunit Event to respond MSMQ Recieved message

Hello All,

My Requirement is to transfer data from / to NAV 3.7 to third party application (.Ne). (both direction).

After some R&D, I come to solution to use MSMQ.

Can anybody tell me is there any way in NAV that CodeUnit will get executed as soon as any message posted in MSMQ.

Any event of code unit or something like that.

Please help.

Regards.

Hi Asim,

For this you should use Navision Application Server (NAS) and in Codeunit 1, NASHandler function some code should be written to run your desired codeunit.

For example for Commerce Gateway and ADCS there is code written in NASHandler function of codeunit 1.

Hi Dhan raj,

In forrum on of the post saying following:

The NASHandler function is only called one at start-up, not repeatedly.

Can u please explain me in my case how NASHandler will be used.

Thanks.

As Far as I know, NAS keeps checking after certain interval (Timer setting in NAS, perhaps 30 seconds) and then code in NAS Handler is repeated after that interval.

Generally NAS is used for scheduling tasks which should run without user intervention

I don’t think the above statement is correct.

Hi Dhan Raj,

I will try out your suggestion.

Can you please also tell me any other way to achieve this other than NASHandler.

Regards…

Hi Asim,

there is a way that codeunit will get executed when MSMQ receive message (using navision application server).

First, you have to setup NASHandler for your NAS.

Second thing is to add msmq listener (via MSMQ bus adapter and Navision Comm comp).

This statement is wrong:

"Generally NAS is used for scheduling tasks which should run without user intervention"

With NAS you can design sync or async type of integration. NAS is prefered approach for integration where initation of nav business logic is must.

Wow there is quite a bit of false information in this thread.

For more information about NAS, read ‘w1w1atas.pdf’, which is in the Doc folder on the product CD. When NAS logs in, the system executes the NASHandler function in codeunit 1 once for each time that it logs in. There is no internal timer that checks this function every now and then, it is executed only once, when NAS logs in.

The startup parameter in NAS is used to manage WHAT it does. Typically, you would create a single instance codeunit, that contains some sort of automation object that keeps running, such as a timer object, or a COM component that runs with events. It’s not the NASHandler function that executes, it’s the functionality in the single instance codeunit that is started by the NASHandler function that executes.

The only event based MSMQ functionality that I know of is the NAV MSMQ Busadapter. It works very well once you know what the limitations are, and you know the messages are formatted exactly the right way. There is not a lot of flexibility in it and I don’t like it at all. I’ve used the MSMQ object model directly and tied it to a timer object. That way it’s not event driven, but that was not a requirement for me.

vt011 accurately describes what you need to do to make that work. There’s a help file on the product CD called ‘devguide.chm’ (don’t know if it’s still there on the 2009 disks, but then it should be in the help file) that contains code samples how to do it. I’ve used that myself and I know for a fact that it works.

No that is not correct, NAS does not check codeunit 1 after certain interval. When NAS logs in, the NASHandler function in codeunit 1 is executed once. Depending on the startup parameter, code is then executed, and usually what you’d do is run a single instance codeunit, so that the NASHandler function only needs one call, and the actual functionality is handled elsewhere.

I would say it is incomplete, not wrong. There are many companies that use NAS only for scheduling tasks. As a developer you don’t see those because that comes out of the box with the Job Scheduler. I don’t have any numbers on how NAS is used, so I don’t think anyone can say whether the statement is true or false.

If you need to use NAS, there is no other way than to add code to the NASHandler function that processes your startup parameter.

Hi All,

Sorry for posting wrong info in this thread.

I got little confused at that time. In fact I have used BizTalk (CommerceGateway) and for which NAS was used and it was/ is codeunit BizTalk Appln. Srv. Startup which has a timer
(Commerce Gateway Timer’.Timer) that keeps checking.

And the NAS Handler infact just starts/ initiates BizTalk Appln. Srv. Startup codeunit when the NAS is started.

Thanks denster for clearing the things…

Hi Asim,

I’ve already done this.
You need to configure this:

  1. A private MSMQ listener.
  2. Install and setup the NAS and modify the Codeunit 1 to receive the NAS Startup parameter.

In my case, when Codeunit 1 receives the nas startup parameter, run another Codeunit in which I added the code to read messages from MSMQ.

Codeunit have this global vars:

MQBus Automation ‘Navision MS-Message Queue Bus Adapter’.MSMQBusAdapter
CC2 Automation ‘Navision Communication Component version 2’.CommunicationComponent → WithEvents = Yes.
InMsg Automation ‘Navision Communication Component version 2’.InMessage
InS InStream
Txt Text (100)

Function OnRun:
IF ISCLEAR(MQBus) THEN
CREATE(MQBus);
IF ISCLEAR(CC2) THEN
CREATE(CC2);
CC2.AddBusAdapter(MQBus,1);

MQBus.OpenReceiveQueue(‘NAS1\private$\toNavision’,0,0);

Function CC2::MessageReceived(VAR InMessage : Automation “’’.IDISPATCH”)
InMsg := InMessage;
InS := InMsg.GetStream();
InS.READ(Txt);
InMsg.CommitMessage();

Everytime an external application writes a message to MSMQ, this codeunit reads and then writes in Navision.

Sorry for my bad English and I hope this helps you solve your problem.

Excellent explanation Raul [Y]

For more detailed information:

http://blogs.msdn.com/nav_developer/archive/2008/05/13/comcom-and-the-bus-adapter.aspx
http://blogs.msdn.com/nav_developer/archive/2008/07/15/things-to-remember-with-msmq-busadapter.aspx

One more:

Make sure that you do not receive mor than 4MB with your Message Queue at a time. Otherwise you get very weird and misleading error messages about insufficient memory.

Rather divide your data into chunks and transfer them one by one.

Rahul/Denster

I am going to try
https://blogs.msdn.com/yves/archive/2005/05/20/420439.aspx

plz let us me whether this is correct or not…

And as soon as msg posted in MSMQ will my code unit get executed or not?

Regards,

Asim,

With the code sample I have, when the MSMQ receive a message, this is received in Navision automatically in seconds.

In my case, I use an external application to send a message to MSMQ with a shipment number. In this moment, my Codeunit (with the code i posted before) receive the message with the number and automatically register and print the corresponding shipment.

Since MSMQ receives the message and this is processed in Nav, it took only a few seconds.

On the other hand, the link you posted, helped me too much to make this development.

Regards.

You have everything you need in the links that are already provided. That example looks alright. It seems to be taken out of a real life implementation (just like Raul’s example) so it must work, and looks exactly like the code in the samples that have already been given to you. It’s much easier to use an XMLPort to process your XML though, that one uses MSDOM to parse the XML. Nothing wrong with that, but XMLPorts are much easier.

Like I said before, the MSMQ Busadapter that comes with NAV is event driven, so when the message comes in, the MessageReceived trigger fires (as long as you implement this in a single instance codeunit). All the things you need are in the links in this topic. You have all the ingredients to work on YOUR implementation and make it work.

Hello DenSter/Raul,

Thanks for your great support. Finally my implementation is working as per you guys suggested. Only problem is that whenever my codeunit get executed it shows following error:

This message is for C/AL programmers:The stream cannot find a zero-terminated string.The stream may be invalid, or the (variable or requested) size may be too small.

I am not able to figure out this problem.

Please help.

Regards.

Hello All,

DesSter has moved my another issue to new thread.

Thank you everybody for your support.

Regards.