basically all you need to do is create a simple toolbar button that opens a fresh entity page and traverse the controls DataValue.
The first requirement I stumbled on was to identify the cloning process. The problem was that In v4.0 ,by default, ms disallows the passing of unknown query string parameters. although you can change this behavior by modifying a registry settings, this would be extremely unsupported and unwise if ms would choose to delete this option on it’s next CRM version. Instead I used the window.open name parameter.
var EntWindowName = 'Cloned' + crmForm.ObjectTypeName; // e.g. ‘Clonedaccount’
window.open( EntWindowUrl , EntWindowName , EntWindowFeatures );
An alternative solution would be to add a new bit attribute to the entity form e.g. new_iscloningproc and pass a value indicating that this is a cloning process.
For example: “/userdefined/edit.aspx?new_iscloningproc=1 “(true);
MS supports this new type of parameter referencing in v4.0, take a look at the following URL for more information: URL Addressable Forms and Views
The second thing I stumbled on was getting the entity layout and URL information. I could have wrote a simple mechanism for that, however, ms already exposes a GetWindowInformation function so I decided to use that instead.
var EntUrlInfo = GetWindowInformation(ObjectTypeCode); // e.g. 1 - account
var EntWindowFeatures = 'toolbars=0,width=' + EntUrlInfo.Width + ',Height=' + EntUrlInfo.Height + ',Left=10,top=90';
The last issue I came across was to correctly set the URL format for vanilla and custom entities. Vanilla entities use a well defined folder structure e.g. "/sfa/accts/edit.aspx" ( account form ) whereas
custom entities use a logical URL e.g. “/userdefined/edit.aspx?etc=1”. ms uses the etc query string parameter to differentiate between the requested entities. In order to solve that I added a simple check on the crmForm.ObjectTypeCode.
var EtcQsParameter = (ObjectTypeCode > 9999)? '?etc=' + ObjectTypeCode : '';
var EntWindowUrl = '/' + ORG_UNIQUE_NAME + '/' + EntUrlInfo.Url + EtcQsParameter;
Here is the entire isv.config toolbar button.
<Button Icon="/_imgs/ico_18_quota.gif"
JavaScript="
function CloneEntity()
{
var ObjectTypeCode = crmForm.ObjectTypeCode;
var EntUrlInfo = GetWindowInformation(ObjectTypeCode);
var EntWindowFeatures = 'toolbars=0,width=' + EntUrlInfo.Width + ',Height=' + EntUrlInfo.Height + ',Left=10,top=90';
var EtcQsParameter = (ObjectTypeCode > 9999)? '?etc=' + ObjectTypeCode : '';
var EntWindowUrl = '/' + ORG_UNIQUE_NAME + '/' + EntUrlInfo.Url + EtcQsParameter;
var EntWindowName = 'Cloned' + crmForm.ObjectTypeName;
window.open( EntWindowUrl , EntWindowName , EntWindowFeatures );
}
CloneEntity();
" Client="Web">
<Titles>
<Title LCID="1033" Text="Clone X" />
</Titles>
<ToolTips>
<ToolTip LCID="1033" Text="Clone X" />
</ToolTips>
</Button>
<ToolBarSpacer />
Paste the code inside the entity onload event and your done.
function OnCrmPageLoad()
{
/* Indentify that this is a cloning process. */
if( window.name == 'Cloned' + crmForm.ObjectTypeName && crmForm.FormType == 1 )
{
GetOriginalEntityFields();
}
}
function GetOriginalEntityFields()
{
if( !opener )
{
alert('Missing opener');
return;
}
var re = new RegExp("INPUT|TEXTAREA|SELECT","gi");
var originalEntityFormElements = opener.document.all.crmForm.all;
var currentEntityFormElements = crmForm.all;
for( var i = 0 ; i < originalEntityFormElements.length ; i++ )
{
var OriginalElement = originalEntityFormElements[ i ];
var OriginalElementId = OriginalElement.id;
if( OriginalElementId != '' )
{
var currentElement = currentEntityFormElements[OriginalElementId];
if( typeof(currentElement.req) != "undefined" )
{
currentElement.DataValue = OriginalElement.DataValue;
currentElement.Disabled = OriginalElement.Disabled;
}
else if( re.test(currentElement.tagName) )
{
currentElement.value = OriginalElement.value;
}
}
} //end for
} //end function
OnCrmPageLoad();