You need to sign in to do that
Don't have an account?
Michael Boland
Create new object from string parameter?
Hey everyone,
Hello SalesForce World, first time posting!
I am working on a User Management C# service from our internal systems to SalesForce.
I wanted to bring back the ID, Name values for sObjects using a generic function that returned the value pair. But I wanted to just pass the sObject type as a string to a function that returns the key pair values.
Has anyone done this before? I really haven't found a way myself to create an sObject with a string parameter.
Richard. D. James
Hi Richard,
I think something like this might help. I would like to know from others if they have another way of doing this so I might be able to refactor my code.
I had sort of the same scenario. The "cmdb_" objects were used to query data I had in Salesforce.
So I would get the objectType, pass it to my method and build the query string.
In my external system, I had the ability to get the object type, so I passed that to this method then mapped it to my SF types.
objectType = records[0].sys_class_name;
private static string get_sfObjectFromExternalID(string objectType, string ID)
{
string obj = "";
if (objectType == "cmdb_ci_service")
obj = "cmcloud__Business_Service__c";
else if (objectType == "cmdb_ci_appl")
obj = "cmcloud__Application__c";
else if (objectType == "cmdb_ci_db_instance")
obj = "cmcloud__Database_Instance__c";
else if (objectType == "cmdb_infra_service")
obj = "cmcloud__Application_Server__c";
else if (objectType == "cmdb_ci_server")
obj = "cmcloud__Computer__c";
String soqlQuery = "SELECT id, cmcloud__Object_Id__c, cmcloud__IT_Asset__c " +
" FROM " + obj +
"WHERE cmcloud__External_ID__c = '" + ID + "'";
try
{
QueryResult query = sfdc.query(soqlQuery);
----
Hello,
You can do by this way. I hope this will help you.
1) This utility takes the input as List<sobjects> and the desired field name, which inturn returns the unique values of the specified field names
public static Set<String> pickDesiredSet(List<SObject> sobjects, String fName){
Set<String> strSet = new Set<String>();
if(sobjects != null && sobjects.size() > 0 && fName != null && fName.trim() != null)
for(SObject sobj : sobjects)
strSet.add(String.valueOf(sobj.get(fName)));
return strSet;
}
2) This utility takes the input as List<sobjects>, key and pair, which inturn returns the key and pair as a map
public static Map<String, String> pickDesiredMap(List<SObject> sobjects, String key, String pair){
Map<String, String> strMap = new Map<String, String>();
if(sobjects != null && sobjects.size() > 0 && key != null && key.trim() != null)
for(SObject sobj : sobjects)
if(!strMap.containsKey(key)) //To keep only the first value
strMap.put(String.valueOf(sobj.get(key)), String.valueOf(sobj.get(pair)));
return strMap;
}
Regards,
Keyur Patel
I'm in agreement with Don Hobson's concept.
If you have the sObject type name, you can write a query for the Id and Name: (C# code)
You could, of course, add WHERE and other such items to the query to further limit, it is a standard SOQL query.