Using below function we can Set Current System Time only to Date Field

function SetCurrentTimeOnly(fieldname) {

var d = Xrm.Page.getAttribute(fieldname).getValue();
var mydate = new Date();
var s = mydate.getSeconds();
var m = mydate.getMinutes();
var h = mydate.getHours();

if (d != null) {
if (d.getHours() == 0) {
d.setHours(h);
d.setMinutes(m);
d.setSeconds(h);
d.setMilliseconds(30);
Xrm.Page.getAttribute(fieldname).setValue(d);
}
}
}

 

Cheers,

Validate Phone Number Field in MS CRM

/***********************************************************************************************
*******************************Validate Phone Number Field**************************************
/***********************************************************************************************/
function ValidatePhoneNumber() {
// Binding the events to crm field, which needs to be validated
crmForm.all.<FieldName>.onkeypress = isvalidPhone;

}
// Allows to enter only Numbers & upto 10 char’s
function isvalidPhone() {

var charCode = event.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;

var phnum = event.srcElement.value.replace(/[^0-9]/g, “”);

if (phnum.length >= 10)
return false;
return true;
}

Generic function to set default Entity and default view in MultiSelection lookup

*Generic function to set default Entity in Multilookup and Default View
function setDefaultEntityInMultiLookUpAndView(MultiLookUpSchemaName, DefaultEntityObjTypeCode, DefaultLookupEntityObjTypeCode, EntityLookUpImageUrl, ViewGuid) {
document.getElementById(MultiLookUpSchemaName).setAttribute(“defaulttype”, DefaultEntityObjTypeCode);
document.getElementById(MultiLookUpSchemaName).setAttribute(“lookuptypes”, DefaultLookupEntityObjTypeCode);
document.getElementById(MultiLookUpSchemaName).setAttribute(“lookuptypeIcons”, EntityLookUpImageUrl);
Xrm.Page.getControl(MultiLookUpSchemaName).setDefaultView(ViewGuid);
}

 

pass the parameters to the above function like below

setDefaultEntityInMultiLookUpAndView(“<new_lookupFiledName>”, “2”, “2”, “/_imgs/ico_18_2.gif”, “<View Guid>”);

Validate the Required fileds on MS CRM Form while saving from Custom Button

Some times we may use the Custom button to save the form. In this case using below function we can validate whether Required Form Fields are filled or not programmatically
function IsFormValidForSaving() {
var valid = true;
Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {
if (attribute.getRequiredLevel() == “required”) {
if (attribute.getValue() == null) {
if (valid) {
var control = attribute.controls.get(0);
alert(‘You must provide a value for’ + ‘ ‘ + control.getLabel());
control.setFocus();
}
valid = false;
}
}
});
return valid;
}

 

Cheers,

Reading Query String Parameters using JavaScript

Hi ,

Using below function we can read the Query String Parameters .

 

function getQuerystringParams(key) {
key = key.replace(/[\[]/, “\\\[“).replace(/[\]]/, “\\\]”);
var regex = new RegExp(“[\\?&]” + key + “=([^&#]*)”);
var qs = regex.exec(window.location.href);
if (qs != null) {
return qs[1];
}
return null;
}

 

call like below

var name = getQuerystringParams(“_Name”) ? decodeURIComponent(getQuerystring(“_Name”)) : null;

Cheers,