Home > CRM 2015 > CRM 2015 – Useful JScript Snippets and Syntaxes

CRM 2015 – Useful JScript Snippets and Syntaxes

Below are the list of useful scripts and syntax’s

Filter look up records

Let’s consider a scenario, “Primary Contact” lookup on Account form should display only Contacts with “Business Phone” (i.e., Contact.telephone1 != Null)

Steps :

  • Add PreSearch event for ‘Primary Contact’ lookup
  • In the event handler define Filter and set to the lookup using ‘addCustomFilter’ method


// Attach below ‘onload’ function to Form onload event

function onload() {



function filterCustomerContacts() {

//Only show Contacts with ‘Business Phone’

var primaryContactFilter = “<filter type=’and’><condition attribute=’telephone1′ operator=’not-null’ /></filter>”;

Xrm.Page.getControl(“primarycontactid”).addCustomFilter(primaryContactFilter, “contact”);


Filter Primary Contact Lookup

Filter Primary Contact Lookup

Get fields of specific types

One of my blog follower asked this question “How to get list of all Option Set fields placed on the form?”.

Below is the Script to get Attributes based on Type or Requirement Level using “”


function onload() {

var requiredAttributeNames = [];

var optionsetAttributeNames = [];

function (attribute, index) {

if (attribute.getRequiredLevel() == “required”) {



if (attribute.getAttributeType() == “optionset”) {




alert(“Required Attributes on the form are : ” + requiredAttributeNames.join());

alert(“Attributes of Type Optionset on the form are : ” + optionsetAttributeNames.join());


Callback functions on Save

Saves the record asynchronously with the option to set callback functions to be executed after the save operation is completed.

Script, errorCallback);

successCallback() {

alert(“Form saved !!!”);


function errorCallback(saveErrorResponse) {

if (saveErrorResponse != null) {

if (saveErrorResponse.message != ‘undefined’ && saveErrorResponse.message != null) {

alert(“Error on Save – ” + saveErrorResponse.message);




Get control placed on Header

  • var nameControlInHeader = Xrm.Page.getControl(“header_{fieldname}”);

Get control placed in business process flow

  • var nameControlInBPF = Xrm.Page.getControl(“header_process_{fieldname}”);
  • We can get controls in the active stage.

Hide\Show time portion of Date Control

  • Page.getControl(“createdon”).setShowTime(true/false);


  • getUserPrivilege – When field level security has been applied to an attribute, determine whether a user has privileges to perform create, read, or update operations on the attribute


  • getMin – Get the minimum allowed value for an attribute that contains a number


  • getMax – Get the maximum allowed value for an attribute that contains a number


  • getIsPartyList – Determines whether an attribute is a partylist.


  • getAttributeType – Get the type of attribute (i.e., Option set,Text etc…)


  • getFormat- Get attribute’s format. (i.e., Text,URL,Email etc…)


  • getInitialValue – Get the initial value of a Boolean or Optionset attribute


  • getMaxLength – Get the maximum allowed length for an attribute that contains a string


  • alertDialog – Display a non-blocking alert dialog with a callback function.

var alertDisplayed = false;
“Showing Alert”,
function () { alertDisplayed = true; }

  • confirmDialog – Display a Non-blocking Confirm dialog with different callbacks depending on the button clicked by the user.

var agree = false;
“Do you agree?”,
function () { agree = true;},
function () { agree = false; }


Categories: CRM 2015 Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: