Javascript – Random Snippets.

This is a slightly random post!

I will publish future posts on specific pieces of useful JavaScript but here I have some random but hopefully useful code snippets.

Maximise Screen

Add this code to the onload event of a form to maximise the screen.
A word of caution: If you are using Unified Service Desk I have found problems with this code! So might be worth avoiding if you use USD. (But also probably not needed because of the nature of a USD application!),0);, screen.height);

Change Tab Display Title

I have found this code useful (occasionally) if I want to dynamically set a tab name.

Notice that get(1) refers to the first tab. This does mean this code becomes dependant on the position of the tabs. Moving stuff around on your form could “break” this code!

var Tab = Xrm.Page.ui.tabs.get(1);
if(Tab != null) {
   Tab.setLabel(“ANY TABNAME YOU LIKE!”);

Lookup Fields

Get values

Grabbing the text value of a lookup field is slightly more complicated than with a standard text fields. With the lookup being an entity you first need to assign to an object that get the attribute your require.
var lookupObject = Xrm.Page.getAttribute(“alookupfieldname”);
if (lookupObject != null) {
   var lookUpObjectValue = lookupObject.getValue();
   if ((lookUpObjectValue != null)) {
     var lookuptextvalue = lookUpObjectValue[0].name;
     var lookupid = lookUpObjectValue[0].id;

Set value

As lookup fields are objects setting their values using code can be a little tricky! You need three pieces of information to achieve this. Including the type of lookup entity, the name (text) of the value you want to assign and the guid.

var value = new Array();
value[0] = new Object();
value[0].id = idValue;
value[0].name = textValue;
value[0].entityType = typeValue;

Alternatively you can use this syntax;

 id: idValue,
 name: textValue,
 entityType: "<<Name Of Entity>>"

Optionsets – Get text“afieldName”).getText();

Text Boxes

Set Value

Xrm.Page.getAttribute(“afieldname”).setValue(“ANY VALUE!”);

Get Value


Get a Field Label

var fieldLabel = Xrm.Page.ui.controls.get(“afieldName”).getLabel();

Set Field Label

var aControl = Xrm.Page.ui.controls.get(“afieldname”);
var newLabel = “ANY LABEL YOU LIKE!”;

Make Mandatory


Set Visibility

var control = Xrm.Page.ui.controls.get(“afieldname”);

Make Readonly


Hide tabs


Hide/Show Sections

To show a section :
Xrm.Page.ui.tabs.get(“yourtabname”).sections.get(“your section name”).setVisible(true);

To hide a section:
Xrm.Page.ui.tabs.get(“yourtabname”).sections.get(“your section name”).setVisible(false);

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