如何使用JS在MS CRM中使用Xrm.WebApi.execute`方法执行“ Rollup”请求

时间:2019-03-18 14:09:34

标签: javascript dynamics-crm dynamics-crm-online

有一个很好的示例,说明如何通过MS CRM WebApi here调用Rollup函数。

但是,它涵盖了对CRM WebApi的常规访问。尽管在最新版本中,新的JS名称空间Xrm.WebApi was introduced。这提供了访问该端点的更直接的方法。

方法Xrm.WebApi.execute应该能够执行Rollup请求,因为它能够执行WhoAmI。但是我正在努力找出正确的参数值以使执行过程发生。

这是我的代码:

var RollupRequest = function(entityType, id, query) {
  this.Target = { entityType: entityType, id: id };
  this.RollupType = "Related";
  this.Query = {
    Query: query
  };
};

RollupRequest.prototype.getMetadata = function() {
  return {
    boundParameter: null,
    parameterTypes: {
      Target: {
        typeName: "Microsoft.Xrm.Sdk.EntityReference",
        structuralProperty: 5
      },
      RollupType: {
        typeName: "Microsoft.Dynamics.CRM.RollupType",
        structuralProperty: 3
      },
      Query: {
        typeName: "Microsoft.Xrm.Sdk.Query.FetchExpression",
        structuralProperty: 5
      }
    },
    operationType: 1, // This is a function. Use '0' for actions and '2' for CRUD
    operationName: "Rollup"
  };
};

var request = new RollupRequest(
  "contact",
  "0473FD41-C744-E911-A822-000D3A2AA2C5",
  "<fetch><entity name='activitypointer'></entity></fetch>"
);

Xrm.WebApi.execute(request).then(
  function(data) {
    console.log("Success: ", data);
  },
  function(error) {
    console.log("Failure: ", error);
  }
);

代码生成以下URL: /api/data/v9.0/Rollup(Target=@Target,RollupType=@RollupType,Query=@Query)?@Target={"@odata.id":"contacts(0473FD41-C744-E911-A822-000D3A2AA2C5)"}&@RollupType=&@Query={"Query":"<fetch><entity name='activitypointer'></entity></fetch>"}

和错误:"Expression expected at position 0 in ''."

这似乎表明RollupType的设置不正确,因为确实缺少URL RollupType。

我假设存在多个潜在错误,因为我使用FetchXML作为查询表达式。但是与此同时,是否有可能指出至少应该更改RollupType属性才能生成正确的URL的内容?

0 个答案:

没有答案