3

I wrote a utility that cycles through a bunch of orgs and pulls out information. I'm using the Partner SOAP API and C# to do this. Now, I'm trying to get some additional information that is only available with the Tooling API. I've figured out how to make the Tooling API connection and when I run this query, it works:

            string SOQL = "SELECT SharingModel FROM CustomObject WHERE DeveloperName = 'Booking'";
            ToolingService.QueryResult qr = new ToolingService.QueryResult();
            qr = ts.query(SOQL);

However, if I change the SOQL string to a Select Count, it fails on the ts.query(SOQL) line. An example aggregate query that fails is:

           string SOQL = "Select count(Id) From ValidationRule where EntityDefinition.DeveloperName = 'Booking'";

The error is: Exception occured There is an error in XML document (1, 433).

The Aggregate query works fine in the developer console.

I'm sure this is some sort of a casting issue but I just can't find it. I'm guessing that the aggregate result can't come back into the QueryResult sObject. I've been reading through the Tooling API Reference.cs file trying to figure it out but I haven't had any luck. Any ideas what I need to change so an aggregate query will work?

7
  • Can you try Select count() From ValidationRule where EntityDefinition.DeveloperName = 'Booking' Commented Feb 17, 2019 at 19:44
  • At this point, I'll try just about anything :) I tried your suggestion. It allowed it to not error out on the ts.query(SOQL) line but caused ANYTHING having to do with qr (the queryresult) to throw the exception "Exception occured Object reference not set to an instance of an object.". I couldn't even check the recordcount (qr.records.Length) without getting that error. It's like it set qr = null or something.
    – Mike Jones
    Commented Feb 17, 2019 at 20:45
  • Can you try qr.result.size? , or just try printing qr to see what it has? Commented Feb 17, 2019 at 20:49
  • Well, qr.result isn't an option but Console.WriteLine(qr.totalSize.ToString()); returned 16 which is the correct answer to my count! I tried a different object to make sure and got the correct answer there as well. This obviously won't work if I select other fields as well but at least I can get a count now. Thanks! If you want to put that as an answer instead of a comment, I'll mark it as the answer. (Or, I'm new here, so if there is a way to mark your comment as the answer, please let me know.)
    – Mike Jones
    Commented Feb 17, 2019 at 21:01
  • I will put answer with my findings and description. Commented Feb 17, 2019 at 21:14

1 Answer 1

2

The Response structure of SELECT Count() and SELECT Count(Id) is bit different and that why it causes the casting to fail.

Response of SELECT COUNT(Id) in tooling api Query.

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:tooling.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sf="urn:sobject.tooling.soap.sforce.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soapenv:Body>
        <queryResponse>
            <result>
                <done>true</done>
                <entityTypeName>AggregateResult</entityTypeName>
                <queryLocator xsi:nil="true"/>
                <records xsi:type="sf:AggregateResult">
                    <sf:Id xsi:nil="true"/>
                    <sf:expr0 xsi:type="xsd:int">0</sf:expr0>
                </records>
                <size>1</size>
                <totalSize>1</totalSize>
            </result>
        </queryResponse>
    </soapenv:Body>
</soapenv:Envelope>

Response of SELECT COUNT() in tooling api Query.

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:tooling.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <queryResponse>
            <result>
                <done>true</done>
                <entityTypeName xsi:nil="true"/>
                <queryLocator xsi:nil="true"/>
                <size>0</size>
                <totalSize>0</totalSize>
            </result>
        </queryResponse>
    </soapenv:Body>
</soapenv:Envelope>

As there is a contract in Soap API and SELECT Count(Id) does not follow the contract, it fails the type casting.

Solution: Use SELECT Count() and get result in queryResult.totalSize

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .