:: Celigo ::
ProductsCustomersBlogAbout
On-Demand Simplified Solutions and services tailor made for on-demand
Code Samples for SuiteTalk Service Manager

The use of Celigo's ServiceManager and ServicePool was made as intuitive as possible for a developer familiar with the SuiteTalke API. Often this is as simple as replacing method calls to the .NET Web Service proxy class with calls to ServiceManager's / ServicePool's "UpperCamelCased" equivalents.

Following is an example from the two versions of the CRM sample applications where you would replace:

WriteResponse response = _service.add( customer );
with:
WriteResponse response = _svcPool.Add(customer);
where _service is an instance of NetSuiteService and _svcPool is an instance of NetSuiteServicePool.

Initializing NetSuiteServicePool

Following is a typical example of initializing a NetSuiteServicePool instance:

NetSuiteServicePoolManager poolMgr = new NetSuiteServicePoolManager();
poolMgr.ServiceConfiguration.SearchPageSize = 40;
poolMgr.ServiceConfiguration.BodyFieldsOnly = true;
poolMgr.Credentials.Add(new NetSuiteCredential(
		Settings.Default.LoginEmail,
		Settings.Default.LoginPassword,
		Settings.Default.LoginAcct,
		Settings.Default.LoginRoleInternalId));
_svcPool = new NetSuiteServicePool(poolMgr);

Here, the user credentials are assumed to be stored in the application's settings, and the NetSuiteServicePool instance being initialized is the _svcPool member variable.

Please note that Celigo does not recommend that you store user credentials in application settings as plain-text. The above example is for illustration purposes only.

Pagination with NetSuiteServicePool

One of the few instance where the NetSuiteServicePool deviates from mirroring the SuiteTalk API is when performing a search which would require subsequent searchNext(..) calls.

Note that the NetSuiteServicePool maintains a pool of active SuiteTalk sessions. Since the searchNext(..) must be perfomed by the exact same session that invoked the search(..) operation, a specilized overloaded method for search(..) has been provided. Use of this overload requires that you obtain a reference to an active session before you perform the initial search:

using (NSSession session = _svcPool.IssueSessionHandle())
{
	SearchResult response = _svcPool.Search(custSearch, session);
	// Process response
	if (response.status.isSuccess)
	{
		// TODO: Process the records returned in the response...
		// Since pagination controls what is returned, check to see
		// if there are anymore pages to retrieve.
		SearchMore(response, session);
	}
	else
	{
		_out.error(GetStatusDetails(response.status));
	}
}

private void SearchMore(SearchResult response, NSSession session)
{
    //SearchResponse response;
    bool isGetAllPages = false;

    // Keep getting pages until there are no more pages to get
    while (response.totalRecords > (response.pageSize*response.pageIndex))
    {
        if (!isGetAllPages)
        {
            // Only continue if user wants to get the next page
            _out.write("\nThere are more search results." +
          		"Would you like to get the next page" +
                    	"for this search? (A/Y/N): ");

            string userResponse = _out.readLn().ToUpper();
            if (string.Equals(userResponse, "N"))
            {
               break;
            }
            else if (string.Equals(userResponse, "A"))
            {
                isGetAllPages = true;
            }
        }

	// Invoke searchMore() operation
	response = _svcPool.SearchMore(response.pageIndex + 1, session);

        // Process response
        if (response.status.isSuccess)
        {
            // TODO: Process the records returned in the response
        }
        else
        {
            _out.error(GetStatusDetails(response.status));
        }
    }
}

If you need to use the NSSession outside a "using" block, make sure that you invoke Dispose() on it to release the locked down session when you are done with the search.

Please note that in case of very lengthy operations, the NSSession might timeout and a subsequent call to SearchNext(..) may fail due to this. This issue would be addressed in a future version of the ServiceManager library.

Of course, if paging is not required you can use the overload of Search(..) that mirrors the one from the SuiteTalk API.

Using NetSuiteServiceManager

If you do not need the addtional functionality provided by the Service Pool, you can directly use the NetSuiteServiceManager instead.

Initilization is similar to that of the NetSuiteServicePool:

NetSuiteServiceManager svcMgr = new NetSuiteServiceManager();
svcMgr.Credentials = credential; 
// pre-configured NetSuiteCredential variable
svcMgr.ServiceConfiguration = serviceConfiguration;
// pre-configured NetSuiteServiceConfiguration variable
Since a ServiceManager is bound to a SuiteTalk session, you can issue Search and SearchNext without a reference to a NSSession.