Friday, October 20, 2017

Validate Email Address in NAV using RegEx

business-man-1002781_1920

Last week I was working on a shipment notification project where I need to send Email Notification of the shipment and one thing we need to check is the email address is valid or not. In Standard NAV the SMTP mail codeunit or Mail Management codeunit has a function to checkValidEmailAddress but it does a very basic validation and it did not meet our needs so I have written a new function to validate the email address.

I have used  RegEx( Regular Expression) and According to Wikipedia

“A regular expression, regex or regexp[1] (sometimes called a rational expression)[2][3] is, in theoretical computer science and formal language theory, a sequence of characters that define a search pattern. Usually this pattern is then used by string searching algorithms for "find" or "find and replace" operations on strings.”

Since we have the access the functions of RegEx function using DotNet, I went a ahead and wrote the following function to validate the email address using RegEx.

In our case we could store multiple email addresses in a field, so I have used String Array to parse and validate the email address.

PROCEDURE ValidateEmailAddresses@1000000008(EmailAddresses@1000000000 : Text);
     VAR
       RegEx@1000000004 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.RegularExpressions.Regex";
       DotNetString@1000000003 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.String";
       EmailAddrArray@1000000002 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array";
       Convert@1000000001 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert";
       I@1000000005 : Integer;
       EmailAddress@1000000006 : Text;
     BEGIN
       EmailAddresses := CONVERTSTR(EmailAddresses,',',';');
       EmailAddresses := DELCHR(EmailAddresses,'<>');
       EmailAddrArray := RegEx.Split(EmailAddresses,';');
       FOR I := 1 TO EmailAddrArray.GetLength(0) DO BEGIN
         EmailAddress := EmailAddrArray.GetValue(I-1);
         IF NOT RegEx.IsMatch
               (EmailAddress,'^[\w!#$%&*+\-/=?\^_`{|}~]+(\.[\w!#$%&*+\-/=?\^_`{|}~]+)*@((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$') THEN
           ERROR(Text106);
       END;
     END;
I got the above email address regular expression pattern from the below link, so please visit the below to know what validation it does.
https://www.rhyous.com/2010/06/15/regular-expressions-in-cincluding-a-new-comprehensive-email-pattern/

If you have any other tips or suggestions , please do share them in the comments below.


Share:

Wednesday, October 18, 2017

How to Schedule NAV Services to Restart

clock-2174116_1920

Recently I was asked if there is a way to schedule a NAV service to restart at a specific time, and for that I have used a small PowerShell script which I have scheduled using windows task scheduler.

Below is the script and steps for that

This example is based on NAV 2013 but the same code can be used for other versions

The below  PowerShell script is used to restart NAV Services, since in our case there are more then one NAV instance on the server, I have used a for loop to find all the available/running instances and then filter those instances using like statement, in this case I am only restarting NAV Instances with the word “Test” in their name. You can replace this with any other word or remove that condition.

Import-Module 'C:\Program Files\Microsoft Dynamics NAV\70\Service\NavAdminTool.ps1' -WarningAction SilentlyContinue | Out-Null
$Instances = Get-NAVServerInstance
for ($i = 0; $i -le $Instances.Count - 1; $i++) {    
     if ($Instances[$i].State -eq 'Running' -And $Instances[$i].Name -like '*Test*') {       
         Restart-Service $Instances[$i].Name
         #Write-Output $Instances[$i].Name
     }
}

Save the above script in a file and store in a location then we need to create a Task Scheduler to execute the above script.

Create a new task using the option shown in Fig 1, then choose the name, and Trigger when you want to run, under the Actions tab choose Start a Program  (Fig 2) and for the program use Powerhsell.exe and specify the ExecutionPolicy ByPass and path for the script in arguments

image

Fig 1


image

Fig 2

image

Fig 3


This will create a task in windows scheduler to restart the services, you can also export the task as .xml and import into another server.

If you have any other tips or suggestions, please do share them in the comments below.

Share:

Saturday, October 7, 2017

October 2017 Cumulative Updates for NAV 2013, 2013 R2, 2015, 2016 and 2017

Here are links to the release notes and download link for the CU's released.

NAV 2013 – Cumulative Update 55

You can download the cumulative update from KB 4045667 

NAV 2013 R2 – Cumulative Update 48

You can download the cumulative update from KB 4045668

NAV 2015 – Cumulative Update 36

You can download the cumulative update from KB 4045669

NAV 2016 – Cumulative Update 24

You can download the cumulative update from KB4045670

NAV 2017 – Cumulative Update 11

You can download the cumulative update from KB4045671

Share: