Some Useful JavaScript Syntax
- Get Form Type
Xrm.Page.ui.getFormType(); // Return(1,2,3,4,5,6)
1 Create
2 Update
3 Read Only
4 Disabled
5 Quick Create (Deprecated)
6 Bulk Edit
- Set time delay to run javascript
window.setTimeout("FunctionName()", 100);
- Set Label of Field Using JavaScript
Xrm.Page.ui.controls.get(fieldname).setLabel(‘New label’);
- Set Tooltip on CRM Field
crmForm.all.my_custom_attribute_c.title = "This Text is visible on Mouse over";
- Get id of lookup Field
Xrm.Page.getAttribute("fieldName").getValue()[0].id;
- To set values in Party List field.
var partlistset= new Array();
partlistset[0] = new Object();
partlistset[0].id = id; // provide a guid type value
partlistset[0].name = name; // provide a suitable name
partlistset[0].entityType = entityType; // provide the entity name of the item ie account/ contact etc .
Xrm.Page.getAttribute("resource").setValue(partlistset);
- All Date Function in JavaScript
Date Functions
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write(month + "/" + day + "/" + year)
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10)
minutes = "0" + minutes
document.write(hours + ":" + minutes + " ")
if(hours > 11)
{
document.write("PM")
}
else
{
document.write("AM")
}
getTime() - Number of milliseconds since 1/1/1970 @ 12:00 AM
getSeconds() - Number of seconds (0-59)
getMinutes() - Number of minutes (0-59)
getHours() - Number of hours (0-23)
getDay() - Day of the week(0-6). 0 = Sunday, ... , 6 = Saturday
getDate() - Day of the month (0-31)
getMonth() - Number of month (0-11)
getFullYear() - The four digit year (1970-9999)
MAKE THE RECIPIENT BLANK FOR NEW EMAILS
var CRM_CREATE_FORM = 1;
var formType = crmForm.FormType;
if(formType == CRM_CREATE_FORM)
{
crmForm.all.to.DataValue = null;
}
- Find Out number of days between two date
function
Datedifference() {
var day1, day2;
var month1, month2;
var year1, year2;
var value1 = Xrm.Page.getAttribute("new_startdate").getValue();
var value2 = Xrm.Page.getAttribute("new_enddate").getValue();
if (value1 != ""
&& value2 != "") {
var firstDate1 = new
Date(value1);
var secondDate1 = new
Date(value2);
day1 = firstDate1.getDate();
month1 = firstDate1.getMonth();
year1 = firstDate1.getYear();
day2 = secondDate1.getDate();
month2 = secondDate1.getMonth();
year2 = secondDate1.getYear();
date1 = year1 + "/" + month1 + "/" + day1;
date2 = year2 + "/" + month2 + "/" + day2;
//alert(date1 + " " +
date2);
var firstDate = new
Date(date1)
var secondDate = new
Date(date2)
var msPerDay = 24 * 60 * 60 * 1000
var dbd = Math.ceil((secondDate.getTime() -
firstDate.getTime()) / (msPerDay));
if (dbd != "0"
&& value2 != null && value1 != null) {
Xrm.Page.getAttribute("new_durationindays").setValue(dbd
+ " days");
}
}
}