I was testing a XML-port used as a parameter in a function of a codeunit that was published as a webservice (I hope this is clear!).
Let me rephrase it anyway:
I have a codeunit published as a webservice. In that codeunit I have a function and that function has a XML-port as parameter.
So when I put this link in a browser http://localhost:7047/DynamicsNAV/WS/The CompanyName/Codeunit/TheCodeunit, I should receive the reply about the format of the XML-port. But I got this error:
<faultstring xml:lang="it-IT">You must choose a company before you can use the XXXXXXX Setup table.</faultstring>
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">You must choose a company before you can use the XXXXXXX Setup table.</string>
Wow, why do I have this error now? My previous test didn’t show any error.
Reading better the error, it means I need to select a company first, but I DID select a company because it is in the URL I put in the browser.
BUT I remembered I just put this new command in the OnInitXMLport()-trigger: XXXXXXSetup.GET();
Commenting the command made the error go away!
Conclusion: the OnInitXMLport()-trigger is run when I just put the above link in a brower even if I DON’T really call the webservice. And the trigger is run BEFORE any company is selected!
I had this problem on NAV2009R2 and for writing this blog I also wanted to know if also in NAV2013 I had this problem. And it has!
The NAV2013 help on the OnInitXMLport()-trigger states this: "Executed when the XMLport is loaded and before any table views and filters are set.". So no nothing about executing this before or after the company is selected.
I don’t know if that is a bug, but the workaround is quite easy: Put the code I the OnPreXMLport-trigger.
… Actually I think it is a bug. It has no sense executing C/AL code while NOT being in some company. So I reported it to Microsoft.