Bookmarklet: Clone Current Record

Sometimes, you just want to clone a current record, and slightly change the details. e.g. some sort of config entity record. You can use the bookmarklet below, that does cloning only at the parent entity level. Once the new form is open you can change the details and save it. Drag the minified code to your bookmarklet bar, and execute it when you have the CRM record open. The bookmarklet uses query parameters to populate the fields in the new form.

Unminified

(function() {
    var contentPanels = Array.from(document.querySelectorAll('iframe')).filter(function (d) {
        return d.style.visibility !== 'hidden'
      });

    if (!contentPanels || contentPanels.length == 0 || !contentPanels[0].contentWindow.Xrm) {
		alert('CRM Form not detected');
		return;
    }
	var Xrm = contentPanels[0].contentWindow.Xrm;
	var extraq = '';
	var clientUrl = Xrm.Page.context.getClientUrl();
	var entityName = Xrm.Page.data.entity.getEntityName();

	Xrm.Page.data.entity.attributes.forEach(function(c){
		var attributeType = c.getAttributeType();
		var attributeName = c.getName();
		var attributeValue = c.getValue();
		if(!attributeValue ||
		attributeName === 'createdon' ||
		attributeName === 'modifiedon' ||
		attributeName === 'createdby' ||
		attributeName === 'modifiedby') return;

		if(attributeType === 'lookup'){
			extraq += (attributeName+'name='+attributeValue[0].name+'&');
			extraq += (attributeName+'type='+attributeValue[0].entityType+'&');
			attributeValue = attributeValue[0].id;
		}
		if(attributeType === 'datetime'){
			attributeValue = attributeValue.toDateString();
		}
		extraq += (attributeName+'='+attributeValue+'&');
	});

	var newWindowUrl = clientUrl+'/main.aspx?etn='+entityName+'&pagetype=entityrecord'+'&extraqs=?'+encodeURIComponent(extraq)+'etn='+entityName;

	window.open(newWindowUrl, '_blank', "location=no,menubar=no,status=no,toolbar=no", false);
})();

Minified

javascript:(function(){var contentPanels=Array.from(document.querySelectorAll('iframe')).filter(function(d){return d.style.visibility!=='hidden'});if(!contentPanels||contentPanels.length==0||!contentPanels[0].contentWindow.Xrm){alert('CRM Form not detected');return;}
var Xrm=contentPanels[0].contentWindow.Xrm;var extraq='';var clientUrl=Xrm.Page.context.getClientUrl();var entityName=Xrm.Page.data.entity.getEntityName();Xrm.Page.data.entity.attributes.forEach(function(c){var attributeType=c.getAttributeType();var attributeName=c.getName();var attributeValue=c.getValue();if(!attributeValue||attributeName==='createdon'||attributeName==='modifiedon'||attributeName==='createdby'||attributeName==='modifiedby')return;if(attributeType==='lookup'){extraq+=(attributeName+'name='+attributeValue[0].name+'&');extraq+=(attributeName+'type='+attributeValue[0].entityType+'&');attributeValue=attributeValue[0].id;}
if(attributeType==='datetime'){attributeValue=attributeValue.toDateString();}
extraq+=(attributeName+'='+attributeValue+'&');});var newWindowUrl=clientUrl+'/main.aspx?etn='+entityName+'&pagetype=entityrecord'+'&extraqs=?'+encodeURIComponent(extraq)+'etn='+entityName;window.open(newWindowUrl,'_blank',"location=no,menubar=no,status=no,toolbar=no",false);})(); void 0;

I tested this only in CRMOnline on version 8.1.0.359.

End note:

This bookmarklet does not copy across the time component, if you have a datetime field with both date and time on the parent form.

Reference:

https://msdn.microsoft.com/en-us/library/gg334375.aspx#BKMK_ExampleXrmUtilityOpentEntityForm

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s