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
JuiceJuice 

Query using multiple filter criteria?

Does anyone have a sample of a XML SOAP query request that has two or more filter criteria?  For example, retrieve all the opportunities where

stage = "Closed Won" and

CloseDate is greater than "2002-06-01" and

CloseDate is less than "2003-06-01"

 

DevAngelDevAngel

Hi Juice,

Check out the sample code for Query using Filter

http://www.sforce.com/us/docs/QueryFilterJava.zip

 

 

JuiceJuice
I'm actually developing using a tool that requires the XML SOAP request.  Do you have a sample of the full XML SOAP request that's sent to sforce.com?
DevAngelDevAngel

Hi Juice,

No problem.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="salesforce" xmlns:types="salesforce/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Header>
    <tns:headerStruct id="h_id1">
      <session_id xsi:type="xsd:string">7Sal2fXPK.gip.WHElfBqYGFErRqF6lSHPbKEEVfrfa7xKD86MQ1KN35LXGNiDzIdgVl7EqHiQdjIEIAI5FU3vbtzKggVlhL</session_id>
      <version xsi:type="xsd:string">2.0</version>
    </tns:headerStruct>
  </soap:Header>
  <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <q1:query xmlns:q1="sfconnector:SalesforceConnector">
      <scope xsi:type="xsd:string">filter</scope>
      <type xsi:type="xsd:string">contact</type>
      <maxRows xsi:type="xsd:int">20</maxRows>
      <select href="#id1" />
      <filter href="#id2" />
      <ifModifiedSince xsi:type="xsd:dateTime">0001-01-01T00:00:00.0000000-08:00</ifModifiedSince>
      <useCaseSafeIDs xsi:type="xsd:boolean">0</useCaseSafeIDs>
    </q1:query>
    <soapenc:Array id="id1" soapenc:arrayType="xsd:string[6]">
      <Item>lastName</Item>
      <Item>firstName</Item>
      <Item>email</Item>
      <Item>title</Item>
      <Item>leadSource</Item>
      <Item>id</Item>
    </soapenc:Array>
    <soapenc:Array id="id2" soapenc:arrayType="xsd:anyType[1]">
      <Item href="#id3" />
    </soapenc:Array>
    <soapenc:Array id="id3" soapenc:arrayType="tns:mapEntry[2]">
      <Item href="#id4" />
      <Item href="#id5" />
    </soapenc:Array>
    <tns:mapEntry id="id4" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">value</key>
      <value href="#id6" />
    </tns:mapEntry>
    <tns:mapEntry id="id5" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">operator</key>
      <value xsi:type="xsd:string">or</value>
    </tns:mapEntry>
    <soapenc:Array id="id6" soapenc:arrayType="xsd:anyType[2]">
      <Item href="#id7" />
      <Item href="#id8" />
    </soapenc:Array>
    <soapenc:Array id="id7" soapenc:arrayType="tns:mapEntry[2]">
      <Item href="#id9" />
      <Item href="#id10" />
    </soapenc:Array>
    <soapenc:Array id="id8" soapenc:arrayType="tns:mapEntry[2]">
      <Item href="#id11" />
      <Item href="#id12" />
    </soapenc:Array>
    <tns:mapEntry id="id9" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">value</key>
      <value href="#id13" />
    </tns:mapEntry>
    <tns:mapEntry id="id10" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">operator</key>
      <value xsi:type="xsd:string">and</value>
    </tns:mapEntry>
    <tns:mapEntry id="id11" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">value</key>
      <value href="#id14" />
    </tns:mapEntry>
    <tns:mapEntry id="id12" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">operator</key>
      <value xsi:type="xsd:string">and</value>
    </tns:mapEntry>
    <soapenc:Array id="id13" soapenc:arrayType="xsd:anyType[2]">
      <Item href="#id15" />
      <Item href="#id16" />
    </soapenc:Array>
    <soapenc:Array id="id14" soapenc:arrayType="xsd:anyType[2]">
      <Item href="#id17" />
      <Item href="#id18" />
    </soapenc:Array>
    <soapenc:Array id="id15" soapenc:arrayType="tns:mapEntry[3]">
      <Item href="#id19" />
      <Item href="#id20" />
      <Item href="#id21" />
    </soapenc:Array>
    <soapenc:Array id="id16" soapenc:arrayType="tns:mapEntry[3]">
      <Item href="#id22" />
      <Item href="#id23" />
      <Item href="#id24" />
    </soapenc:Array>
    <soapenc:Array id="id17" soapenc:arrayType="tns:mapEntry[3]">
      <Item href="#id25" />
      <Item href="#id26" />
      <Item href="#id27" />
    </soapenc:Array>
    <soapenc:Array id="id18" soapenc:arrayType="tns:mapEntry[3]">
      <Item href="#id28" />
      <Item href="#id29" />
      <Item href="#id30" />
    </soapenc:Array>
    <tns:mapEntry id="id19" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">field</key>
      <value xsi:type="xsd:string">ownerID</value>
    </tns:mapEntry>
    <tns:mapEntry id="id20" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">value</key>
      <value xsi:type="xsd:string">00530000000bxGz</value>
    </tns:mapEntry>
    <tns:mapEntry id="id21" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">operator</key>
      <value xsi:type="xsd:string">equals</value>
    </tns:mapEntry>
    <tns:mapEntry id="id22" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">field</key>
      <value xsi:type="xsd:string">title</value>
    </tns:mapEntry>
    <tns:mapEntry id="id23" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">value</key>
      <value xsi:type="xsd:string">President</value>
    </tns:mapEntry>
    <tns:mapEntry id="id24" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">operator</key>
      <value xsi:type="xsd:string">starts with</value>
    </tns:mapEntry>
    <tns:mapEntry id="id25" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">field</key>
      <value xsi:type="xsd:string">ownerID</value>
    </tns:mapEntry>
    <tns:mapEntry id="id26" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">value</key>
      <value xsi:type="xsd:string">00530000000bxGz</value>
    </tns:mapEntry>
    <tns:mapEntry id="id27" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">operator</key>
      <value xsi:type="xsd:string">equals</value>
    </tns:mapEntry>
    <tns:mapEntry id="id28" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">field</key>
      <value xsi:type="xsd:string">title</value>
    </tns:mapEntry>
    <tns:mapEntry id="id29" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">value</key>
      <value xsi:type="xsd:string">VP</value>
    </tns:mapEntry>
    <tns:mapEntry id="id30" xsi:type="tns:mapEntry">
      <key xsi:type="xsd:string">operator</key>
      <value xsi:type="xsd:string">starts with</value>
    </tns:mapEntry>
  </soap:Body>
</soap:Envelope>

GaganGagan

Hi  everyone,

Im developing application in vb.net using API version 2.0.
Can u refer me some sample code for calling query call using multiple selection criterion.

 

Thanx
Gagan

DevAngelDevAngel

Hi Gagan,

If your solution is not yet in market, I would recommend using API 2.5.  It is much simpler to use in .Net than was API 2.0.   Can you tell me why you want to use 2.0 instead of 2.5?  I have the old samples, it's just that you will be much better off with the newer version.

GaganGagan

Hi Dave,
Thanx for the reply, but sorry i canot change the api version at this point. API 2.5 is still to trustworthy as few days bach it was giving login trouble.

Please refer me some codes where i can see how to set multiple selection criterion for the query call in vb.net. this matter is quite urgent so i would appriciate your quick response.

the second problem im facing is regarding requesting data from querycall for accounts entity. the returned is " bad file name" for "site" field. it seems its not a permission problem as im able to get this field in describ call. the same code works fine for the opportunities. the selectlist im passing for account entitiy is as follows:

--------------------------------
SelectList.Add("name")
SelectList.Add("site")
SelectList.Add("billingCity")
SelectList.Add("type")
SelectList.Add("id")
SelectList.Add("createdDate")

-------------------------------------

 

 

Please help

thanx and regards,
Gagan

DevAngelDevAngel

Hi Gagan,

Here is a query sample that demonstartes a simple query, and query with "and" and then a query with 2 "and"s and an "or".

You are making a mistake if you are developing a new solution using 2.0.  I would like to convince you to give 2.5 another look.  You would have finished your query stuff by now.

Cheers

Message Edited by DevAngel on 12-10-2003 08:07 AM

GaganGagan

Hi Dave,

Thanx for the guidence,

I am taking it seriously  and will try to convince my other team members to opt for API 2.5 . Please let me know what changes i have to make if i change my api for existing application. I understand that there will be bit of recoding.

Thanx and regards,
Gagan

DevAngelDevAngel

Hi Gagan,

The changes required generally involve the way you handle the xml returned from your calls.  If you are already using xpath queries to get the data, then your xpaths will change slightly.  If you are using describe, your call will be serialized into an object that allows you to easily get to the fields and values by using accessing properties and fields on the object.  Your queries will be greatly simplified (no more nested filter structures).  All update, insert (now called create) and delete methods are batch. 

I assume you will be using the partner wsdl.  Samples on this will be on the site in the next week or two.

You can ease into the new api by using both at the same time.  For instance, if you are having problems with the crazy query filters in 2.0, try the new query and swap out the old query.  There is not problem accessing both apis with a single session id.

Please take an hour or so to look at the API 2.5 samples and hopefully the changes and effort will become appearant.

Cheers

GaganGagan

Hi Dave,

Thanx for the guidence.. i have started using api 2.5. Please let me know what is the diffrence in using enterprise and partners API. My company (theikos) is a technical partner of salesforce and we are building an application for a one of the biggest CRM user of salesforce using sforce webservices. Please tell me which API 2.5 (Enterprise or Partner) version i am supposed to work on. Presently  im working on enterprise version.

Please update the API docs for 2.5 API as there are lots of things missing there.

 

While queston remains the same how to use multiple selection criterion even in api 2.5

Thanx and reagrds,
Gagan
Theikos

DevAngelDevAngel

Hi Gagan,

Theikos should use the partner wsdl as a general rule.  The partner version is generic in it's approach to the objects and schema.  You cannot depend on a particular schema from customer to customer and since the enterprise wsdl represents a particular uses's view of a schema, you will run into problems.

I am currently working on partner wsdl samples that should be available in the near future.

Documentation revs are not trivial and not to be overlooked either.  We will be reving the documentation when the dust settles a bit.

GaganGagan

hi Dave,
thanx for the repling...

my next problem is i am writing diffrent queries for opportunities which is giving diffrent error these are
1.
"Select name, type, stagename, probability, closeDate, amount from opportunity where Name like '%enterasys%' and Amount > 1000.0 and Probability < 90 and IsWon = false"

This returns " Error in XML Document"

2.
Select name, type, stagename, probability, closeDate, amount from opportunity where Name like '%enterasys%' and Amount > 1000.0 and Probability < 90 and CloseDate > #1/1/2003 6:30:00 AM#

this returns "common.exception.ApiException: malformed query"

Please guide me the solution

Is it a limit for a passing number of selection criterion  in  a querystring (SOQL).

Please write me  sample for SOQL string which is using multiple selection crieterion using diffrent data types like date boolean and double etc in a single query. My form is taking five(5) diffrent crterion from the user. Please help me in this scenerio.

Thanx and Regards,
Gagan
Theikos

 

GaganGagan

Dave,

Im stuckup in this....
I am using soap api 2.5 for partners. While firing a query call i am able to get records. But im not having a clue how to get records into a datatable object for query.records object. nither i am able to get it in xml for nor in the any of the dataobject that .net supports.

