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
Madhu3006Madhu3006 

To query the childs records in Account(if child are not present )

Hi Every One,

 

I wrote the code ,it works fine if the account is having opportunites.

some times there wont be an opportunities that have associated to Accounts

at that point of time.the code fails

here is the query what i am using

QueryResult rs =binding.query("SELECT Id,Name,(SELECT Name,Id FROM Opportunities )FROM Account where id='0019000000NR6Dg' ");

 

 

package com.sforce.enterprise.javaclass;

import java.lang.reflect.Field;
import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import org.apache.axis.wsdl.symbolTable.FaultInfo;

import com.sforce.async.QueryResultList;
import com.sforce.soap.enterprise.LoginResult;
import com.sforce.soap.enterprise.Logout;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.SessionHeader;
import com.sforce.soap.enterprise.SforceServiceLocator;
import com.sforce.soap.enterprise.SoapBindingStub;
import com.sforce.soap.enterprise.fault.LoginFault;
import com.sforce.soap.enterprise.fault.UnexpectedErrorFault;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.Case;
import com.sforce.soap.enterprise.sobject._case;
import com.sforce.soap.enterprise.sobject.CaseHistory;
import com.sforce.soap.enterprise.sobject.Contact;
import com.sforce.soap.enterprise.sobject.Opportunity;
import com.sforce.soap.enterprise.sobject.SObject;
import com.sforce.soap.enterprise.sobject.SObject.*;

public class ParentChildQuery {

private static final SObject ParentChildQuery = null;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

ParentChildQuery _cntQuery=new ParentChildQuery();
try {
_cntQuery.queryChildRec();
} catch (UnexpectedErrorFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LoginFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}

public void queryChildRec() throws UnexpectedErrorFault, LoginFault,
RemoteException, ServiceException
{
try
{
SoapBindingStub binding= (SoapBindingStub) new SforceServiceLocator().getSoap();
LoginResult lr=binding.login("XXx", "XXX");
//Reset the binding to use the endpoint returned from login
String Seid=lr.getSessionId();
System.out.println("seesion Id is"+Seid);
binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, lr.getServerUrl());
//Create the session id header, and add it to the proxy binding
SessionHeader sh=new SessionHeader();
SessionHeader shd=new SessionHeader();
shd.setSessionId(Seid);
sh.setSessionId(lr.getSessionId());
System.out.println();
binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(),"SessionHeader",sh);
//String res="SELECT LastName,(SELECT Name FROM Opportunities )FROM Contact Where SendtoSAP__c=TRUE";
//QueryResult rs =binding.query("SELECT FirstName, LastName,(SELECT Name,Id FROM Opportunities where StageName='Prospecting' )FROM Contact Where SendtoSAP__c=TRUE ");
//QueryResult rs =binding.query("SELECT FirstName, LastName,(SELECT Name,Id FROM Opportunities )FROM Contact Where SendtoSAP__c=TRUE ");
QueryResult rs =binding.query("SELECT Id,Name,(SELECT Name,Id FROM Opportunities )FROM Account where id='0019000000NR6Dg' ");

//QueryResult rs =binding.query("SELECT CaseNumber,Id,AccountId FROM Case where AccountId != null"); //SaveResult rs=binding.create(sObjects)
SObject So=rs.getRecords()[0];
System.out.println("Lenght of the records" +rs.getSize());
//Contact =(Contact)(So.getId());
int loopCount = 0;
boolean done = false;
while(!done)
{
SObject[] records = rs.getRecords();
for (int i = 0; i < records.length; i++) {
Account acct=(Account) records[i];
//Contact contact =(Contact) records[i];
String fName=acct.getName();
String lName=acct.getId();
//String _cntid=contact.getId();
//Integer optynmae=acct.getCases().getSize();
//System.out.println("TOtal Cases that are associated to Account -------"+optynmae);
String optyname1 = null;
String optyId=null;
SObject [] oprtrecords = null;
QueryResult optyname=acct.getOpportunities();
System.out.println("optysize" +optyname.getSize());
if(optyname.getSize()>0)
{
int j = 0;
oprtrecords=optyname.getRecords();
for( j=0;j<oprtrecords.length;j++)

{



Opportunity casesrec= (Opportunity) oprtrecords[j];
String acctidincases=casesrec.getAccountId();
// if(acctidincases.isEmpty()&& acctidincases!=null){
//Case opty=(Case) oprtrecords[j];
optyname1= casesrec.getName();
optyId = casesrec.getId();
//casesrec.getAccountId()

System.out.println("Casenumber-------" +optyname1+"CaseId---------"+optyId);
//}


}
}
else{
System.out.println("No Asscocaited Opty for this account");
}
if (fName==null) {
System.out.println("AccountName " + (i + 1) + "CoditionPrinted " + lName);
} else {
System.out.println("Accountid " + (i + 1) + ": " + fName
+ " " + lName);
}
if (rs.isDone()) {
done = true;
} else
{
rs = binding.queryMore(rs.getQueryLocator());
}
System.out.println("\nQuery succesfully executed.");

}
}

binding.clearHeaders();
binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(),"SessionHeader", sh);
}catch(Exception e)
{
System.out.println(e.getMessage());

}


}

}

 

Please suggets me how to proceed.

regards

Madhu

SRKSRK
you write
QueryResult optyname=acct.getOpportunities();
i believe you r code is failing at this point of time