Monday, April 27, 2015

Dexterity - next doc num template

Having automatic 'next doc num' requires many different parts if you want to make sure to release unused next doc nums. For example, if a user clicks into a Doc Num field that is prepopulated with the next doc num, but then changes it, you'll want to release the prepopulated number, so numbers aren't wasted.


1. Setup window / table
Add your 'next doc num' to a setup window. The table may be a company-wide setup table, just like POP_Setup. Or it could be partitioned on "something", like document type, like SOP_ID_SETP.

2. Utility functions for getting next number

Note: I pass in an anonymous table, so that I can write unit tests and pass in a temp table instead of the actual setup table. This shields my setup data from getting damaged.

GetNextDocNum
function returns NextDocNum NextDocNum;
inout integer Status;
inout anonymous table tbl; {Anonymous so we can put this under test without affecting actual data}

local integer i = 0;
local boolean Success;
local string tmpNextDocNum;

Status = NextDocNumExists(table tbl);

if Status <> OKAY then
abort script;
end if;

NextDocNum = NextDocNum of table tbl;

{Loop 1000 times until we find a number that isn't used}

repeat
if not Exists(NextDocNum) then
exit repeat;
end if;

call Document_Number_Inc_Dec, NextDocNum, true, Success;
increment i;
until i >= 1000;

if i = 1000 then
Status = PROBLEM_GETTING_NEXT_NUM;
clear NextDocNum;
release table tbl;
else
{Because this is a valid next doc number, we want to increment the one in the table
to one after this}
tmpNextDocNum = NextDocNum;
call Document_Number_Inc_Dec, tmpNextDocNum, true, Success;
if not Success then
Status = PROBLEM_GETTING_NEXT_NUM;
clear NextDocNum;
else
NextDocNum of table tbl = tmpNextDocNum;
save table tbl;
if err() <> OKAY then
Status = PROBLEM_GETTING_NEXT_NUM;
clear NextDocNum;
end if;
end if;

end if;

NextDocNumberExists
function returns integer Status;
inout anonymous table tbl;

'Setup Key' of table tbl = 1;
change table tbl by number 1;
if err() <> OKAY then
Status = PROBLEM_GETTING_NEXT_NUM;
release table tbl;
abort script;
end if;

if empty(NextDocNum of table tbl) then
Status = NO_NEXT_NUM_SETUP;
release table tbl;
abort script;
end if;


Status = OKAY;

Exists
function returns boolean Exists;
in NextDocNum NextDocNum;

NextDocNum of table TransactionTable = NextDocNum;
get table TransactionTable by number 1;

Exists = (err() = OKAY);

ReleaseUnusedDocNum
{
Release the rejected num back into the wild
}

in NextDocNum NextDocNum;
inout anonymous table tbl;

if not Exists(NextDocNum) then
'Setup Key' of table tbl = 1;
change table tbl by number 1;
if err() = OKAY then
NextDocNum of table tbl = NextDocNum;
save table tbl;
end if;
release table tbl;
end if;

3. The window that uses the next number
Doc Num Pre Script - this populates the Doc Num field with the next doc num when the user clicks into the field
local integer Status;
local string tmpNum;

if empty(NextDocNum) then
tmpNum = GetNextDocNum(Status, table TheRealSetupTable);
if Status <> OKAY then
warning getmsg(Status);
abort script;
end if;

NextDocNum = tmpNum;
'(L) TempOldDocNum' = NextDocNum;
force change NextDocNum;
end if;
Doc Num Change Script
if '(L) TempOldDocNum' <> DocNum and not empty('(L) TempOldDocNum' ) then
{this means they didn't accept the populated doc num, so release it back into the wild}
call ReleaseUnusedDocNum, '(L) TempOldDocNum', table TheRealSetupTable;
clear '(L) TempOldDocNum';
end if;

TempOldDocNum Change Script -  This is very similar to the Doc Num Change Script, but look at the condition + which value is passed to ReleaseUnusedDocNum
if '(L) TempOldDocNum' = DocNum and not empty('(L) TempOldDocNum' ) then
{This means they're deleting the pre-populated value, so delete it}
call ReleaseUnusedDocNum, DocNum, table TheRealSetupTable;
clear '(L) TempOldDocNum';
end if;

HandleChanges - if they are discarding their changes

 if '(L) TempOldDocNum' = DocNum then call ReleaseUnusedDocNum, DocNum, table TheRealSetupTable; clear '(L) TempOldDocNum'; end if;

Call TempOldDocNum in
1. Clear Button
2. Win Post when there are no changes

1 comment:

There was an error in this gadget