Form programming methods

The following table lists the form programming methods available for updated entities only.

Method Description
Xrm.Page.context.client.getClient Returns a value to indicate which client the script is executing in.
Xrm.Page.context.client.getClientState Returns a value to indicate the state of the client.
Xrm.Page.context.getUserName Returns the name of the current user.
Xrm.Page.data.entity.getPrimaryAttributeValue Gets a string for the value of the primary attribute of the entity.
Xrm.Page.data.refresh Asynchronously refreshes the data of the form without reloading the page.
Xrm.Page.data.save Saves the record asynchronously with the option to set callback functions to be executed after the save operation is completed.
Xrm.Page.data.entity attribute.getIsPartyList Determines whether a lookup attribute represents a partylist lookup.
Xrm.Page.ui control.clearNotification Removes a message already displayed for a control.
Xrm.Page.ui control.setNotification Displays a message near the control to indicate that data is not valid.
Xrm.Page.ui.clearFormNotification Use this method to remove form level notifications.
Xrm.Page.ui.setFormNotification Use this method to display form level notifications.
Xrm.Page.ui control.addCustomFilter Use fetchXml to add additional filters to the results displayed in the lookup. Each filter will be combined with any previously added filters as an ‘AND’ condition.
Xrm.Page.ui control.setShowTime Specifies whether a date control should show the time portion of the date.
Xrm.Utility.alertDialog Displays a non-blocking alert dialog with a callback function.
Xrm.Utility.confirmDialog Displays a non-blocking confirm dialog with different callbacks depending on the button clicked by the user.

PreSearch Event in Microsoft Dynamics 365

PreSearch event for updated entities

The new PreSearch event occurs just before the search dialog box opens when you set a lookup value. This event does not have user interface to set an event handler in the application; it can only be set using the Xrm.Page.ui control.addPreSearch method. Use this event with the addCustomFilter, addCustomView and setDefaultView methods to control the views opened when people search for a record to set as the value of a lookup field.

Use this method to apply changes to lookups based on values current just as the user is about to view results for the lookup.

Xrm.Page.getControl(arg).addPreSearch(handler)
Arguments
Type: Function to add.

Remarks
This method is only available for Updated entities.

The argument is a function that will be run just before the search to provide results for a lookup occurs. You can use this handler to call one of the other lookup control functions and improve the results to be displayed in the lookup.

 

Use this method to remove event handler functions that have previously been set for the PreSearch event.

Xrm.Page.getControl(arg).removePreSearch(handler)
Arguments
Type: Function to remove.

Remarks
This method is only available for Updated entities.

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,