Skip to content

Filtering over StatusCode with In/NotIn operator #315

@mivra

Description

@mivra

The XrmQuery filtering does not work as expected with statuscode columns (and possibly other number based columns) and In/NotIn operator.
The issue is with value WebApi parameter encapsulation - it requires the values to be inside the ' quotes, otherwise error like this is thrown - Cannot convert the literal '281600002' to the expected type 'Edm.String'.

Bad query segment
Microsoft.Dynamics.CRM.NotIn(PropertyName='statuscode',PropertyValues=[281600002,281600005,281600006]))

Proper query segment:
Microsoft.Dynamics.CRM.NotIn(PropertyName='statuscode',PropertyValues=['281600002','281600005','281600006']))

Now I have a code like this

const notActiveStatuses = [
    ken_partnerlead_statuscode.Spam,
    ken_partnerlead_statuscode.Lost,
    ken_partnerlead_statuscode.Onhold,
  ];

const result = await XrmQuery.retrieveMultiple((query) => query.ken_partnerleads)
  .select((ken_partnerlead) => [ken_partnerlead.ken_partnerleadid, ken_partnerlead.statuscode])
  .filter((filter) => Filter.notIn(filter.statuscode, notActiveStatuses))
  .promise();

Filter typing forces me into the array of ken_partnerlead_statuscode which is type-generated enum type. So I'm unable to pass it just as array of strings, which would actually work. If I try to use the columnname directly (Filter.notIn('statuscode', notActiveStatuses)), then the columnname is encapsulated twice.

I managed to overcome the issue in a rather hacky way — by using improper typing.

  const notActiveStatuses = [
    ken_partnerlead_statuscode.Spam,
    ken_partnerlead_statuscode.Lost,
    ken_partnerlead_statuscode.Onhold,
  ].map((status) => status.toString()) as unknown as ken_partnerlead_statuscode[];

Environment

  • Version 1710 (9.2.25113.156) online

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions