CRM 2011 Duplicate Function to Duplicate Record

0 comments


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.

CRM 2011 Get Last Two Character/ Last Character

0 comments


Have you ever encounter problems such as request for date format to be MMYY or YYMM?
Below i added JavaScript to cater for the problem.


function GenerateYYMM() {

var newvalueMonth;
var valueMonth = new Date().getMonth() + 1;
var valueYear = new Date().getFullYear();

        //get the last 2 character of year
var valueYearLength = valueYear.toString().length;
var YearToString = valueYear.toString();
var last_char=YearToString.charAt(valueYearLength-1);
var Before_last_char=YearToString.charAt(valueYearLength-2);
var LastTwoYearChar = Before_last_char + last_char;

        //get the last 2 character of month with length 2
var valueMonthLength = valueMonth.toString().length;
if (valueMonthLength != 2) {newvalueMonth = "0" + valueMonth.toString();}
else {newvalueMonth = valueMonth.toString();}

var NewYYMM = LastTwoYearChar + newvalueMonth;
return NewYYMM;
}

*Note based on today date. U can use this on any date field too.

C# Parse String to GUID/ Cast to system.guid

0 comments


*Example Script

Guid.Parse(string input)

CRM 2011 Disable All Field In Section

0 comments


*Example Script
Purpose : To disable all field in the section.

function DisableSectionAttributes(SectionName) {
    function setFldDisabled(ctrl) {
        ctrl.setDisabled(true);
    }
    function setFldEnabled(ctrl) {
        ctrl.setDisabled(false);
    }
    var ctrlName = Xrm.Page.ui.controls.get();
    for (var i in ctrlName) {
        var ctrl = ctrlName[i];
        var ctrlSection = ctrl.getParent().getName();
        if (ctrlSection == SectionName) {
            setFldDisabled(ctrl);
        }
    }
}

Using section name to disable the field.