Microsoft Dynamics NAV: Copy Table Data from one Company to another Company

In Microsoft Dynamics NAV you can setup separate distinct companies. Unless a table is setup to share data across all companies, by setting the DataPerCompany property to No, each company’s data is separate and distinct from the other. In many cases it may be desirable to have tables that contain the same values across companies. Due to validation and performance considerations the setting of the DataPerCompany property may not be an option.

Fortunately in C/SIDE we can use the CHANGECOMPANY function to specify the company that a record variable references. Using this method we can easily copy data from one company to the other. It is simple enough to explicitly define two variables for a particular record and write a few lines of code to copy all of the data from one company to the other. I am all about reuse and prefer not modifying code for each table or scenario. Fortunately in C/SIDE we can make use of the RecordRef variable type. This allows us to dynamically specify the tables to reference and create a neat little function:

CopyTableFromTo(FromCompanyName : Text[30];ToCompanyName : Text[30];TableNo : Integer)

IF GUIALLOWED THEN BEGIN
  Window.OPEN('#1###############\' +
              '@2@@@@@@@@@@@@@@@');
END;

// Open source table
FromTableRef.OPEN(TableNo, FALSE, FromCompanyName);
IF GUIALLOWED THEN BEGIN
  Window.UPDATE(1, FromTableRef.NAME);
END;
// Open destination table
ToTableRef.OPEN(TableNo, FALSE, ToCompanyName);
RecCount := FromTableRef.COUNT;

// Loop through the fields
IF FromTableRef.FINDFIRST THEN REPEAT
  RecNo += 1;
  IF GUIALLOWED THEN BEGIN
        Window.UPDATE(2,ROUND(RecNo/RecCount * 10000, 1));
  END;
  ToTableRef.INIT;
  FOR i := 1 TO FromTableRef.FIELDCOUNT DO BEGIN
    FromFieldRef := FromTableRef.FIELDINDEX(i);
    ToFieldRef := ToTableRef.FIELDINDEX(i);
    //TODO: Check field types
    ToFieldRef.VALUE := FromFieldRef.VALUE;
  END;
  IF NOT ToTableRef.INSERT THEN ToTableRef.MODIFY;
UNTIL FromTableRef.NEXT = 0;

ToTableRef.CLOSE;
FromTableRef.CLOSE;

IF GUIALLOWED THEN BEGIN
  Window.CLOSE;
END;

With the variables:


This function can be use in ways similar to:

//This is intended to be called in a function:
//CopyFromSource(CompanyName : Text[30];TableNo : Integer)

// Verify company is valid
Company.GET('CRONUS USA, Inc.');
// Set the table number; in this case Country/Region
TableNo := 9;

Company.SETFILTER(Name, '<>%1', 'CRONUS USA, Inc.');
IF Company.FINDSET(FALSE, FALSE) THEN REPEAT
  CopyTableFromTo(CompanyName, Company.Name, TableNo);
UNTIL Company.NEXT = 0;

Note: This function does not trigger and code (or data) validation. When using the CHANGECOMPANY function if you run validation code on a variable for Company B in Company A, it will modify the record in Company B, but it will run the trigger in Company A.

Change Internet Explorer (IE) Security Settings to Enable JavaScript

As discussed in a previous post some of the Internet Explorer (IE) security settings can be frustrating at times. To enable JavaScript open the Tools menu (Alt + X) and click Options.


Next, click on the Security Tab and click on the Internet (the image of the globe) and then click the Custom Level button.



Afterwards, scroll to the Scripting section and click Enable right below the Active scripting option.


Finally, click Ok on to close each of the windows.

Change Internet Explorer (IE) Security Settings to Enable File Downloads


The security restrictions in Internet Explorer may be understandable but at times they can be extremely frustrating and tedious to change. One such setting is the blocking of File Downloads. To change the File Downloads setting open the Tools menu (Alt + X) and click Internet Options. 


Next, click on the Security Tab and click on the Internet (the image of the globe) and then click the Custom Level button.


Afterwards, scroll to the Downloads section and click Enable right below the File download option. 


 Finally, click Ok on to close each of the windows.


Microsoft Dynamics NAV: Set the Color Style of a List Page Row


In Microsoft Dynamics NAV a List page displays content from a table in a list format. It is often desired to differentiate the data in the list based upon a record value. Fortunately this can be accomplished by setting the Style Property of a Field type on the page. The text in a field will be displayed according to the Style Property value selected. The Style can be set to one of the following values and be formatted as:

Standard - Standard
StandardAccent - Blue
Strong  - Bold
Strong Accent - Blue + Bold
Attention - Red + Italic
AttentionAccent - Blue + Italic
Favorable - Bold + Green
Unfavorable - Bold + Italic + Red 
Ambiguous - Yellow
Subordinate - Grey

Setting the Style property fixes the style of the field contents to the option selected. What if you want to set the style dynamically? That can be done with the StyleExpr property. The StyleExpr property sets whether the format that is specified in the Style Property is applied to text in a field. You can set the StyleExpr value to the name of a variable or a Style Property text (in apostrophes) 'Strong'.



Setting the StyleExpr Property to a variable allows it to be set dynamically by code. For example, create a function SetStyle that returns a Text value according to the desired logic:


Then set the variable after you retrieve the record, the formatting will be applied to each record.


Microsoft Dynamics NAV: Debugging the NAV Application Server (NAS)

The NAV Application Server (NAS), often called the Classic Server, runs Dynamics NAV code through a service and there is no graphical user interface (GUI).  Without the GUI, there are some limitations on the C/AL code that can be executed through the NAS.  C/AL Form and Dataport functions as well as Dialog system functions cannot be executed through the NAS.   Without the a GUI, a different approach needs to be taken to debug code.  It isn't always enough to step through the code as it runs in the NAV client because the resulting code may not always execute the same through the NAS.

Debugging the NAS is a lot easier than many may think.  To debug the NAS, first verify that a developers license exists in the Application Server folder.  Then start the NAS from the command line using the following syntax (this is different than installing the NAS as a service): 

"C:\Program Files (x86)\Microsoft Dynamics NAV\60\Application Server\nassql.exe" Debug,Breakpoints=C:\Temp\Breakpoints.xml,appservername=JOBQUEUE,Servername=localhost,Database=Demo Database NAV (6-0),Company="CRONUS USA, Inc.",startupparameter=JOBQUEUE

Note: Replace the parameter values in the above statement with the values for your environment.  

With the Debug parameter specified the debugger will open and you can set breakpoints and step through the code. The Breakpoints parameter is used to specify the file that is used to load a predefined set of breakpoints.  This makes debugging a lot easier as you do not need to step through every line of code to find your desired breaks.  If the breakpoints file does not exist it will be created when you close the application server session, if you had set any breakpoints.

Note: The Breakpoints file is nothing more than an XML file that contains the breakpoints location:

<?xml version="1.0" encoding="utf-8"?>
<BreakpointList>
  <Object Type="Codeunit" ID="449" Name="Job Queue Start Codeunit">
    <Breakpoint>
      <TriggerName>HasWinPermission</TriggerName>
      <CodeNo>2</CodeNo>
      <TriggerLine>8</TriggerLine>
      <Enabled>Yes</Enabled>
    </Breakpoint>
  </Object>
</BreakpointList>

You can read more on the breakpoints file on MSDN.

Reading Stuff

Information in this document subject to change without notice.
All Software source code published is for demonstration and knowledge sharing purposes only. The Code is supplied "as is" without warranty as to result, performance or merchantability. Use at your own risk.
The opinions expressed herein are the opinions of the author and do not reflect those of any other entity.