SuiteTalk in C# – Direct casting of types

C# is a Typeful language. That can cause problems when coding SuiteTalk in C#. Ever search for an item, entity or transaction and want to turn right around and update it, not knowing it’s type? Yep… I’ve been there too.

In order to build out this example, I’m going to just clip some code. I don’t have a good working example. However, I think you’ll get the idea.

So you search for an item record like this. I’m searching by external id. However, what you get back could be an object of any number of different types.

Search Example

If the search was successful, it will return:

(Record) result.recordList[0]

The problem…

What you need is not an object of type Record, but an Item (the base type) which can be cast to a type of one of the following: InventoryItem, SerializedInventoryItem, NonInventoryItemForSale, and so on. There are a bunch.

var UpdateItem = new Record();

What you really want is something that looks like this:

var updateItem= new InventoryItem() {…};
var updateItem= new SerializedInventoryItem() {…};
var updateItem= new NonInventoryItemForSale() {…};

Or something like this:

var updateRecRef= new RecordRef() { type = RecordType.InventoryItem, …};
var updateRecRef= new RecordRef() { type = RecordType.NonInventoryItem, … };
var updateRecRef= new RecordRef() { type = RecordType.NonInventoryForSale, … };

Here are two examples that take care of all the casting without knowing the object type of result.recordList[0].

var UpdateRec = Activator.CreateInstance(result.recordList[0].GetType());

Type type = result.recordList[0].GetType();
PropertyInfo pi = type.GetProperty(“internalId”);
string internalId = pi.GetValue(result.recordList[0]).ToString();
pi.SetValue(UpdateRec, internalId);

pi = type.GetProperty(“externalId”);

string externalId = pi.GetValue(result.recordList[0]).ToString();
pi.SetValue(UpdateRec, externalId);



var UpdateRecRef = RecordRef() {
     type = (RecordType) Enum.Parse(typeof(RecType), result.recordList[0].GetType().Name, true)


Both these examples handle the conversion from type = Record to type of some flavor of Item. In the second example, the type is mapped to one of the Enum options of RecordType.InventoryItem, RecordType.SerializedInventoryItem, RecordType.NonInventoryItemForSale and so on.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s