CRM 2011 Record Duplication Ribbon


*Note used by passing parameter through url. Trigger point will be custom ribbon button.

Before:
Make sure created one ribbon button name as duplicate to call this javascript function.

Javascript Function:


function DuplicateAsset() {

    var AssetId = Xrm.Page.data.entity.getId();

    var AssetName = Xrm.Page.data.entity.attributes.get("vw_name").getValue(); //Single Text
    var Administrator = Xrm.Page.data.entity.attributes.get("vw_administrator").getValue(); //Dropdown
    if (Xrm.Page.data.entity.attributes.get("vw_custodian").getValue() != null) { //Lookup 
    var CustodianId = Xrm.Page.data.entity.attributes.get("vw_custodian").getValue()[0].id;
    var CustodianName = Xrm.Page.data.entity.attributes.get("vw_custodian").getValue()[0].name;
    var CustodianType = Xrm.Page.data.entity.attributes.get("vw_custodian").getValue()[0].entityType;
    }
    var InvoiceDate = FormatDate("vw_invoicedate"); //Date

    //define default values for new Incident record
    var parameters = {};
   
if (AssetName != null) { parameters["vw_name"] = AssetName + " - COPY"; }
    if (Administrator != null) { parameters["vw_administrator"] = Administrator; }
    if (CustodianId != null && CustodianName != null) { parameters["vw_custodian"] = CustodianId; parameters["vw_custodianname"] = CustodianName; }
    if (InvoiceDate != null) { parameters["vw_invoicedate"] = InvoiceDate; }

    //pop form with default values
    Xrm.Utility.openEntityForm("Entity Name", null, parameters);
}


// This function takes the fieldname of a date field as input and returns the value of that field in MM/DD/YYYY format
// Note: the day, month and year variables are numbers
function FormatDate(fieldname) {
    var d = Xrm.Page.data.entity.attributes.get(fieldname).getValue();
    if (d != null) {
        var curr_date = d.getDate();
        var curr_month = d.getMonth();
        curr_month++;  // getMonth() considers Jan month 0, need to add 1
        var curr_year = d.getFullYear();
        return curr_month + "/" + curr_date + "/" + curr_year;
    }
    else return null;
}


This code work when u key in the field with the value or without value.
One duplicate record will pop out for u to save and edit.

Limitation: 
Url character had their limit of character. If u hit the amount of character then this code wont work anymore.

0 comments: