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);

}

2 comments:

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

    www.n8fan.net

    ReplyDelete
  2. Within this webpage, you'll see the page, you need to understand this data. donorperfect consultant

    ReplyDelete