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
ckempckemp 

SOQL Joins via API

In our Java code, I'm doing something like this with the Partner API:

 

 

queryResult = binding.query("SELECT User.Id, UserName, FirstName, LastName, Email, Alias, IsActive, CompanyName, " + "Street, City, State, Country, PostalCode, Phone, LanguageLocaleKey, Profile.Name FROM User");

 

records = this.loadQueryResults(queryResult);

 

for (HashMap<String, String> record: records) { log.debug("Salesforce ID: " + record.get("id").toString()); log.debug("Username: " + record.get("username").toString()); log.debug("First Name: " + record.get("firstname").toString()); log.debug("Last Name: " + record.get("lastname").toString()); log.debug("Email: " + record.get("email").toString()); log.debug("Alias: " + record.get("alias").toString()); log.debug("Is Active?: " + record.get("isactive").toString()); log.debug("Company Name: " + record.get("companyname").toString()); log.debug("Street: " + record.get("street").toString()); log.debug("City: " + record.get("city").toString()); log.debug("Province: " + record.get("state").toString()); log.debug("Country: " + record.get("country").toString()); log.debug("Postal Code: " + record.get("postalcode").toString()); log.debug("Phone: " + record.get("phone").toString()); log.debug("LanguageLocaleKey: " + record.get("languagelocalekey").toString()); log.debug("Profile: " + record.get("name").toString()); }

 

And here is loadQueryResults():

 

 

private ArrayList<HashMap<String, String>> loadQueryResults(QueryResult qr) { ArrayList<HashMap<String, String>> returnVal = new ArrayList<HashMap<String, String>>(); if (qr.getSize() > 0) { boolean keepLooping = true; while (keepLooping) { for (int i = 0; i < qr.getRecords().length; i++) { MessageElement[] records = qr.getRecords(i).get_any(); HashMap<String, String> fields = new HashMap<String, String>(); if (qr.getRecords(i).getId() != null) { fields.put("id", qr.getRecords(i).getId()); } for (int j = 0; j < records.length; j++) { MessageElement record = records[j]; if (!fields.containsKey(record.getName())) { fields.put(record.getName().toLowerCase(), record.getValue()); if (record.getValue() == null) { System.out.println(record.getName().toLowerCase() + " ==> [null]"); } else { System.out.println(record.getName().toLowerCase() + " ==> " + record.getValue().toString()); } } } returnVal.add(fields); } if (qr.isDone()) { keepLooping = false; } else { try { qr = binding.queryMore(qr.getQueryLocator()); } catch (InvalidQueryLocatorFault e) { log.error("Failed to query, error message was: \n" + e.getExceptionMessage()); } catch (ApiFault af) { log.error("Failed to query, error message was: \n" + af.getExceptionMessage()); } catch (RemoteException e) { e.printStackTrace(); } } } } return returnVal; }

 

 

 

I can get the User.Id value, but cannot get Profile.Name (it prints out "profile ==> [null]" for that value.)  Does anyone know how I can get this without having to do another query?  When I do the same query in SoqlXplorer, it returns me the Profile.name value just fine so I know it is possible.

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
the fields from Profile (and any other object that you join to) are contained in a nested Profile object, so you don't have a flat xml tree as your code assumes, to get profile.name, you'll need to look for the name element inside the profile element.