Xrm.WebApi.online.execute帮助创建通知

时间:2019-02-01 06:06:17

标签: javascript dynamics-crm xrm

我正在尝试对Xrmapi调用使用execute。下面是我的代码。它执行不正常,并且我无法到达错过的地方。 TIA

var opportunityClose={
"type":"GET",
"contentType":"application/json; charset=utf-8",
"datatype":"json",
"url":url
};
Xrm.WebApi.online.execute(new Sdk.WinOpportunityRequest(opportunityClose,4)).then(function (data, textStatus, XmlHttpRequest) {
        console.log("success getting notification activities");
        debugger;
        if (data.d.results.length == 0) // no notifications, nothing to do
            return;
        var notifications = [];

        $.each(data.d.results, function (i, notification) {

            var icon = (notification.guru_Icon ? notification.guru_Icon.Value : 770170000);
            notification.Level = Guru.Xrm.Notifications.mapIconToNotificationLevel(icon);
            notifications.push(notification);
        });
        Guru.Xrm.Notifications.AddFormNotifications(notifications);
    },
    function (XmlHttpRequest, textStatus, errorObject) {
        console.log(XmlHttpRequest);
        console.log(errorObject);
        console.log("failed to load notification activities");
    });

1 个答案:

答案 0 :(得分:0)

您的代码似乎缺少一些关键信息,例如关闭机会的ID。

This document包含一个如何通过Xrm.WebApi.online关闭Opp的示例。

这是示例代码:

var Sdk = window.Sdk || {};
/**
 * Request to win an opportunity
 * @param {Object} opportunityClose - The opportunity close activity associated with this state change.
 * @param {number} status - Status of the opportunity.
 */
Sdk.WinOpportunityRequest = function (opportunityClose, status) {
    this.OpportunityClose = opportunityClose;
    this.Status = status;

    this.getMetadata = function () {
        return {
            boundParameter: null,
            parameterTypes: {
                "OpportunityClose": {
                    "typeName": "mscrm.opportunityclose",
                    "structuralProperty": 5 // Entity Type
                },
                "Status": {
                    "typeName": "Edm.Int32",
                    "structuralProperty": 1 // Primitive Type
                }
            },
            operationType: 0, // This is an action. Use '1' for functions and '2' for CRUD
            operationName: "WinOpportunity",
        };
    };
};


var opportunityClose = {
    "opportunityid@odata.bind": "/opportunities(c60e0283-5bf2-e311-945f-6c3be5a8dd64)",
    "description": "Product and maintainance for 2018",
    "subject": "Contract for 2018"
}

// Construct a request object from the metadata
var winOpportunityRequest = new Sdk.WinOpportunityRequest(opportunityClose, 3);

// Use the request object to execute the function
Xrm.WebApi.online.execute(winOpportunityRequest).then(
    function (result) {
        if (result.ok) {
            console.log("Status: %s %s", result.status, result.statusText);
            // perform other operations as required;
        }
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

在相关说明中,此documentation描述了如何通过WebAPI使用WinOpportunity操作。

这是节选:

POST [Organization URI]/api/data/v9.0/WinOpportunity HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0

{
 "Status": 3,
 "OpportunityClose": {
  "subject": "Won Opportunity",
  "opportunityid@odata.bind": "[Organization URI]/api/data/v9.0/opportunities(b3828ac8-917a-e511-80d2-00155d2a68d2)"
 }
}