I pasteing the code below. may be all i need is a sample from ur partner query call (which u r currently building.) for the reference, ill make it myself further.

Regards,
Gagan
Theikos
----------------------------------

Public Function ReturnRecords(ByVal Querystring As String, ByVal URL As String, ByVal SID As String, ByVal APIVERSION As String, ByVal UserName As String, ByVal UserId As String, ByRef RecordTable As DataTable, ByRef errmsg As String) As Boolean

Dim qr As QueryResult = Nothing

Binding.QueryOptionsValue = New QueryOptions

Binding.QueryOptionsValue.batchSize = 25

Binding.QueryOptionsValue.batchSizeSpecified = True

SetHeaders(SID, URL, APIVERSION, errmsg)

Try

'Call the query method passing a SOQL statement that specifies the fields to return, the

'object on which the fields exist and, optionally, a where clause

qr = Binding.query(Querystring)

'check to see that there was a hit on the data.

If qr.size > 0 Then

?????????????? Here i am clueless Please Help

 

end if

Catch ex As Exception

errmsg = ex.ToString

End Try

 

Message Edited by Gagan on 12-15-2003 05:36 AM

DevAngelDevAngel

Hi Gagan,

For 1. the query works as you entered it, for me.  Try this on your developer account:

Select name, type, stagename, probability, closeDate, amount from opportunity where Name like '%emergency%' and Amount > 1000.0 and Probability < 90 and IsWon = false

The only thing I changed was the Name criteria in the where clause. 

For 2. the problem is in the date specification.  You have kind of an MS Access looking date delimitation going on here where instead you need to just pass the date without delimeters formatted as an iso8601 date string.  The modified query below should work.

Select name, type, stagename, probability, closeDate, amount from opportunity where Name like '%emergency%' and Amount > 1000.0 and Probability < 90 and CloseDate >= 2003-01-01T06:30:00Z.

I am noticing some inflexibility in the timezone offset portion of the date (Z).  After the seconds in the date you will need to supply a time zone offset.  Z means no offset, otherwise it would be +/- some hours like -8.00 for pst or +1.00 for uk daylight savings.  The final time string being 2003-01-01T06:30:00-8.00.

I am currently only seeing the Z offset working and will investigate further.

Cheers

DevAngelDevAngel

Hi Gagan,

The function below just iterates the result set saving the name and value from each field for each record saving them in a string variable.  The Any property is an array of XmlElements, one element for each field on the returned record.  The important properties on the XmlElement object are InnerText which contains the field value as a string and Name which contains the name of the field represented by that XmlElement.  The number of elements in the array should match the number of fields specified in the select list of the query.   Any XmlElement in the array can contain a nil value if the field is defined as nillable by the describe call.  I mention this because it's not a case that is being handled in the sample below.

From the aforementioned, in-process samples.

Cheers:

private void btnRunQuery_Click(object sender, System.EventArgs e)
{
 try
 {
  sforcep.QueryResult qr = binding.query(this.txtSOQL.Text);
  if (qr.size > 0)
  {
   string output = "";
   for (int i=0;i<qr.records.Length;i++)
   {
    output += "record " + Convert.ToString(i) + ":" + Environment.NewLine;
    sforcep.sObject record = qr.records[i];
    output += "   id: " + record.Id + Environment.NewLine;
    System.Xml.XmlElement[] fields = record.Any;
    for (int j=0;j<fields.Length;j++)
    {
     output += "   " + fields[j].LocalName + ": " + fields[j].InnerText + Environment.NewLine;
    }
    output += Environment.NewLine;
   }
   output += Environment.NewLine;
   this.txtResults.Text = output;
  }
  else
  {
   System.Windows.Forms.MessageBox.Show("No records matched query.", "Patner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
  }
 }
 catch (Exception ex)
 {
  System.Windows.Forms.MessageBox.Show("Query failed: " + ex.Message, "Partner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
 }
}