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
pathworkspathworks 

Can I query for the Name of an arbitrary object type?

 
Using the API, can I query for the Name of an arbitrary object, given only its id?
 
I'm trying to workaround the bug in the HYPERLINK formula where I cannot pass a text field (such as {!Name}) to my application (because of the lack of URL encoding).  Therefore, I'm trying to pass only the object id.  This could be for any object type.  I then plan to retrieve the name using the API.
 
Is there a generalized way to do this?  It could be for any object type (custom or not).  I tried having the "object type" passed in on the query string, and doing a
 
QueryResult [] qr = binding.query("select Name from " + objecttype + " where id = '" + id + "');
 
However, the returned SObject type does not have a Name property, and I can't dynamically cast it to something that does, as it could be any custom object. 
 
I'm currently tinkering which trying to grab it from the raw XML, maybe by using the getSerializer() method...but am not sure how much luck I'll have.
 
Thanks,
Dan
 

Message Edited by pathworks on 08-02-2006 10:19 AM

SuperfellSuperfell
If you call describeSObject on the objectType, each field has a flag that indicates whether its the "Name" field for that objectType. (the flag is called nameField IIRC)
pathworkspathworks
Thanks, that sounds promising.  I think the only remaining issue is how to get that field back from the query results.  The SObject class has no getField(String fieldName) method that I can find.  So I think I'm left with this
 
QueryResult qr = binding.query("select " + nameField + " from " + objectType + " where id = '" + id + "'");
SObject [] objects = qr.getRecords();
if (objects != null && objects.length == 1) {
 // how do I get the property for "nameField" here...?
}
 

Message Edited by pathworks on 08-02-2006 10:19 AM

SuperfellSuperfell
This is probably way easier with the partner API, you'd be able to do something like.

QueryResult qr = binding.query("select " + nameField + " from " + objectType + " where id = '" + id + "'");
SObject [] objects = qr.getRecords();
if (objects != null && objects.length == 1) {
 String name = objects[0].getAny(0).getValue();
}

pathworkspathworks
 
Interesting.  Do you know if that will work for an arbitrary type?  For example, if I use our Partner WSDL to generate client bindings...but then our integration is used by another organization with a new object type, say "Pet", can I still query on Pet and expect the deserialization to an SObject to work?  Or will it complain about an unrecognized type?
 
I have a feeling that to do this completely generically (for any custom objects), I'm going to have to go one level lower and not rely on the ws client bindings from the wsdl...
 
Sorry if some of this is a bit naive here.
 
Dan
 
 
SuperfellSuperfell
The partner WSDL is entirly generic and will work for any organization.
pathworkspathworks
 
Thanks.  Now that I think about it more, I think that's what I'm currently using.  I don't see the getAny() method though; so I'll rebuild from the WSDL and see if that shows up.
 
Thanks,
Dan
 
pathworkspathworks

I switched to the partner API, and it looks to be much easier - getting the name field from describeSObject, and then getting the value with getAny()[0].getValue().  Thanks!

Dan

 

pathworkspathworks
 
A couple of interesting things came up.  First, the Lead object has two name fields, as returned by describeSObject.  The first in the list is LastName, the second is FirstName.  I assume I should query for both, and join them with a space in the middle.  I can do that - can I safely rely on the Name fields returned from describeSObject to be returned in the reverse order from their use when concatenated into the Name?  Or is there some other way to determine how to concatenate multiple name fields?
 
Second, when I run a "select LastName from Lead where id = '" + id"'" I get back an SObject [] with length 1.  When I call get_any() on the only object returned, I get a MessageElement [] result of null.  So it seems to not contain any values.  I'm sure I'm doing something wrong:
 
         QueryResult qr = binding.query("select " + nameField + " from " + type + " where id = '" + id + "'");
         SObject [] objects = qr.getRecords();
         if (objects == null || objects.length != 1) {
          response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No object with id " + id + " found.");
          return;
         }
         MessageElement [] elems = objects[0].get_any();
         if (elems == null || elems.length != 1) {
          // CURRENTLY ALWAYS ENDS UP HERE WITH NULL ELEMS
          response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "No properties returned on object.");
         }
         String name = elems[0].getValue();
Dan
 

Message Edited by pathworks on 08-02-2006 05:23 PM

pathworkspathworks
 
I still can't quite seem to get this working.  I switched to using the Partner web services.  Two problems/questions:
 
- If an SObject has multiple Name fields (as returned from describeSObject), what order should I concatenate them in to generate the actual name?
 
- getAny() always returns null (after I do a "select " + nameField + " from " + type + " where id = '" + id + "'")
 
Dan
 
SuperfellSuperfell
Axis will "help" you by removing any null elements from the any collection, this is likely causing the problems you're running into. I think its fixed in the newer versions of axis.
pathworkspathworks

Ok - the lead had an empty first name.  When I change the code to grab multiple name fields and concatenate them it will fix this (currently the code just picks the "last" name field from the for loop).  Which order should I concatenate them in?

Thanks,

Dan

 

SuperfellSuperfell
Unfortuantly you've ran into a hole, we don't describe the order for compound name fields. (which is additionally complicated by locale specific rules).
pathworkspathworks

 

Aha - locale specificity was my "next question".  I guess I'll just append them in a random order and hope for the best.

Thanks,

Dan