A function to descrease a string:
DecStr(IcodIncoming : Code[20];IintStep : Integer) OcodReturnCode : Code[20]
// DecStr //> 104
// Like the NAVISION INCSTR-function but DECREASING
// PARAMETERS:
// IcodIncoming : String to be decreased (if this string does not contain a number, an empty string is returned)
// IintStep : Decrement with this value (if it is negative, an empty string will be returned)
// (if it is 0, the value of "IcodIncoming" will be returned)
// RETURN-VALUE : decreased string
// in some cases, just exit
IF IintStep = 0 THEN
EXIT(IcodIncoming);
IF IintStep < 0 THEN
EXIT('');
IF IcodIncoming = '' THEN
EXIT('');
LcodAllowedChars := '1234567890';
LintLen := STRLEN(IcodIncoming);
LintStartOfNumericPart := LintLen;
// search the last not numeric character
WHILE (STRPOS(LcodAllowedChars,COPYSTR(IcodIncoming,LintStartOfNumericPart,1)) <> 0) DO
LintStartOfNumericPart -= 1;
// start of numeric part
LintStartOfNumericPart += 1;
IF LintStartOfNumericPart > LintLen THEN
EXIT(''); // no numeric part found at right end of the string
EVALUATE(Lint,COPYSTR(IcodIncoming,LintStartOfNumericPart));
Lint -= IintStep;
IF Lint < 0 THEN
ERROR(LtxcNegative,IcodIncoming,IintStep,Lint);
LcodAllowedChars := FORMAT(Lint);
OcodReturnCode :=
COPYSTR(IcodIncoming,1,LintStartOfNumericPart - 1) +
PADSTR('',LintLen - LintStartOfNumericPart - STRLEN(LcodAllowedChars) + 1,'0') +
LcodAllowedChars;
Regards,Alain Krikilion
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!
0
Comments
Will see if there are more than one numeric in the string passed
Just to make your code even better, I implemented some code to handle values that already consist of only integers.
Add this code to the top of the Kirki's code, just above the Exit statements.
This will make sure that no errors occur when the codefield consists of only integers (for scenario's where you might not know that up-front).
The code will then just decrement the integer and exit with that value.
Create this local variable within the DecStr function:
>>
IntValue of type Integer
<<
Add the below code: >>
IF EVALUATE(IntValue, IcodIncoming) THEN BEGIN
IntValue -= IintStep;
OcodReturnCode := FORMAT(DecValueInt);
EXIT(OcodReturnCode);
END;
Just put all of it in a global function in a "Common Functions" Codeunit to use this whenever you need.