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
odieodie 

Webservices - getting child nodes in SObject

Hi,

 

I am using WebServices to query SF and am using the SearchResult Object. I am having trouble manipulating the result Object. My search statement is as follows. Please note that I want the child object account name.

sr = binding.search("find {4083068888 OR 4083069999 OR 4083067777} in phone fields returning "+

" contact(id, phone, firstname, lastname, account.name), "+

" lead(id, phone, firstname, lastname),"+

" account(id, phone, name)");

I am using the code below to access the result values but I am unable to get the child object.

 

SearchRecord[] records = sr.getSearchRecords();

Vector contacts = new Vector();

Vector leads = new Vector();

Vector accounts = new Vector();

if (records != null && records.length > 0) {

for (int i=0;i<records.length;i++){

SObject record = records[i].getRecord();

if (record.getType().toLowerCase().equals("contact")) { contacts.add(record);

} else if (record.getType().toLowerCase().equals("lead")){ leads.add(record);

} else if (record.getType().toLowerCase().equals("account")) { accounts.add(record);

}

}

if (contacts.size() > 0) {

System.out.println("Found " + new Integer(contacts.size()).toString() + " contacts:"); for (int i=0;i<contacts.size();i++){

SObject c = (SObject)contacts.get(i);

System.out.println(c.getId() + // Id, works c.get_any()[1].getValue() + //Last Name, works

c.get_any()[2].getValue() + //First name, works c.get_any()[3].getValue()); //Account name, does NOT work. not sure how i should get the value for the Name tag out.

}

}


 Any pointers would be appreciated. How do you get a hold of child nodes in the SObject?

 

thanks,

Odie 

 

 

 

Message Edited by odie on 03-02-2009 01:35 PM
Best Answer chosen by Admin (Salesforce Developers) 
odieodie

Finally got this to work. Thanks Simon, for all the help.

 

 

Iterator iter = c.get_any()[3].getChildElements();

while(iter.hasNext()){

MessageElement m = (MessageElement)iter.next();

if(m.getName().equalsIgnoreCase("name"))

System.out.println("--------------"+m.getValue());

}

 

 

 

Message Edited by odie on 03-02-2009 02:48 PM

All Answers

SuperfellSuperfell

the field(s) from account will be in a child element called account

 

sobject

type contact 

firstname foo

lastname bar

account

type account

name foobar inc.

 

If you dump the raw xml it should be see the nested structure.

odieodie

Simon, thanks for the response. 

I did dump the response and see the structure that you mentioned. How do you programatically manipulate it ? How Do i extract the value of Name ?

thanks,

Oide 

 

 

<ns1:Account xsi:type="sf:sObject" xmlns:ns1="urn:sobject.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ns1:type>Account</ns1:type> <ns1:Id xsi:nil="true"/> <ns1:Name>ACB</ns1:Name></ns1:Account>

 

 

 

SuperfellSuperfell
get_any typically returns an xmlelement type object, check the docs for whatever soap stack you're using.
odieodie

Simon,

 

Get_any gives the value of the root element (null in this case). It does not fetch the child object. I tried chacking the Axis docs (the SOAP toolkit I'm using is Axis) and didnt find much there.

 

Odie 

SuperfellSuperfell
because you're calling getValue on the object from the any. that object is a like a DOM node, you can traverse all the child elements as well.
odieodie

Simon, thanks for your inputs,

 

that is what I was trying and looks like the below statement should work, but it does not. I'm positive I'm looking at the right node (getNodeName give the correct value of "Name"), not sure why I'm not getting the value.

 

c.get_any()[3].getChildNodes().item(2).getNodeValue()); 

 

once again the doc structure is :

 

 

<ns1:Account xsi:type="sf:sObject" xmlns:ns1="urn:sobject.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<ns1:type>Account</ns1:type>

<ns1:Id xsi:nil="true"/>

<ns1:Name>ACB</ns1:Name>

</ns1:Account>

 

 

 

 

Message Edited by odie on 03-02-2009 02:18 PM
SuperfellSuperfell
isn't it c.get_any()[3].getChildNodes().item(2).getValue()
odieodie

Nope, get value does not exist for type Node. Dosent complie.

getNodeValue compiles (but dosent work :)

odieodie

Finally got this to work. Thanks Simon, for all the help.

 

 

Iterator iter = c.get_any()[3].getChildElements();

while(iter.hasNext()){

MessageElement m = (MessageElement)iter.next();

if(m.getName().equalsIgnoreCase("name"))

System.out.println("--------------"+m.getValue());

}

 

 

 

Message Edited by odie on 03-02-2009 02:48 PM
This was selected as the best answer