Sunday, September 9, 2018

Web Service > PHPToolkit > asyncAddList, checkAsyncStatus and getAsyncResult Code Samples

Note: This article applies to the PHP Toolkit before version 2012.2.


 

As per the Suite Talk Web Services Platform Guide; Web services requests can be processed synchronously or asynchronously. In synchronous requests, your client application sends a request to the SuiteTalk Platform where it is processed and a response is returned. The client application handles the response as appropriate.  In asynchronous requests, your client application sends a request to the SuiteTalk Platform where it is placed in a processing queue and handled asynchronously with other requests. Note that all available jobs for each polling period will be processed contiguously. There is no enforced waiting period for a job that is available. Once a job is processed, a job ID is returned in the Web services response. Your client application can then check on the status and result of the request by referencing the job ID.

The checkAsyncStatus operation can be used to check the status of an asynchronous Web services submission. When a jobId is submitted, the status, percent complete, and estimated remaining duration are returned.

The getAsyncResult operation can be used to retrieve the results of an asynchronous web services submission.

This Article outlines samples for performing an asynchronous request using addList and using checkAsyncStatus against that request.

Note: This article requires existing knowledge of Web Services and PHP.


asyncAddList Sample Code:

   require_once '../PHPtoolkit.php';
    require_once 'login_info.php';

    global $myNSclient;


    # ASYNCADDLIST SAMPLE
    # ===============


    // create array of customer fields
    $customer1Fields = array (
        'isPerson'            => true,
        'firstName'            => 'myFirstName1',
        'lastName'            => 'myLastName1',
        'companyName'        => 'myCompanyName1',
        'phone'                => '555-555-1234',
        'email'                => 'myEmail1@myEmail.com',
        );


    $customer2Fields = array (
        'isPerson'            => true,
        'firstName'            => 'myFirstName2',
        'lastName'            => 'myLastName2',
        'companyName'        => 'myCompanyName2',
        'phone'                => '555-555-1234',
        'email'                => 'myEmail2@myEmail.com',
        );

    // create Customer record
    $customer1 = new nsComplexObject('Customer');
    $customer1->setFields($customer1Fields);

    $customer2 = new nsComplexObject('Customer');
    $customer2->setFields($customer2Fields);

    // perform asyncAddList operation
    $addResponse = $myNSclient->asyncAddList(array($customer1, $customer2));

    // get job id
    $jobId = $addResponse->jobId;

    // handle response
   while ($addResponse->status == 'pending' || $addResponse->status == 'processing')
   {
    sleep(10);
    $addResponse =  $myNSclient->checkAsyncStatus($jobId);
   }

   // once it is done processing, get the result
   $result = $myNSclient->getAsyncResult($jobId, 1);

   print_r($result);


The above code would generate the following SOAP request for the asyncAddList request:

<Body>
<asyncAddList>
<record xsitype="Customer">
<isPerson>true</isPerson>
<firstName>myFirstName1</firstName>
<lastName>myLastName1</lastName>
<companyName>myCompanyName1</companyName>
<phone>555-555-1234</phone>
<email>myEmail1@myEmail.com</email>
</record>
<record xsitype="Customer">
<isPerson>true</isPerson>
<firstName>myFirstName2</firstName>
<lastName>myLastName2</lastName>
<companyName>myCompanyName2</companyName>
<phone>555-555-1234</phone>
<email>myEmail2@myEmail.com</email>
</record>
</asyncAddList>
</Body>
 


 In the sample code above for an asyncAddList request, you can see the following code making use of getAsyncResult and checkasyncStatus:

    // get job id
    $jobId = $addResponse->jobId;

    // handle response
   while ($addResponse->status == 'pending' || $addResponse->status == 'processing')
   {
    sleep(10);
    $addResponse =  $myNSclient->checkAsyncStatus($jobId);
   }
   // once it is done processing, get the result
   $result = $myNSclient->getAsyncResult($jobId, 1);

   print_r($result);


 

 The above code would generate the following SOAP request for checkAsyncStatus:

 <Body>
<checkAsyncStatus>
<jobId>ASYNCWEBSERVICES_TSTDRV275226_070220091020377703182005990_60</jobId>
</checkAsyncStatus>
</Body>


The above code would generate the following SOAP request for getAsyncResult request:

 <Body>
<getAsyncResult>
<jobId>ASYNCWEBSERVICES_TSTDRV275226_070220091020377703182005990_60</jobId>
<pageIndex>1</pageIndex>
</getAsyncResult>
</Body>


 

No comments:

Post a Comment