Developers » Script Examples by Type » Asynchronous Task Script Example


Asynchronous Task Script Example

Quick Links

Best Practices

[Back to top]

Use asynchronous tasks for jobs that take a long time to complete, or when you need to perform a simple job several times. For example, you can create an asynchronous task to loop through data, such as a CSV file or custom table, and create a new case for each row.

Asynchronous tasks are a powerful resource because they enable you to run scripts in parallel. When not used correctly, this can create adverse performance issues. To optimize performance, we recommend that you:

  • Do not open an external database connection from an asynchronous task because you can take all the available database connections from the pool of connections in the database datasource. This will create performance issues inWorkflow.
  • Do not run FTP operations from an asynchronous task because there are usually constraints in the FTP servers about how many concurrent FTP connections are allowed.
  • Do not use external API systems from an asynchronous task.
  • Rather than executing an asynchronous task for each new case that needs to be created, create a large numbers of cases in a bulk process from a script . The following recommended method of creating cases in bulk provides better performance, easier implementation, and allows you to create more cases with greater flexibility:

void ScriptCaseManager save ( List of Cases ) 

Note: You can pass only serializable parameters, such as String, Integer, HashMap, and List when executing an asynchronous task. You cannot pass certain objects, such as Case, User, or DB Connection, as parameters.

Asynchronous Task Script 

[Back to top]

Create a script using the following, or similar parameters:

  • Script Type: Asynchronous Task
  • Script Name: Async Create Case
  • system_id: async_create_case

Then, you can use the following code sample to create a new case. In line 5, you can see the parameter from the map that will be sent when the asynchronous task is called.
def wf = resp.getCaseType('rma'); 
def nCase = resp.newCase(wf);

//Parameter from map that will send in a call of this async task
nCase.setDescription('created from script');;

Script that Calls Asynchronous Task Script

[Back to top]

Create a script using the following, or similar parameters:

  • Script Type: This can be either a Workflow or Scheduled Task script type
  • Script Name: Example that Calls Async Task
  • system_id: example_that_calls_asynch_task

The following code sample shows how to call the asynchronous task from a script. See line 7 for the request: 

for (int i = 0; i < 10; i++ ) {  
    def asyncParams = new HashMap<String,String>() 
    asyncParams.put('case_name', 'some case name'+i);  

try {
     //Call asynchronous task which will create a new case with parameters loaded from that row of the file  
     resp.asyncTask.execute ('async_create_case', asyncParams); 

} catch (Exception e) {       

  //Create a new exception handling case  
  resp.exception.newException('sampleAsyncTask'+i, e.getMessage(), "Issue processing row ", e.getStackTrace().toString(), "MAJOR");  

Related Articles