Tuesday, June 22, 2010

Clone Record in CRM using CrmServiceToolkit (JavaScript)

A few weeks ago, one of our customers requested record cloning features in some of our customized entities. I tried to look for this feature through internet and found out a lot of interesting articles stating different ways of cloning records.

Below is the list of articles I found out for your reference:
  1. Clone Records Without Code
  2. Clone Records Using Workflow
  3. Cloning Records In Microsoft CRM 4.0 Using AJAX Controls
Among them, I decided to use ASP.NET way. I download the source code, modified it a bit and used in our project. After testing for a few days, I noticed about five or six seconds delay to load an asp.net page when I click "Clone Record" button for the first time.

I discussed about this issue with one my colleagues and he suggested me to use CRM Web Service Toolkit (JS) framework to clone the record. It is not very easy to use, but it helps us a lot to solve our performance issues and reduce steps in deployment. We are using this toolkit to retrieve data from CRM directly instead of writing our own asmx web services applications. Of course, you need to reference CrmServiceToolkit in your JavaScript deployment. Thanks to Daniel Cai for his research, great effort and generous contribution to our CRM community.

Below is my JavaScript source code:

 
onCloneRecordClick = function()
{
    try
    {
        if(confirm('Do you want to create a duplicate record?'))
        {
            // Create Clone Record and Retrieve Primary Key
            var _id = makeClone(crmForm.ObjectTypeName, crmForm.ObjectId);
           
            // Open Newly Created Record in CRM

            if(confirm('Duplicate record created successfully.\nDo you want to open it?'))
            {
                var _url = getFormURL(crmForm.ObjectTypeName, _id);
                window.open(_url, "_self");
            }
        }
    }
    catch(ex)
    {
        showError(ex.description);
    }
} 

// Create Clone Record
makeClone = function(entityName, objId)
{
    var attribName = crmForm.ObjectTypeName + "id";
    var fetchXML =     "" +
                    "" +
                    "";        
    var oSource = CrmServiceToolkit.Fetch(fetchXML);        
    
    // Create Clone Record
    var oClone = new CrmServiceToolkit.BusinessEntity(entityName);        
    for ( var p in oSource[0].attributes)
        if(p != attribName)
            oClone.attributes[p] = oSource[0].attributes[p].value;
        
    var id = CrmServiceToolkit.Create(oClone);
    return id;
}

// Retrieve URL to Open CRM Form
getFormURL = function(entityName, objId)
{
    var path = "../../../userdefined/edit.aspx?id=" + objId + "&etn=" + entityName;
    return path;
}

// Show Error Message 
showError = function(message)
{
    attachOnChangeAllControls();

    var notificationsArea = document.getElementById('Notifications');
    
    if (notificationsArea == null)
    {
        alert(message);
         return;
    }
    notificationsArea.title = '0';
    var notificationHTML = '









' + message + '
';     notificationsArea.innerHTML += notificationHTML;     notificationsArea.style.display = 'block'; } 

Explanation:
(added on 2nd Sept 2010)

How to use CrmServiceToolkit in CRM?
1. Download CRM Service Toolkit from codeplex
2. Extract zip file and place CrmServiceToolkit.min.js and qunit.js files under ISV folder
3. Use loadscript() function to load CrmServiceToolkit file to CRM at Form Load Event (Please refer to henrycordes' blog) This is not recommended by Microsoft.
4. Use above CloneRecord code in your CRM Form.


Reference:

3 comments:

Anonymous said...

Hello! nice job, How register all of this? directly on ISV.CONFIG on Button Javascript="" ?

Thuta Hlaing said...

Yes, you need to write loadscript function in ISV to call external js file.

If you don't want to touch ISV, you can also use alternative method to create the button. Please refer to:

http://mahenderpal.wordpress.com/2010/08/05/create-cutom-button-in-crm-without-using-isvconfig/

Surely, it is not supported by Microsoft :P

Regards
Thuta

Withers said...

This code has a ton of syntax error or i'm missing something.
also does the javascript go in the button code or onform load?

Post a Comment