Open a Dynamics CRM 2013 Quick Create Form using JavaScript

If you want to create a new record in Dynamics CRM 2013 using a Quick Create form from a ribbon (command bar) button, there is no supported way of doing so, but it is possible using unsupported JavaScript.

Create a web resource containing the following code:

if (typeof(CustomQuickCreate) == typeof(undefined)) {
    CustomQuickCreate = {}
}

// CustomQuickCreate.Callbacks constructor
CustomQuickCreate.Callbacks = function(saveCallback) {
    CustomQuickCreate.Callbacks.initializeBase(this, ["CustomQuickCreate", 1000]);
    this.saveCallback = saveCallback
};

// CustomQuickCreate.Callbacks prototype
CustomQuickCreate.Callbacks.prototype = {
    saveCallback: null,
    onQuickCreateSaveSuccessCallback: function(record) {
        var thisObj = this;
        this.hideQuickCreateWithAnimation(function() {
            !IsNull(thisObj.saveCallback) && thisObj.saveCallback(record);
            thisObj.removeEntireGlobalQuickCreateFromDom()
        });
        return true;
    }
};

// Method to show Custom Quick Create Form
CustomQuickCreate.ShowQuickCreateForm = function(entityType, formTitle, saveCallback) {

    // Get the entity type code of the current record
    var thisType = Xrm.Page.context.getQueryStringParameters().etc;

    // Get the ID of the current record
    var thisId = Xrm.Page.data.entity.getId();

    // Get the entity type code of the record being created
    var lookupService = new RemoteCommand("LookupService", "RetrieveTypeCode");
    lookupService.SetParameter("entityName", entityType);
    var createType = lookupService.Execute().ReturnValue;

    // Show form (uses unsupported CRM system call)
    Mscrm.GlobalQuickCreate.GlobalQuickCreateBehavior.launchGlobalQuickCreate(new CustomQuickCreate.Callbacks(saveCallback), formTitle, createType, null, thisId, thisType);
}

// CustomQuickCreate.Callbacks inherits from Mscrm.GlobalQuickCreate.GlobalQuickCreateCallbacks in CRM's GlobalQuickCreateBehavior.js file
CustomQuickCreate.Callbacks.registerClass("CustomQuickCreate.Callbacks", Mscrm.GlobalQuickCreate.GlobalQuickCreateCallbacks);

Here’s an example of how to open a quick create form using this code:

CustomQuickCreate.ShowQuickCreateForm("incident", "Create a new Case", function() { alert('Callback..!'); });

Use the callback to do whatever you want once the record is created, for example navigate to the new record, or show an alert confirming success.

Leave a Reply

Your email address will not be published. Required fields are marked *