+ Start a Discussion

Dynamically cast to an sObject without knowing what sobject will be coming up next in a listbox

Is there a way to dynamically cast an sobject using the name of that object as a string?

For example, generally if I'm updating a specific sobject, I might do something like:

Account acct = new Account();
acct.Name = "Test Account";

List<Account> accountList = new List<Account>();

SaveResult[] sr = prodConnection.upsert(accountList.ToArray());

However, I may not know whether the user selects an Account, Contact or Opportunity, etc.?

What solutions have others come up with on this when inserting/upserting records into Salesforce from C#?
Vamsi KrishnaVamsi Krishna
you can try something like this

sObject sObj = Schema.getGlobalDescribe().get(ObjectName).newSObject();

but if you want to access specific fields of the object you might end up casting the generic sObject to that specific type.