简体   繁体   中英

How to enable ribbon button in email after getting incident state in CRM 2011?

In CRM, I need to enable/disable a button in the Email form entity. The button is enabled if the parent entitys(Incident/Case) status is active otherwise its disabled.

My current setup:

  1. Button is in Email Entity Form view ribbon
  2. Button has a command calling some JS function (this works).
  3. The command has an enable rule which calls EnableEmailButton
  4. The enable rule expects the function to return true (enable) or false (disable)

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);
        }
    );
}

Since retrieveRecord is an async call, I cannot guarantee when I will get a response. But I need a response before the function returns the value of isCaseOpen .

How do I do this?

Basically I need to create a delay until I get an AJAX response.

I fixed this by editing the SDK.JQuery.retrieveRecord function in the file Microsoft provides. I added an extra parameter to the function which lets the me decide if the call is async or not.

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));
   }
  });
 },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM