DECSTR – Decrease integer value in string
INCSTR is a C/AL command, which is often used to increase document numbers, which usually start with some letters followed by a number, e.g. AB-00010.
With INCSTR(‘AB-00010’) you get then ‘AB-00011’.
In a Nav forum a member asked, if there is a simple way for decreasing such a document no. don’t know, why he needs that, but it’s a nice idea.
So, after a little research and finding some typical looooong c/al code solutions, i developed my own small, very cool solution using .net class Regex:
OnRun()
// loc. variables
//DocNo : Code 20
DocNo := DecStr(‘AB-00010’);
MESSAGE(DocNo);
LOCAL DecStr(DocNo : Code[20]) : Code[20]
// loc. variables
//Prefix: Text
//NoString: Text
//Number: Integer
//Regex: DotNet System.Text.RegularExpressions.Regex.’System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
//DotNetInt: DotNet System.Int32.’mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
//PadLength: Integer
Prefix := Regex.Replace(DocNo,’\d+’,”);
NoString := Regex.Match(DocNo,’\d+’).Value;
Number := DotNetInt.Parse(NoString);
IF Number > 0 THEN BEGIN
Number := Number – 1;
PadLength := STRLEN(DocNo) – STRLEN(Prefix) – STRLEN(FORMAT(Number));
EXIT(Prefix + PADSTR(”,PadLength,’0′) + FORMAT(Number));
END;
ERROR(‘Resulting number would be negative.’);
Results in: AB-00009
the resulting number has the same length.
Additional the links i found:
- http://dinukak-navmate.blogspot.co.at/2015/01/decstr-like-incstr-but-decreasing.html
- http://forum.mibuso.com/discussion/17478/decstr-like-incstr-but-decreasing
Filed under: .net, c/al, dynamics nav, nav 2009, nav 2013, nav 2015, nav 2016, nav functions

*This post is locked for comments