Jack
Jack

Reputation: 10037

How do you search with TransactionSearch?

How do you find a Vendor Payment (Transaction) with an Vendor Payment internal id? I'm having a difficult time trying to figure out how Transaction search work.

Below is my code: it return success but no result

using (var serviceClient = new ServiceClient(token))
        {
            var tranSearch = new TransactionSearchAdvanced();

            var recordRefs = new List<RecordRef>();
            recordRefs.Add(new RecordRef()
            {
                internalId = @"723212",
                type = RecordType.vendorPayment,
                typeSpecified = true
            });

            var types = new List<string>();
            types.Add(@"_vendorPayment");
            tranSearch.criteria = new TransactionSearch()
            {
                basic = new TransactionSearchBasic()
                {
                    internalId = new SearchMultiSelectField()
                    {
                        @operator = SearchMultiSelectFieldOperator.anyOf,
                        operatorSpecified = true,
                        searchValue = recordRefs.ToArray()
                    },
                    type = new SearchEnumMultiSelectField()
                    {
                        @operator = SearchEnumMultiSelectFieldOperator.anyOf,
                        operatorSpecified = true,
                        searchValue = types.ToArray()
                    }                        
                }                    

            };                

            var result = serviceClient.search(tranSearch);



        }

The search result return success however there is no record in recordlist or search in searchrowlist.

Upvotes: 1

Views: 1013

Answers (1)

Mike Robbins
Mike Robbins

Reputation: 3287

Think of TransactionSearchAdvanced() like a saved search in the UI. You need to specify the criteria to use AND the columns that you want to return. Before you run your search, add the following code to specify the columns to return. You'll probably want to add a criteria for mainLine is true as well or you'll get one result per transaction line rather than one result per transaction.

tranSearch.columns = new TransactionSearchRow()
{
    basic = new TransactionSearchRowBasic()
    {
        tranId = new[] {new SearchColumnStringField()}
    }
};

var result = ns.search(tranSearch);

if (result.status.isSuccess)
{
    foreach (var rowList in result.searchRowList)
    {
        if (rowList is TransactionSearchRow row)
        {
            var tranId = row.basic.tranId[0].searchValue;
            var total = row.basic.total[0].searchValue;
            Console.WriteLine($"{tranId} - {total}");
        }
    }
}

Easiest Method: Since you have the internal ID, you can use TransactionSearchBasic() to get the entire record like this:

var search = new TransactionSearchBasic()
{
    type = new SearchEnumMultiSelectField()
    {
        @operator = SearchEnumMultiSelectFieldOperator.anyOf,
        searchValue = new[] { "_vendorPayment" },
        operatorSpecified = true,
    },
    internalId = new SearchMultiSelectField()
    {
        @operator = SearchMultiSelectFieldOperator.anyOf,
        searchValue = new[] { new RecordRef { internalId = "723212" } },
        operatorSpecified = true
    }
};

var results = ns.search(search);

foreach (var result in results.recordList)
{
    if (result is VendorPayment vendorPayment)
    {
        Console.WriteLine(vendorPayment.tranId);
    }
}

Upvotes: 2

Related Questions