Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Trouble Constructing a Rollup QueryExpression With ConditionExpression - Calling Web API from Javascript

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hello all, I'm attempting to construct a QueryExpression in javascript with a ConditionExpression. The ConditionExpression object contains 3 properties- AttributeName, Operator, and Values. The Values property is an array of objects. I'm having trouble figuring out how the Values array objects must be constructed for it to be consumed by the API properly. Without a ConditionExpression the call I'm making is successful.

Conditions: [
          {
            AttributeName: "activitytypecode",
            Operator: ConditionalOperator.In.toString(),
            Values: [
                "4201"
            ]
          }
        ]
Passing in just an array of strings like above results in this error:
A node of type 'PrimitiveValue' was read from the JSON reader when trying to read the entries of a typed resource set;
however, a node of type 'StartObject' or 'EndArray', or a null value, was expected.

I know the Values objects must be defined somehow like this (this does not work):
[
  {
	"@odata.type": "Edm.String",
	"@odata.value": "4201"
  }
]

In C# creating a condition expression is trivial
new ConditionExpression("activitytypecode", ConditionOperator.In, new String[] {"4201"})

My next step is to setup everything up in C# and see how it serializes this into the URL. I'm trying to avoid that. Could anyone shed some light on this?

Any help is greatly appreciated, thank you.

*This post is locked for comments

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Trouble Constructing a Rollup QueryExpression With ConditionExpression - Calling Web API from Javascript

    Now it's easier to understand what you meant :) To use time in more efficient way the next time include your full scenario to initial question. That will cut number of additional questions.

    Backing to initial problem.

    If you want to use just Soap just forget my post about WebApi and check this - mileyja.blogspot.com/.../retrieve-all-entity-instances-related.html

    If you want to make it work with WebApi I'm afraid that you will have to use try/fault path. WebApi sometimes is SO not obvious...

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Trouble Constructing a Rollup QueryExpression With ConditionExpression - Calling Web API from Javascript

    No worries!

    Starting from the beginning- I am trying to roll up all activities related to an account similar to your example here butenko.pro/.../how-to-call-rollup-of-activities-using-webapi the only difference is I'm attempting to add filters/conditions to select particular activity types. I tried FetchXml but it is too limited for this case.

    It seems there is a bug in the Web API (in both v8.2 and v9.0) as there isn't any way to define the filter or condition without it erroring. For values it expects an array of objects- no primitive types (string, int, guid) are allowed. Passing in an array of empty objects results in a error "No parameterless constructor exists for Microsoft.Dynamics.CRM.Object". Passing in an array of objects with any parameters like { value: "4201" } results in a different error "The property 'value' does not exist on type 'Microsoft.Dynamics.CRM.Object'". Both errors make sense, but I do believe it is unintended behavior.

    I do think you're on the right track with using SOAP. I'm building out C# code on our server to make the rollup request using the SDK and IOrganizationService.Execute to actually perform the call.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Trouble Constructing a Rollup QueryExpression With ConditionExpression - Calling Web API from Javascript

    It seems that we have some kind of misunderstanding.

    What I posted is call to CRM's WebApi endpoint to get activities with task or phonecall activitytypecode. You can find more information here - msdn.microsoft.com/.../gg334767.aspx

    Backing to your code I still don't understand what you're trying to do. Can you please explain from the very beginning?

    One more thing - if you use QueryExression you will not be able to use WebApi. Only SOAP.

    Alternatively you can use FetchXml to query data using WebApi - msdn.microsoft.com/.../mt607533.aspx

    Good luck.

  • Kevin Grech Profile Picture
    Kevin Grech 380 on at
    RE: Trouble Constructing a Rollup QueryExpression With ConditionExpression - Calling Web API from Javascript

    Edit: Enclosed conditions in the curly brackets 

    Where are you passing that code? You might want to serialize that json using JSON.stringify. A JSON object is always enclosed in curly brackets  { }. So, if you were submitting via a POST message, it would be something like:

    var x =  {

       Conditions: {

            [

             {

               AttributeName: "activitytypecode",

               Operator: ConditionalOperator.In.toString(),

               Values: [

                   "4201"

               ]

             }

           ]

    }

    }

    JSON.stringify(x);

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Trouble Constructing a Rollup QueryExpression With ConditionExpression - Calling Web API from Javascript

    It does not. Here is query I built to show what I mean with "Equal" and "Or":

    /api/data/v8.2/activitypointers?$filter=activitytypecode eq 'task' or  activitytypecode eq 'phonecall'

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Trouble Constructing a Rollup QueryExpression With ConditionExpression - Calling Web API from Javascript

    Hmm Or filtering leads to the same issue. Regardless of the operator or if you're adding a condition or filter, it requires a Values array of Microsoft.Dynamics.CRM.Object

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Trouble Constructing a Rollup QueryExpression With ConditionExpression - Calling Web API from Javascript

    Hello,

    You will have to use Or filtering activitytypecode equals 4201 or activitytypecode equals 4202

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans