如何在CRM 2011中获取事件状态后启用电子邮件中的功能区按钮?

时间:2014-03-05 20:22:58

标签: javascript jquery ajax rest crm

在CRM中,我需要启用/禁用电子邮件表单实体中的按钮。如果父实体(事件/案例)状态处于活动状态,则启用该按钮,否则将禁用该按钮。

我目前的设置:

  1. 按钮位于“电子邮件实体表单”视图功能区
  2. Button有一个命令调用一些JS函数(这可行)。
  3. 该命令具有调用EnableEmailButton
  4. 的启用规则
  5. 启用规则要求函数返回true(启用)或false(禁用)
  6. JS:

    var isCaseOpen = false;
    function EnableEmailButton()
    {
        var regardingObject = crmForm.all.regardingobjectid.DataValue;
        if(regardingObject)
        {
            var regardingObjectId = regardingObject[0].id;
            regardingObjectId = regardingObjectId.replace('{','').replace('}','');
    
            GetEntityById(regardingObjectId, "Incident", OnGotIncident);
        }
    
        return isCaseOpen;
    }
    
    function OnGotIncident(incident) {
        if(incident.StateCode.Value == 0)
        {
            isCaseOpen = true;
        }
    }
    
    function GetEntityById(entityId, entityName, CallbackFunction) {
        SDK.JQuery.retrieveRecord
        (
            entityId,
            entityName,
            null,
            null,
            function OnSuccess(entity) {
                CallbackFunction(entity);
            },
            function errorHandler(error) {
                alert(error.message);
            }
        );
    }
    

    由于retrieveRecord是异步调用,我无法保证何时会收到回复。但是在函数返回isCaseOpen的值之前我需要一个响应。

    我该怎么做?

    基本上我需要创建一个延迟,直到我得到一个AJAX响应。

1 个答案:

答案 0 :(得分:0)

我通过编辑Microsoft提供的文件中的SDK.JQuery.retrieveRecord函数来修复此问题。我在函数中添加了一个额外的参数,让我决定调用是否异步。

JS:

retrieveRecord: function (id, type, select, expand, isAsync, successCallback, errorCallback) {
  ///<summary>
  /// Sends an asynchronous request to retrieve a record.
  ///</summary>
  ///<param name="id" type="String">
  /// A String representing the GUID value for the record to retrieve.
  ///</param>
  this._stringParameterCheck(id, "SDK.JQuery.retrieveRecord requires the id parameter is a string.");
  ///<param name="type" type="String">
  /// The Schema Name of the Entity type record to retrieve.
  /// For an Account record, use "Account"
  ///</param>
  this._stringParameterCheck(type, "SDK.JQuery.retrieveRecord requires the type parameter is a string.");
  ///<param name="select" type="String">
  /// A String representing the $select OData System Query Option to control which
  /// attributes will be returned. This is a comma separated list of Attribute names that are valid for retrieve.
  /// If null all properties for the record will be returned
  ///</param>
  if (select != null)
   this._stringParameterCheck(select, "SDK.JQuery.retrieveRecord requires the select parameter is a string.");
  ///<param name="expand" type="String">
  /// A String representing the $expand OData System Query Option value to control which
  /// related records are also returned. This is a comma separated list of of up to 6 entity relationship names
  /// If null no expanded related records will be returned.
  ///</param>
  if (expand != null)
   this._stringParameterCheck(expand, "SDK.JQuery.retrieveRecord requires the expand parameter is a string.");
  ///<param name="isAsync" type="Boolean">
  /// Synchronous or Asynchronous request.
  /// 
  ///</param>
  this._booleanParameterCheck(isAsync, "SDK.JQuery.retrieveRecord requires the isAsync parameter is a boolean.");
  ///<param name="successCallback" type="Function">
  /// The function that will be passed through and be called by a successful response. 
  /// This function must accept the returned record as a parameter.
  /// </param>
  this._callbackParameterCheck(successCallback, "SDK.JQuery.retrieveRecord requires the successCallback parameter is a function.");
  ///<param name="errorCallback" type="Function">
  /// The function that will be passed through and be called by a failed response. 
  /// This function must accept an Error object as a parameter.
  /// </param>
  this._callbackParameterCheck(errorCallback, "SDK.JQuery.retrieveRecord requires the errorCallback parameter is a function.");

  var systemQueryOptions = "";

  if (select != null || expand != null) {
   systemQueryOptions = "?";
   if (select != null) {
    var selectString = "$select=" + select;
    if (expand != null) {
     selectString = selectString + "," + expand;
    }
    systemQueryOptions = systemQueryOptions + selectString;
   }
   if (expand != null) {
    systemQueryOptions = systemQueryOptions + "&$expand=" + expand;
   }
  }

  $.ajax({
   type: "GET",
   contentType: "application/json; charset=utf-8",
   datatype: "json",
   async: isAsync,
   url: this._ODataPath() + type + "Set" + "(guid'" + id + "')" + systemQueryOptions,
   beforeSend: function (xhr) {
    //Specifying this header ensures that the results will be returned as JSON.             
    xhr.setRequestHeader("Accept", "application/json");
   },
   success: function (data, textStatus, xhr) {
    //JQuery does not provide an opportunity to specify a date reviver so this code
   // parses the xhr.responseText rather than use the data parameter passed by JQuery.
    successCallback(JSON.parse(xhr.responseText, SDK.JQuery._dateReviver).d);
   },
   error: function (xhr, textStatus, errorThrown) {
    errorCallback(SDK.JQuery._errorHandler(xhr));
   }
  });
 },