简体   繁体   中英

Getting Fields Values as Null from Saved Search in Netsuite

I created a saved search in Netsuite. I'm trying to get the field values through the web services call from my application. I am getting total records size correctly, but while trying to get the fields values I'm getting null for all fields(example, itemname) from my saved search. Here is the code:

TransactionSearchAdvanced tsa = new TransactionSearchAdvanced();
tsa.setSavedSearchId("825");
SearchResult result = _port.search(tsa);
System.out.println("size of recds..." + result.getTotalRecords());

if (result.getTotalRecords() > 0) {
    // retain the search ID in order to get more pages 
    String sSearchId = result.getSearchId();
    SearchRowList rowList = result.getSearchRowList();
    processRowList(rowList);
    int iNumPages = result.getTotalPages();
    System.out.println("pages" + iNumPages);
    for (int i = 2; i <= iNumPages; i++) {
        result = _port.searchMoreWithId(sSearchId, i);
        rowList = result.getSearchRowList();
        System.out.println("jjjj" + rowList);
        processRowList(rowList);
    }
}

_port.logout();

public void processRowList(SearchRowList rowList) {
    try {
        System.out.println("inside process...." + rowList);
        for (int i = 0; i < rowList.getSearchRow().length; i++) {
            TransactionSearchRow ts = (TransactionSearchRow) rowList.getSearchRow(i);
            TransactionSearchRowBasic tsb = ts.getBasic();
            SearchColumnSelectField[] scsf = tsb.getItem();
            for (SearchColumnSelectField sf: scsf) {
                RecordRef rf = sf.getSearchValue();
                System.out.println("itemname:::" + rf.getName());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I am new to Netsuite but I was desperate trying to solve the same problem, and I think I have the answer. I think that, when you try to print the items names, you are trying to iterate over all the possible fields that a row may have. However, this "item" object that the TransactionSearchRowBasic contains is not the list of items that this object may contain. Then, you must try to get the values that your transaction saved search do contain.

So, let's say that I have a transaction saved search in which I want to show the transaction's period, type, account, amount, date, and the external ID of the related department. First, I looked in this link for the API ID of each record compared to it's UI name:

https://system.netsuite.com/help/helpcenter/en_US/RecordsBrowser/2012_2/Records/transaction.html

Then I used a very similar java code as yours but with some fixes.

This method verifies that everything is ok and calls the method that really prints the data fields:

public void printTransactionRowFields() throws ExceededUsageLimitFault, UnexpectedErrorFault, InvalidSessionFault, ExceededRecordCountFault, RemoteException, UnsupportedEncodingException, SOAPException
    {
        TransactionSearchAdvanced tsa = new TransactionSearchAdvanced();
        //This is my Transaction saved search Internal ID
        String savedSearchID = "999";
        tsa.setSavedSearchId(savedSearchID);

        SearchResult srr = _port.search(tsa);
        boolean isSuccess = srr.getStatus().isIsSuccess();
        System.out.println("Search result: "+ isSuccess);
        int searchNumRecords = srr.getTotalRecords();
        System.out.println("Amount of records: "+ searchNumRecords);
        if (isSuccess) 
        {
            if(searchNumRecords > 0)
            {
                int iNumPages = srr.getTotalPages();
                //Must check the amount of pages to process every record
                System.out.println("Num pages: "+ iNumPages);
                //Must retain search ID to search for more pages. This is a web service ID, it's not the same as the internal ID "999"
                String sSearchID = srr.getSearchId();
                System.out.println("Search id: "  + sSearchID);
                SearchRowList rowList = srr.getSearchRowList();
                System.out.println("Num rows in row list 1: " +rowList.getSearchRow().length);
                processReportRowList(rowList);
                //We process the next iNumPages pages (it's not 0 index)
                for ( int i=2; i<=iNumPages; i++) {
                    //Users who authenticate to NetSuite by providing their credentials in the SOAP header of
                    //their requests must use searchMoreWithId to retrieve search results that span multiple pages.
                    //They cannot use searchMore or searchNext, since both operations require an active session.
                    //(For information on request-level-credential authentication, see Request-Level Credentials.)
                    srr = _port.searchMoreWithId(sSearchID, i);
                    rowList = srr.getSearchRowList();
                    System.out.println("Num rows in row list #" +i+": " +rowList.getSearchRow().length);
                    processReportRowList(rowList);
                } 
            }else{
                System.out.println("This saved search contains no records to process.");
            }
        }
        else{
            System.out.println("Search could not find Transaction Saved Search with id: " + savedSearchID);
        }
    }

This method handles the printing of my transaction's period, type, account, amount, date, departmentName fields:

public void processReportRowList(SearchRowList rowList) 
    {
        if(rowList!= null){
            System.out.println("Search row length: " + rowList.getSearchRow().length);
            for (int i=0; i<rowList.getSearchRow().length; i++) {
                TransactionSearchRow row=(TransactionSearchRow)rowList.getSearchRow(i); 
                TransactionSearchRowBasic basic= row.getBasic();
                System.out.println((basic==null)?"Basic is Null.":"Basic is not null.");
                RecordRef period = basic.getPostingPeriod(0).getSearchValue();
                String periodInternal = period.getInternalId();
                String  type = basic.getType(0).getSearchValue();
                RecordRef account = basic.getAccount(0).getSearchValue();
                Double amount = basic.getAmount(0).getSearchValue();
                Double amountFC = basic.getFxAmount(0).getSearchValue();
                Calendar tranDate = basic.getTranDate(0).getSearchValue();
                DepartmentSearchRowBasic deparmentRow = row.getDepartmentJoin();
                String departmentExternalID = deparmentRow.getExternalId(0).getSearchValue().getInternalId();
                //Print results
                System.out.println("Row# "+i+"\n"+"Period Internal ID: "+periodInternal +"\n Type: "+ type+ "\n Account Internal ID: "+
                accountInternal + "\n Amount: "+ amount + "Foreign Currency Amount: "+ amountFC + "\n Date: " + tranDate.getTime().toString()+
                "\n Department External ID: "+departmentExternalID);
            }

        }else{
            System.out.println("Row list arrived null");
        }
    }

Notice that I had to create a DepartmentSearchRowBasic as I am trying to get the department external ID from the Department record. In my saved search, I selected that department external ID with the "Department Fields..." option.

If you get lost with which method over your TransactionSearchRowBasic object will return you the correct field you selected in your saved search result, you can use Eclipse's debug mode to see which fields are not null and what types they return and contain.

Hope this answer helps all people desparate with the lack of good Netsuite documentation! (Though, that's only my opinion)

Here is an example that performs a search and loops through the results and assigns the Subsidiary name ( Record = Subsidiary, Field Name = name ) to a variable. The variable value is re-assigned with each occurrence of loop and for illustration purposes only.

I think you were looking for the getFieldValue function.

Hint : Use the NetSuite Records Browser & Schema Browser for a valid list of records and columns.

var rec = '';
var subsidiaryName = '';
var searchResults = nlapiSearchRecord( 'subsidiary', null, null, null );
for ( var i = 0; i < Math.min( 100, searchResults.length ); i++)
{
    var record = nlapiLoadRecord(searchResults[i].getRecordType(),searchResults[i].getId() );
    subsidiaryName = record.getFieldValue('name');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM