function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
venturec3venturec3 

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>();
accountList.add(acct);

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.