Charles
Charles

Reputation: 756

NetSuite Transaction Search Returned Without Any Line Items for Invoice

I am attempting to import an invoice from NetSuite into my program. In this program, I need as much information about the invoice as possible to be returned. However it does not appear that any of the line item information is being returned. Here is my piece of code I am completing for the search. Any suggestions? I am trying to complete this with as few calls out to NetSuite as possible in order to keep performance high.

SearchResult searchResults = new SearchResult();

TransactionSearch ts = new TransactionSearch();
TransactionSearchBasic tsb = new TransactionSearchBasic();

// Search for Invoices
if (_InvoiceTxnIds.Count > 0)
{
    tsb.internalId = new SearchMultiSelectField();
    tsb.internalId.@operator = SearchMultiSelectFieldOperator.anyOf;
    tsb.internalId.operatorSpecified = true;

    List<RecordRef> rrlist = new List<RecordRef>();
    foreach (string sTxnId in _InvoiceTxnIds)
    {
        RecordRef rr = new RecordRef();
        rr.internalId = sTxnId;
        rrlist.Add(rr);
    }

    tsb.internalId.searchValue = rrlist.ToArray();

    ts.basic = tsb;

    searchResults = _service.search(ts);
}

Upvotes: 3

Views: 5074

Answers (2)

Hamada
Hamada

Reputation: 11

Thanks for this amazing and helpful note specially turning (_searchPreferences.bodyFieldsOnly = False) to get the Items details
I've modified the code to fit VB (get invoice details by Invoice Number , Thanks

Protected Sub GetInvoiceDetails_Click(sender As Object, e As EventArgs) Handles Button12.Click

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
    '=========== Setup a service Name 
    Dim service1 As NetSuiteService = New NetSuiteService()
    service1.CookieContainer = New CookieContainer
    '============ set application  id , you need to set it up prior on Netsuite , Setup-Integration-Manage Integration , then add App name and generate ID 
    Dim AppInfo = New ApplicationInfo()
    AppInfo.applicationId = "ABCDEFGhdjddjdjdjd-theAppID-&66hsdjhfn"
    service1.applicationInfo = AppInfo
    '========= Setup a passport 
    Dim passport1 As Passport = New Passport()
    passport1.account = "1234567" ' Your Company Account ID with Netsuite
    passport1.email = "[email protected]" 'Your/or user email on Netsuite, you nead to maintain the role related, for example in this give Invoice-View, Transaction Search-View too
    Dim role As RecordRef = New RecordRef()
    role.internalId = "3" ' 3 for admin , netsuite will active the 2 factor Auth soon , so use any role than Admin , and replace this ID 
    passport1.role = role
    passport1.password = "Your Password on Netsuite"
    '==== log in
    Dim Status As Status = service1.login(passport1).status
    '---- you can put the result in Textbox to see the result and steps too , or writeline works too
    TextBox1.Text = Status.isSuccess.ToString
    '==========
    TextBox1.Text = TextBox1.Text & " =================" & Environment.NewLine
    Dim _InvoiceTxnIds(2) As String  ' put the invoice Numbers in Array , or you can make a loop too , in this example it will search for 3 Invoice#80,#81 and #82 details 
    _InvoiceTxnIds(0) = "80"
    _InvoiceTxnIds(1) = "81"
    _InvoiceTxnIds(2) = "82"

    Dim SearchResult1 As SearchResult = New SearchResult()
    Dim Ts As TransactionSearch = New TransactionSearch()

    Dim TsBasic As TransactionSearchBasic = New TransactionSearchBasic()

    TsBasic.internalId = New SearchMultiSelectField()
    TsBasic.internalId.operator = SearchMultiSelectFieldOperator.anyOf
    TsBasic.internalId.operatorSpecified = True

    Dim Rrlist As List(Of RecordRef) = New List(Of RecordRef)()

    For Each sTxnId As String In _InvoiceTxnIds
        Dim rr As RecordRef = New RecordRef()
        rr.internalId = sTxnId
        Rrlist.Add(rr)
    Next
    '------ put search preferences 
    Dim _prefs As Preferences = New Preferences()
    _prefs.warningAsErrorSpecified = True
    _prefs.warningAsError = False
    service1.preferences = _prefs
    Dim _searchPreferences As SearchPreferences = New SearchPreferences()
    '_searchPreferences.pageSize = _pageSize
    _searchPreferences.pageSizeSpecified = True
    _searchPreferences.bodyFieldsOnly = False  ' important to make it false to let Netsuite return back the invoice item details 
    service1.searchPreferences = _searchPreferences
    '============
    service1.searchPreferences.bodyFieldsOnly = False

    TsBasic.internalId.searchValue = Rrlist.ToArray()
    Ts.basic = TsBasic

    Dim res As SearchResult = service1.search(Ts)

    TextBox1.Text = TextBox1.Text & " res.status.isSuccess value : " & res.status.isSuccess & Environment.NewLine
    If res.status.isSuccess Then
        Dim recordList As Record()
        TextBox1.Text = TextBox1.Text & " res.totalPages : " & res.totalPages & Environment.NewLine

        For i As Integer = 1 To res.totalPages

            recordList = res.recordList
            TextBox1.Text = TextBox1.Text & " recordList Lenghth #: " & recordList.Length & Environment.NewLine
            For j As Integer = 0 To recordList.Length - 1
                If TypeOf recordList(j) Is Invoice Then
                    Dim Inv As Invoice = CType((recordList(j)), Invoice)
                    TextBox1.Text = TextBox1.Text & " invoice #: " & Inv.tranId & Environment.NewLine
                    TextBox1.Text = TextBox1.Text & "Total amount: " & Inv.total & Environment.NewLine
                    If Inv.itemList IsNot Nothing Then
                        TextBox1.Text = TextBox1.Text & " invoice items list Qty #: " & Inv.itemList.ToString & Environment.NewLine

                        For k = 0 To Inv.itemList.item.Length - 1
                            Dim item As InvoiceItem = CType((Inv.itemList.item(k)), InvoiceItem)
                            TextBox1.Text = TextBox1.Text & " Item Name:" & item.item.name & ", quantity: " & item.quantity & Environment.NewLine
                            ' you can use the other values too like prices, amount, cost ....ETC
                        Next
                        TextBox1.Text = TextBox1.Text & "Total amount: " & Inv.total & Environment.NewLine

                    End If


                End If
            Next
        Next
    Else
        TextBox1.Text = TextBox1.Text & " Error:" & res.status.ToString & " ----" & Environment.NewLine

    End If





End Sub

Upvotes: 0

Charles
Charles

Reputation: 756

I found my answer in the "Suite Talk Web Services Platform Guide":

SuiteTalkWebServicesPlatformGuid_2012.1.pdf (Page 34, Setting Search Preferences.)

I have included my solution and code below in case the guide become unavailable at a future date.

bodyFieldsOnly
boolean
Defaults to TRUE and indicates that the information in the body fields of the record are returned — significantly improving performance. Any fields in associated lists or sublists are not returned. If the bodyFieldsOnly field is set to FALSE, all fields associated with the record are returned.

So I was missing setting my bodyFieldsOnly to false. Once it was set to false then I was getting back the full information desired.

/// <summary>
/// <p>This function builds the Pereferences and SearchPreferences in the SOAP header. </p>
/// </summary>
private void setPreferences()
{
    // Set up request level preferences as a SOAP header
    _prefs = new Preferences();
    _service.preferences = _prefs;
    _searchPreferences = new SearchPreferences();
    _service.searchPreferences = _searchPreferences;

    // Preference to ask NS to treat all warnings as errors
    _prefs.warningAsErrorSpecified = true;
    _prefs.warningAsError = false;
    _searchPreferences.pageSize = _pageSize;
    _searchPreferences.pageSizeSpecified = true;
    // Setting this bodyFieldsOnly to true for faster search times on Opportunities
    _searchPreferences.bodyFieldsOnly = false;
}

Upvotes: 7

Related Questions