Friday, April 12, 2019

Call Asynchronous Web Service Operation in C#

To find out whether there is asynchronous call for an operation, please check NetSuite Help Center article for that operation. This article shows an example of a asynchronous search in NetSuite.

This is a brief description of the process:
- Prepare a new CustomerSearchBasic object instance
- Instead of calling search() call asyncSearch()
- The SOAP call will finish without getting the results
- Job ID is generated as a result of asyncSearch()
- Now the search operation is placed in NetSuite processing queue, identified by its Job ID
- use checkAsyncStatus() operation to check whether the search is already finished or not
- Once the Job has finished, fetch results with getAsyncResult()

Example code (search for Customer with Internal ID = 100):

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

using NSWS_Async.com.netsuite.webservices;

 

namespace NSWS_Async

{

    class Program

    {

        static void Main(string[] args)

        {

            // create service object and CookieContainer

            NetSuiteService service = new NetSuiteService();

            service.CookieContainer = new System.Net.CookieContainer();

 

            // create Passport with credentials

            service.passport = new Passport();

            service.passport.account = "00000";

            service.passport.email = "user@domain.com";

            service.passport.password = "password";

 

            // Example search object (looking for Customer with Internal ID = 100)

            CustomerSearchBasic csearch = new CustomerSearchBasic();

            csearch.internalId = new SearchMultiSelectField();

            csearch.internalId.@operator = SearchMultiSelectFieldOperator.anyOf;

            csearch.internalId.operatorSpecified = true;

            csearch.internalId.searchValue = new RecordRef[1];

            csearch.internalId.searchValue[0] = new RecordRef();

            csearch.internalId.searchValue[0].internalId = "100";

 

            // Make the Async call to NetSuite; it will return Job ID

            AsyncStatusResult asyncResult = service.asyncSearch(csearch);

            Console.WriteLine("Searching, status: " + asyncResult.status.ToString());

 

            // Check the status

            // when Failed, set searchFailed

            // when pending or processing, set searchFinished = true, otherwise set SearchFinished = false

            bool searchFinished = (!((asyncResult.status == AsyncStatusType.pending) ||

                (asyncResult.status == AsyncStatusType.processing)));

            bool searchFailed = (asyncResult.status == AsyncStatusType.failed);

 

            // Now keep waiting for the search results

            if (!searchFailed)

            {

                while ((!searchFinished) && (!searchFailed))

                {

                    // Check current status of the job

                    AsyncStatusResult jobStatus = service.checkAsyncStatus(asyncResult.jobId);

                    searchFinished = (!((jobStatus.status == AsyncStatusType.pending) ||

                        (jobStatus.status == AsyncStatusType.processing)));

                    searchFailed = (jobStatus.status == AsyncStatusType.failed);

                    Console.WriteLine("Polling for job status: " + jobStatus.status.ToString()

                        + " " + Convert.ToInt16(jobStatus.percentCompleted)

                        + "% complete");

 

                    // sleep for 5 seconds if not finished yet

                    if (!searchFinished) { System.Threading.Thread.Sleep(5000); }

                }

            }

 

            if (!searchFailed)

            {

                Console.WriteLine("Async job call successful. Fetching results");

 

                // Fetch the job results

                AsyncSearchResult result = (AsyncSearchResult)service.getAsyncResult(asyncResult.jobId, 1);

                if (result.searchResult.status.isSuccess)

                {

                    if (result.searchResult.recordList != null)

                    {

                        // Print out all returned records

                        foreach (Record record in result.searchResult.recordList)

                        {

                            Customer customer = (Customer)record;

                            Console.WriteLine("Returned customer: " + customer.companyName);

                        }

                    }

                }

                else

                {

                    Console.WriteLine("Search failed");

                   

                    // In case of error, show error details

                    if (result.searchResult.status.statusDetail != null)

                    {

                        foreach (StatusDetail statusDetail in result.searchResult.status.statusDetail)

                        {

                            Console.WriteLine("ERROR: " + statusDetail.message);

                        }

                    }

                }

            }

            else

            {

                Console.WriteLine("Async job call failed");

            }

 

            Console.WriteLine("Finished. Press ENTER to exit.");

            Console.ReadLine();

        }

    }

}

No comments:

Post a Comment