Clone the record from Javascript

Our client want a new function to clone the record from existing one. For example, we have one entity called new_student entity.

The following are the steps how to catch this function in MSCRM 2011.
1) Add the clone button on the Form's Ribbon.
2) Create new attribute "Clone From" in the new_student entity.
3) Call the javascript function "cloneRecord()" when user click the "Clone" Ribbon button.
4) This "cloneRecord()"  will redriect to URL that go to create new record form with existing record id.
5) On the form load, do fetch xml using with passed record id and fill the value on the new form.

1)Add the clone button on the Form's Ribbon.

2) Create new attribute "Clone From" in the new_student entity.



And put it into the new_student form and hide this attribute.

3) Call the javascript function "cloneRecord()" when user click the "Clone" Ribbon button.
4) This "cloneRecord()" will redriect to URL that go to create new record form with existing record id.

function CloneRecord() {

    var serverUrl;

    var errorMessage = "Context to retrieve the Server URL is not available.";
    if (typeof GetGlobalContext != "undefined") {
        serverUrl = GetGlobalContext().getServerUrl();
    }
    else {
        if (typeof Xrm != "undefined") {
            serverUrl = Xrm.Page.context.getServerUrl();
        }
        else {
            alert(errorMessage);
            return;
        }
    }
    if (serverUrl.match(/\/$/))
    {
        serverUrl = serverUrl.substring(0, serverUrl.length - 1);
    }

    var recordUrl = serverUrl + "/main.aspx?";

    var params = "etn=new_student";

    params += "&pagetype=entityrecord&extraqs=";
    params +=  encodeURIComponent("&new_clonefrom='"+Xrm.Page.data.entity.getId()+"'");
    var newStudentURL = recordUrl + params;
    window.open(newStudentURL , "_blank", "width=900px,height=600px,resizable=1");
}



5) On the form load, do fetch xml using with passed record id and fill the value of old record on the new form.


var _oService;
var _sOrgName = "new";
var _sServerUrl = Xrm.Page.context.getServerUrl();


function FormOnLoad()
{
 var FORM_TYPE_CREATE = 1;
 var FORM_TYPE_UPDATE = 2;
 var FORM_TYPE_READ_ONLY = 3;
 var FORM_TYPE_DISABLED = 4;
 var FORM_TYPE_QUICK_CREATE = 5;
 var FORM_TYPE_BULK_EDIT = 6;
 var formType = Xrm.Page.ui.getFormType();
 if (formType == FORM_TYPE_CREATE) {
  var CloneRecordId=Xrm.Page.getAttribute("new_clonefrom").getValue() ;
  CloneRequest(CloneRecordId);
 }
}
function CloneRequest(CloneStudentId)
{

 var sFetch = "<fetch mapping='logical' count='10'>" +
       "<entity name='new_student'>"+
      "<attribute name='new_studentid'/>"+
      "<attribute name='new_name' />"+
     "<order attribute='new_name' descending='false' />"+
     "<filter type='and'><condition attribute='new_studentid' value=" + CloneStudentId+ " uitype='new_student' operator='eq'/></filter>" +
      "</entity>"+
    "</fetch>";
 _oService = new FetchUtil(_sOrgName, _sServerUrl);
 _oService.Fetch(sFetch, CloneRequestComplete);

}
function CloneRequestComplete(results)
{
  if (results[0].attributes["new_name"] != null) Xrm.Page.getAttribute("new_name").setValue(results[0].attributes["new_name"].value);

}

4 comments:

  1. I look forward in reading more of your work.

    www.n8fan.net

    ReplyDelete
  2. You make it entertaining and you still take care of to keep it wise. I am really satisfied with the posting that you have given us; I can not wait to read far more from you.

    www.triciajoy.com

    ReplyDelete
  3. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post.

    Bry
    www.gofastek.com

    ReplyDelete
  4. Love it! Very interesting topics, I hope the incoming comments and suggestion are equally positive. Thank you for sharing this information that is actually helpful.


    ufgop.org
    ufgop.org

    ReplyDelete