Tech Tuesday: Default Batch Code Description
Have you ever wanted to create a process that automatically defaults the batch number on a window to a specific value? On our website we have a template for “Default Batches” you can download and import for Extender Enterprise version.
Here is a description of what the lines of code does so you can understand how this process works. Here is the code to set the Batch Number on the Receivables Transaction Entry window. Below that we show each line and describe what is happening.
<Batch Number> = ls_batch_number;
First we start with define the Extender action, which we have set up to run on the Open of the Receivables Transaction Entry window, and set the action to run a Dynamics script.
First we define a local variable to store the batch number we are creating to default. The “local string” defines a local string variable and we have named the variable ls_batch_number. Every line has to end with a semi-colon ; for the code to work. We want to set the default batch to the user name then include today’s date in the format of yymmdd. So if I log in as ‘sa’ on June 1st, 2016 the batch would be set to SA20160601. Please note the maximum length of the batch name is 15 characters, and the date takes up 8 characters, so if your users login name is longer than 7 characters, it will lose any characters after the 15th character.
local string ls_batch_number;
Within Dynamics GP there are global, meaning available anywhere within Dynamics GP. When you log into Dynamics GP, the ‘User ID’ of globals gets set to the user name you log into GP. The ‘User Date’ of globals by default is set to today’s date, but you can change the User Date within Dynamics GP which sets the ‘User Date’ of globals. We force the user name to upper case with the “upper” function. So if I log into GP as ‘sa’, it would set the value to SA. The reason, Dynamics GP forces the batch name to upper case, so our code needs to do that as well. We then pull the year, month and day values from the User Date with date functions with the same names. The other code within here is the “pad” function, which we describe below the code snippet.
The pad function will put leading zeros on the month value IF the month number is less than 10, as we say the length is “2”. So June would show as “06”. The “str” function changes the month from a number to a string so the leading zero stays. If we did not force this to a string, we would lost the leading zero.
pad(str(month(‘User Date’ of globals)), LEADING, str(0), 2) +
Next we want to read the Dynamics GP table Batch_Headers. The way the code works, we have to set the 2 key values ‘Batch Number’ and ‘Batch Source’ to read the table to determine if the batch already exists. The 1st line sets the ‘Batch Number’ to the batch number created in our local variable. The 2nd line sets the ‘Batch Source’ to a global variable RM_SALES_STR which has a value of “RM_SALES”. We could have written the 2nd line as ‘Batch Source’ of table Batch_Headers = “RM_SALES”; and this would have worked the same way. The advantage of using the global variable, if Dynamics GP ever changes what that Batch Source value is, they change their global constant RM_SALES_STR to have that value, and you do not have to make any code changes. The 3rd line is how to read the table to see if that batch number and batch source exist in the Batch_Headers table.
Once we read the table, if the batch number and batch source does exist, then the “err” is set to OKAY, if the error is not OKAY, then we want to create the new batch. If the batch does exist, there is nothing we need to do.
if err(table Batch_Headers) <> OKAY then
Since the batch does not exist, we need to set the values on the Batch Entry window.
The Origin is set to the value 1 to set the origin to “Transaction Entry”. Since this is a drop down list, setting the value to 1, will set it to the 1st value in the drop down list shown above.
The Batch Frequency is set to the value of a global constant SINGLE_USE to set the frequency to Single Use in the drop down list. You could have set that value = 1 instead of SINGLE_USE.
‘Batch Frequency’ of table Batch_Headers = SINGLE_USE;
Set the Series of the Batch to the global constant of SALES. You could set that value = 3 instead of SALES.
Series of table Batch_Headers = SALES;
Set the ‘User ID’ of the Batch to the global variable we discussed above of the login name.
‘User ID’ of table Batch_Headers = ‘User ID’ of globals;
Set the ‘GL Posting Date’ of the Batch to the global variable we discussed above of the User Date.
‘GL Posting Date’ of table Batch_Headers = ‘User Date’ of globals;
Set the ‘Modified Date’ and ‘Created Date’ of the Batch to today’s date. The function sysdate() returns today’s date in the format Dynamics GP expects the date to be formatted.
Set the ‘Batch Comment’ of the Batch to a hard coded string within the double quotes, followed by the global variable we discussed above of the login name.
‘Batch Comment’ of table Batch_Headers = “Auto created batch for ” + ‘User Name’ of globals;
Finally we need to set the ‘Checkbook ID’ for the batch. The “get first table CM_Checkbook_MSTR” code gets the very 1st record in that table. This assumes the CM_Checkbook_MSTR table has at least 1 record to get the Checkbook ID.
Set the ‘Checkbook ID’ of the Batch_Headers table to the value in the ‘Checkbook ID’ field in the CM_Checkbook_MSTR table.
‘Checkbook ID’ of table Batch_Headers = ‘Checkbook ID’ of table CM_Checkbook_MSTR;
We have to end the “If” section with “end if”.
Now we will save the record to the Batch_Headers table. If this is an existing Batch, it will not change any values, if this is a new batch it will be saved in the Batch_Headers table
save table Batch_Headers;
Finally we set the Batch Number on the window (<Batch Number>) to the local variable we built. The < > signifies to Extender this is the name of the field on the Receivables Transaction Entry window.
<Batch Number> = ls_batch_number;
You may have noticed as we were setting values of the fields within the Batch_Headers table, some of the field names had single quotes around the field name, and some of the field names did not have single quotes. If the field name has a space within the name, you have to put single quotes around the field names. This specifies the values as field names, and not hard coded strings. If you want to use hard coded strings, you put double quotes around a “hard coded string”. We hope this helps you understand what the code within this template is doing, and takes away some of the mystery of the commands.
Integrate & Automate without Any Code.
SmartList Data has Never Been Faster.
The Easiest Way to Report on GP Data.
Leave a Comment