canGoBatch & mustGoBatch - When to use and why?

I was posed this question today by a friend.

What is the intention behind the canGoBatch() method on the RunBaseBatch class? His point was since the RunBaseBatch class was solely providing batch capabilities, what is the point of overriding the canGoBatch() and return false to hide the batch tab?

I explained that there can be a scenario where you dont want to allow a user to change any settings on the batch tab. In such cases, you can make canGoBatch return false and hide the batch tab.

Now he had another question, why do you not want to allow a user to change any settings on the tab?

In a real world scenario, this is a typical case where you compulsorily want to ensure that your task runs in a batch. Take for example you are importing transaction files into AX from another legacy system. Now, you would want to post these on a particular date only, say end of every month. For this purpose, the administrator has setup a recurring batch job. Now an unknown user comes in, and posts a particular transaction by unchecking the Batch processing check box in the batch tab. What happens is the transaction is immediately posted and this may lead to some discrepancy in the system. So, this is one instance where we should ensure that the batch tab is hidden from unauthorized users.

But the solution is not to just ovverride the canGoBatch method and return false. Instead there should be logic to check for the current user's permission and then return false.

Something like this,

public boolean canGoBatch()
{
boolean ret;

// Check if the current user should be allowed to access batch tab
ret = hasSecurityKeyAccess(SecurityKeyNum(MySecurityKey), AccessType::Delete);

return ret;
}

My friend nodded and then asked another twisted question.

Suppose a user who has access to the batch tab, runs the job by unchecking the Batch processing check box. Wont this still lead to discrepancies? Well, it sure will. In that case, we will need to ensure that the job runs only in ran in a batch else it doesn't run. How do we do that?

The solution is to override the mustGoBatch() method. This method ensures that the job runs ONLY when run as a batch job. So, if a user unchecks the Batch processing check box and then runs the job, it wont run because the mustGoBatch() method is returning true which means that this job should ONLY run in a batch.

The canGoBatch() and mustGoBatch() methods are the ways to control and manipulate the batching capabilities.

Comments

Popular posts from this blog

How to add empty ranges in query

Get selected records in Dynamics AX 2012

How to refer to form controls in code without setting the AutoDeclaration property in Dynamics AX?