CRM 2011 Duplicate Function to Duplicate Record


Problems: How easily can i create a record if the record is nearly same as another record? Whereby i no need to key in everything again,

Solution:
This function is called by using one custom button.


function Duplicatet() {

    var Id = Xrm.Page.data.entity.getId();
    var Name = Xrm.Page.data.entity.attributes.get("vw_name").getValue();
    var Status = Xrm.Page.data.entity.attributes.get("vw_status").getValue();
    var PurchaseDate = FormatDate("vw_purchasedate");
    if (Xrm.Page.data.entity.attributes.get("vw_parentassetnoid").getValue() != null) {
    var ParentAssetNoId = Xrm.Page.data.entity.attributes.get("vw_parentassetnoid").getValue()[0].id;
    var ParentAssetNoIdName = Xrm.Page.data.entity.attributes.get("vw_parentassetnoid").getValue()[0].name;
    var ParentAssetNoIdType = Xrm.Page.data.entity.attributes.get("vw_parentassetnoid").getValue()[0].entityType;
    }

    //define default values for new Incident record
    var parameters = {};
    
    if (Name != null) { parameters["vw_name"] = Name + " - COPY"; }
    if (AsseTagNo != null) { parameters["vw_assettagno"] = AssetTagNo; }
    if (Status != null) { parameters["vw_status"] = Status; }
    if (Condition != null) { parameters["vw_condition"] = Condition; }
    if (ParentAssetNoId != null && ParentAssetNoIdName != null) { parameters["vw_parentassetnoid"] = ParentAssetNoId; parameters["vw_parentassetnoidname"] = ParentAssetNoIdName; }
    if (PurchaseDate != null) { parameters["vw_purchasedate"] = PurchaseDate; }
  
    //pop form with default values
    Xrm.Utility.openEntityForm("EntityName", 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;
}


*Note this function cannot return form with a lot of field. Because this function pass parameter through url and there limitation on url max character.

0 comments: