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,

Access MS CRM 2011 with IP address ,can get Odata Java Script Query Results

Access MS CRM 2011 with IP address ,can get Odata Java Script Query Results:

 

Some times we may access the CRM with IP address as below.AccessCrmWithIp

 

At this point of time CRM Javascript  Xrm.Page.context.getServerUrl() cant get the Server Url. To over come this use the following Java Script code to get the Server Url.

var serverUrl = document.location.protocol + “//” + document.location.host + “/” + Xrm.Page.context.getOrgUniqueName(); 

This will work fine for the both IP and CRM Serverl Urls.

Please have a glance the below example.

Below i am commenting var serverUrl = Xrm.Page.context.getServerUrl();

function OdataRetrieveMultipleUsingJquery(ODataFilterQuery) {
//var serverUrl = Xrm.Page.context.getServerUrl();

var serverUrl = document.location.protocol + “//” + document.location.host + “/” + Xrm.Page.context.getOrgUniqueName();
var Odata_OrgName = “/xrmservices/2011/OrganizationData.svc”;
var oDataUri = serverUrl + Odata_OrgName + ODataFilterQuery;
var jSonArray = new Array();

jQuery.ajax({
type: “GET”,
contentType: “application/json; charset=utf-8″,
datatype: “json”,
url: oDataUri,
async: false,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader(“Accept”, “application/json”);
},
success: function (data, textStatus, XmlHttpRequest) {
if (data && data.d != null) {
jSonArray.push(data.d);
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
alert(“Invalid Record Selection or No Record found with the Search Item”);
}
});

return jSonArray;
}

 

Hope this helps you.

 

Regards,