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

Script:

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

function onload() {

Xrm.Page.getControl(“primarycontactid”).addPreSearch(filterCustomerContacts);

}

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 “Xrm.Page.data.entity.attributes.forEach”

Script:

function onload() {

var requiredAttributeNames = [];

var optionsetAttributeNames = [];

Xrm.Page.data.entity.attributes.forEach(

function (attribute, index) {

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

requiredAttributeNames.push(attribute.getName());

}

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

optionsetAttributeNames.push(attribute.getName());

}

});

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

Xrm.Page.data.save().then(successCallback, 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);

Syntaxes

  • 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

Xrm.Page.getAttribute(“name”).getUserPrivilege().canUpdate;

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

Xrm.Page.getAttribute(“creditlimit”).getMin()

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

Xrm.Page.getAttribute(“creditlimit”).getMax()

  • getIsPartyList – Determines whether an attribute is a partylist.

Xrm.Page.getAttribute(“to”).getIsPartyList()

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

Xrm.Page.getAttribute(0).getAttributeType()

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

Xrm.Page.getAttribute(0).getFormat()

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

Xrm.Page.getAttribute(“address1_addresstypecode”).getInitialValue()

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

Xrm.Page.getAttribute(“name”).getMaxLength()

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

var alertDisplayed = false;
Xrm.Utility.alertDialog(
“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;
Xrm.Utility.confirmDialog(
“Do you agree?”,
function () { agree = true;},
function () { agree = false; }
);

🙂

Advertisement
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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s

%d bloggers like this: