web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to initialize a batch in Custom service

(1) ShareShare
ReportReport
Posted on by 670
Hi Folks,
 
When the request is received from WebAPI, I need to send the response back that I have received the request and run a batch job.
 
So for that I need to initiate a Sysopertaion controller class to initiate the batch job which will run a batch job and simultaneously will send the response as "The request is received" and afterwards the business logic can be run in batch.
 
So How can I achieved this requirement in service class? And how it will run in base after wards?
 
Custom controller class
class  abcBatchController extends SysOperationServiceController
{
    public static void main(Args _args)
    {
        abcBatchController controller = new abcBatchController();
        controller.parmLoadFromSysLastValue(false);
        controller.startOperation();
    }
}
 
 
Custom Service class
 
class abcService
{  
    public abcResponseContract create(abcRequestContract _RequestContract)
    {  
        abcResponseContract               responseContract = new abcResponseContract();
        abcRequestContract                abcReqContract = _RequestContract;
        str                                                 message;
        abcIntegrationHelper              IntegrationHelper = new abcIntegrationHelper();
        guid                                                sessionId;
        abcIntegrationStatus                              status;
        abcBatchController                requestController = new abcBatchController();
        
        sessionId = newGuid();
        status  = abcIntegrationStatus::NotProcessed;
        try
        {           
            message = "";
            responseContract = abcIntegrationHelper.generateResponse(abcIntegrationStatus::Success, sessionId, message); 
          
            status = abcIntegrationStatus::Success;
        }
        catch (Exception::Deadlock)
        {
            retry;
        }           
        return responseContract;
    }    
}
 
 
 
Thanks in Advance!!
I have the same question (0)
  • Martin Dráb Profile Picture
    237,994 Most Valuable Professional on at
    It's not clear to me what you need from us. It seems that you already have the response covered, therefore I'm assuming we can ignore this part of the requirement. You also have a method to run the batch, but you don't call it from create(). If you know how to call a static method, adding the call should be easy. Please confirm.
     
    You told us little about the implementation of your controller (what you showed can't be the complete code), therefore it's not clear which execution mode you're using (I'm assuming you want either ScheduledBatch or ReliableAsynchronous) and whether it doesn't show a dialog. A dialog would be a problem, as the web service has no user to interact with forms.
     
    Next time, please make sure you explain which particular problem you're struggling with.
  • D365FO Avatar Profile Picture
    670 on at
     
    Yes, I have tried to call the controller class from my service class
    class abcService
    {
        public abcResponseContract create(abcRequestContract _RequestContract)
        {  
            abcResponseContract               responseContract = new abcResponseContract();
            abcRequestContract                abcReqContract = _RequestContract;
            str                                                 message;
            abcIntegrationHelper              abcIntegrationHelper = new abcIntegrationHelper();
            guid                                                sessionId;
            abcIntegrationStatus                              status;
    
            sessionId = newGuid();
            status  = abcIntegrationStatus::NotProcessed;
            try
            {
                infolog.clear();
                message = "";
    
                Args args = new Args();
    
                abcBatchController::main(args);
    
                responseContract = IntegrationHelper.generateResponse(abcIntegrationStatus::Success, sessionId, message); 
              
                status = abcIntegrationStatus::Success;
            }
            catch (Exception::Deadlock)
            {
                retry;
            }           
            return responseContract;
        }
    }
     
    Controller class
     
    class  abcBatchController extends SysOperationServiceController
    {
    
        public static void main(Args _args)
        {
            abcBatchController controller = new abcBatchController();
            controller.parmLoadFromSysLastValue(false);
            controller.startOperation();
        }
    
        public void new()
        {
            super();
    
            this.parmClassName(classStr(abcIntegrationHelper));
            this.parmMethodName(methodStr(abcIntegrationHelper, process));
            this.parmDialogCaption("Generate Report batch");
            this.batchInfo().parmBatchExecute(this.parmExecutionMode() == SysOperationExecutionMode::ScheduledBatch);
        }
    
    }
    But this below error is coming in formRun when I m calling this through postman
     
  • Martin Dráb Profile Picture
    237,994 Most Valuable Professional on at
    I already told you about this problem. Your controller is trying to open the dialog form, but there is no client to render the form on and no user who could confirm the dialog, therefore this step doesn't make sense and your code must fail.
     
    You must suppress the dialog, e.g. by returning false from parmShowDialog().
     
    Also, if you believe that your code sets the execution mode to ScheduledBatch, you're wrong. You can do that by using a parameter in the constructor or passing the value to parmExecutionMode().
  • Layan Jwei Profile Picture
    8,124 Super User 2025 Season 2 on at
    Hi D365FO avatar,
     
    Didn't you say that u want to send the repsonse to a batch job or sth? I can't see that u are doing this as you are calling the batch job before getting the response
     
    Can you show us this integration helper class please?
    As I'm not sure from where this formRun came. Maybe use parmShowDialog(false) in ur controller in the main method 
     
    Also if you debug ur custom service, at which line exactly does it fail? Is it when u call the batch or when u generate the response. Please put break points and let us know
     
    Maybe check those links to learn more about sysOperationFramework


  • D365FO Avatar Profile Picture
    670 on at
    Thanks Martin Dráb 

    After adding the below code my error is gone but again the requirement to send the response asap when the request is received is not achieved yet. In my case, the batch job logic is running first and then the response is coming. 
     
    class  abcBatchController extends SysOperationServiceController
    {
    
        public static void main(Args _args)
        {
            abcBatchController controller = new abcBatchController();
            controller.parmLoadFromSysLastValue(false);
            controller.startOperation();
        }
    
        public void new()
        {
            super();
    
            this.parmClassName(classStr(abcIntegrationHelper));
            this.parmMethodName(methodStr(abcIntegrationHelper, process));
            this.parmShowDialog(false);
            this.parmExecutionMode(SysOperationExecutionMode::Asynchronous);
    }
    
    }


    Yes Layan Jweithe requirement is still missing which help me out.
     
    Helper class
    
    public class abcIntegrationHelper
    {
        public void process(abcRequestContract _contract)
        {
            
            ---LOGIC 
        }
    
         public abcResponseContract generateResponse(abcIntegrationStatus _status, guid _runSession, str _errorMessage = "")
        {
            abcResponseContract abcResponseContract = new abcResponseContract();
    
            abcResponseContract.parmRunID(_runSession);
            abcResponseContract.parmCode(enum2Str(_status));
            abcResponseContract.parmMessage(_errorMessage);
    
            return abcResponseContract;
        }
    }
     
  • Martin Dráb Profile Picture
    237,994 Most Valuable Professional on at
    Please give us more details about the current problem. Are you saying that abcIntegrationHelper.process() doesn't get executed asynchronously? How did you determine it?
     
    Also, you really should decide which execution mode you want to use. You suddenly switched to Asynchronous, although you're talking about a batch before. Even if you gave us wrong information and you actually don't want a batch, are you really sure you want the Asynchronous mode (rather than the other two asynchronous modes I mentiobed before)?
  • D365FO Avatar Profile Picture
    670 on at
     
    Update - Sorry, Now I have corrected it to ScheduledBatch, now it is going in batch and response is generating as required.
    class  abcBatchController extends SysOperationServiceController
    {
    
        public static void main(Args _args)
        {
            abcBatchController controller = new abcBatchController();
            controller.parmLoadFromSysLastValue(false);
            controller.startOperation();
        }
    
        public void new()
        {
            super();
    
            this.parmClassName(classStr(abcIntegrationHelper));
            this.parmMethodName(methodStr(abcIntegrationHelper, process));
            this.parmShowDialog(false);
            this.parmExecutionMode(SysOperationExecutionMode::ScheduledBatch);
            this.batchInfo().parmBatchExecute(NoYes::Yes);
        }
    
    }
     
     
    Problem - But the _contract buffer of Requested contract is coming blank when the Helper class is running in Batch, so how can I get the contract parameter values in helper class????
     
     
    public class abcIntegrationHelper
    {
        public void process(abcRequestContract _contract)
        {
            
            ---LOGIC 
        }
    
         public abcResponseContract generateResponse(abcIntegrationStatus _status, guid _runSession, str _errorMessage = "")
        {
            abcResponseContract abcResponseContract = new abcResponseContract();
    
            abcResponseContract.parmRunID(_runSession);
            abcResponseContract.parmCode(enum2Str(_status));
            abcResponseContract.parmMessage(_errorMessage);
    
            return abcResponseContract;
        }
    }
     
     
     
  • Martin Dráb Profile Picture
    237,994 Most Valuable Professional on at
    That's correct - you never set any value of the controller's contract, therefore it'll always be empty.
  • Layan Jwei Profile Picture
    8,124 Super User 2025 Season 2 on at
    If you don't pass the request contract to the helper class then how do you want the helper class to have access to the request?
     
    Also where in the code do you call process method? I can't see it being called at all
  • D365FO Avatar Profile Picture
    670 on at
     
    PLease help me to understand if the controller's contract will be blank then in my case when custom service is scheduling it in a batch, then how the batch job will get the requested contract parameter values.
     
    Because when I m debugging in IIS, the contract value is coming in custom service class and as it will scheduled in batch job and I have debugging in Batch.exe then in that case the request contract is coming as NULL.
     
    So please help me on this issue..
     
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 669 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 449 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 384 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans