Colby Clark
Colby Clark

Reputation: 181

NetSuite API search by externalId

I am attempting to search for a NetSuite Vendor (which may or may not exist in NetSuite) with the NetSuite API; however, the only piece of information I have about the Vendor is the externalId. My goal is to query NetSuite for the Vendor with the given externalId and if the Vendor exists, use it in a subsequent API call. If the Vendor doesn't exist, I'll create it and use the vendor I've just created in the subsequent API call.
Despite my searches I have not been able to find a way to search NetSuite for a record by externalId (I've found many ways to search by a Field; however I haven't found a way to search by an Attribute i.e. externalId). Any help is appreciated.

Upvotes: 8

Views: 5673

Answers (4)

Kev
Kev

Reputation: 131

Here's snippet for c#: (I have used array of externalId, just in case if you want more than one)

1. Create multiselectfield search

SearchMultiSelectField field =
new SearchMultiSelectField
{
    @operator = SearchMultiSelectFieldOperator.anyOf,
    operatorSpecified = true
};

var itemList = new List<com.netsuite.webservices.RecordRef>();
foreach (var externalId in externalIds)
{
    var recRef = new com.netsuite.webservices.RecordRef
    {
        externalId = externalId
    };
    itemList.Add(recRef);
}           

field.searchValue = itemList.ToArray();

var basic = new TransactionSearchBasic {externalId = field};

2. Call search function on service:

NetSuiteService service; // Instantiate with netsuite creds
SearchResult results = service.search(selectSearch);

List<Record> transactions = new List<Record>();

if (results?.status?.isSuccess ?? false)
{
    for (int i = 1; i <= results.totalPages; i++)
    {
        var recordList = results.recordList;
        transactions.AddRange(recordList);
    }
}
else if (results?.status != null)
{
    this.log.Debug(GetStatusDetails(results.status));
}
else
{
    this.log.Debug("Failed to recieve results");
}

return transactions;

Upvotes: 1

melicent
melicent

Reputation: 1251

The restlet code from user5227543 worked for me, except I had to add a check for vendorList.length>0 in addition to the null check:

var record = (vendorList!=null && vendorList.length > 0) ? nlapiLoadRecord('vendor',vendorList[0].getId()) : nlapiCreateRecord('vendor');

Upvotes: 0

user5227543
user5227543

Reputation:

Using Restlet

var vendorFilter = [];
vendorFilter.push(new nlobjSearchFilter('externalid',null,'is',dataIn.externalid);


var vendorList = nlapiSearchRecord('vendor',null,vendorFilter,null);

var record = (vendorList!=null) ? nlapiLoadRecord('vendor',vendorList[0].getId()) : nlapiCreateRecord('vendor');

// process your logic

Upvotes: 3

Colby Clark
Colby Clark

Reputation: 181

After further research and a little help, I've figured out how to search by externalId. Hopefully this is useful for someone in the future:

Using php: Create a new GetRequest() object and a new RecordRef() object set the RecordRef's externalId to the desired external ID set the RecordRef's type to "vendor" set the GetRequest's baseRef to the RecordRef you've just created using the NetSuite client execute the get() method passing the the GetRequest() object created previously. The get() method will return a GetResponse() containing information about your search (and the object if it exists).

$getRequest = new \NetSuite\WebServices\GetRequest();
$recordRef = new \NetSuite\WebServices\RecordRef();
$recordRef->externalId = "theExternalIdGoesHere";
$recordRef->type = "vendor";
$getRequest->baseRef = $recordRef;
$response = $client->get($getRequest);

Upvotes: 9

Related Questions