x++dynamics-ax-2012-r2sysoperationframework

Determine if a job is running in X++ code


I have a scenario where a service passes is data to AX, and then we use the SysOperationFramework to process that data without making the service client wait for processing to finish.

An issue arises if the user attempts to open the record in the external application while processing is still occuring.

Is there a way, in X++, so see which jobs are currently executing (and further, see the parameters that were passed in) so that we can send an error that we can handle to the user?


Solution

  • There is a way, yes. The data you are looking for is stored in the Batch table. You will find a ClassNumber and a Status field. Just select a record matching your class that has the status executing. If a record exists, it is being executed.

    The parameters are stored in a the Parameters field in a container. You can unpack the container be creating an instance of your class and unpacking it, like so (quick code that will not compile but you get the point):

    Batch batch;
    SysOperationServiceController sysOperationServiceController;
    YourDataContract yourDataContract;
    
    select batch 
        where batch.ClassNumber = YourClassNumber
        && batch.Status == BatchStatus::Executing;
    
    // todo: you might have to check the type of the object before assignment
    // todo: also check if batch record has been found
    sysOperationServiceController = batch.object();
    
    if (sysOperationServiceController.unpack(batch.Parameters))
    {
        // todo: you might have to check the type of the object before assignment
        yourDataContract = sysOperationServiceController.getDataContractObject('_theParemterNameOfyourDataContract');
    
        // todo: here you can read the parameters from your contract
    }
    else
    {
        throw error("Unpack failed");
    }