JavaScript Terminology In MSCRM

1)Get the value from a CRM field

Xrm.Page.getAttribute(“CRM Field Schema Name”).getValue();

OR

Xrm.Page.data.entity.attributes.get(“—-“).getValue();

 

2)Set the value of a CRM Field

Xrm.Page.getAttribute(“—“).setValue(“MyValue”);

 

3) Hide/Show field

  1. Xrm.Page.ui.controls.get(fieldName).setVisible(false);
  2. Xrm.Page.ui.controls.get(fieldName).setVisible(true);

OR

Xrm.Page.getControl(“CRM field name”).setVisible(true);

 

4) Hide/Show a Tab/section (for Tab 5)

Xrm.Page.ui.tabs.get(5).SetVisible(false);

Xrm.Page.ui.tabs.get(5).SetVisible(true);

 

5) Disable/Enable field

  1. Xrm.Page.getControl(fieldName).setDisabled(true);
  2. Xrm.Page.getControl(fieldName).setDisabled(false);

OR

Xrm.Page.ui.controls.get(fieldname).setDisabled(true);

 

6) Set the required level

Xrm.Page.getAttribute(“CRM field name”).setRequiredLevel(“none”);

setRequiredLevel(“required”);

setRequiredLevel(“recommended”);

 

7)Get the selected value of Picklist

Xrm.Page.getAttribute(“CRM Field Name”).getSelectedOption().value;

Xrm.Page.getAttribute(“CRM Field Name”).getSelectedOption().text; — for Text

 

8)Call the onchange event of a field.

Xrm.Page.getAttribute(“CRM field name”).fireOnChange();

 

9) Set the focus to a field

Xrm.Page.getControl(“CRM field name”).setFocus(true);

 

10) Get Form Type

Xrm.Page.ui.getFormType();

 

11) Get Control Type

Xrm.Page.getControl(“CRM fieldname”).getControlType();

 

12) Stop an event on save event

event.returnValue=false;

 

13) Return array of strings of users security role GUIDs

Xrm.Page.context.getUserRoles()

14) Xrm.Page.getControl(“Webresourcename “).getObject. contentWindow. document. getElementByID(“divId1”)or (“bodyID”)  — For Accessing webresource HTML

 

15) a) Prevent the AutoSave.

function preventAutoSave(context)

{

Var onsave=context.getEventArgs();returns the event on which the script is currently running on

If(Xrm.page.getAttribute(“schema name”).getValue())

{

onsave.preventDefault();//Prevents the form from saving

}

}

b)Stop  the AutoSave Event

function  stopSave(context)

{

Var onsave=context.getEventArgs();returns the event on which the script is currently running on

If(onsave.getSaveMode==70) //  70 indicates Form Autosave Event

{

onsave.preventDefault();//Prevents the form from saving

}}

All Save 1
All Save and Close 2
All Save and New 59
All AutoSave 70
Activities Save as Completed 58
All Deactivate 5
     All Reactivate 6
User or Team owned entities Assign 47
Email (E-mail) Send 7
Lead Qualify 16
Lead Disqualify 15

 

 

 

16) Return array of strings of users security role GUIDs:

Xrm.Page.context.getUserRoles()

Original link here.

 

17) Get record GUID

var recordId = Xrm.Page.data.entity.getId()

var recordIdWithoutCurlyBraces = Xrm.Page.data.entity.getId().substring(1,37);

 

18) Get event source

var eventSource = executionContext.getEventSource(); // tick Pass execution context as first parameter

 

19) Get event source (fieldname)

var fieldName = executionContext.getEventSource().getName(); // tick Pass execution context as first parameter

 

20) Get event source (value)

var fieldValue= executionContext.getEventSource().getValue(); // tick Pass execution context as first parameter.

 

21) Set Lookup Value

What is lookup field?

A Lookup field type represents the relationship attribute on the related entity.

 

Required Attributes of lookup fields

id : The GUID of the item. Required for set.

name : The name of the item to be displayed. Required for set.

entityType : The entity name of the item. Required for set.

 

How to get values of a lookup field?
1

2

3

4

5

6

7

8

9

function getLookupDetails() {

var entityName, entityId, entityLabel, lookupFieldObject;

 

// parentaccountid is the lookup field name that we try to reach its values

lookupFieldObject = Xrm.Page.data.entity.attributes.get(‘parentaccountid’);

if (lookupFieldObject.getValue() != null) {

entityId = lookupFieldObject.getValue()[0].id;

entityName = lookupFieldObject.getValue()[0].name;

entityLabel = lookupFieldObject.getValue()[0]. entityType;

}

 

Get a lookup value
var lookup = new Array();
lookup = Xrm.Page.getAttribute(“attributename”).getValue();
if (lookup != null) {
var name = lookup[0].name;
var id = lookup[0].id;
var entityType = lookup[0].entityType;
}

Set a lookup value
var lookup = new Array();
lookup[0] = new Object();
lookup[0].id = recorid;
lookup[0].name = recordname;
lookup[0].entityType = entityname;
Xrm.Page.getAttribute(“attributename”).setValue(lookup);

 

How to set a lookup field value?

The function below sets new_teamid lookup field:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

function setLookupField() {

 

var lookupData = new Array();

var lookupItem = new Object();

//Set the GUID

lookupItem.id = myTeamId;

//Set the name

lookupItem.name = myTeamName;

lookupItem.entityType = “team”;

lookupData[0] = lookupItem;

 

//If existing value is empty, then set new value

var existingValue = Xrm.Page.getAttribute(“new_teamid”).getValue();

 

if (existingValue === null) {

Xrm.Page.getAttribute(“new_teamid”).setValue([{

id: myTeamId,

name: myTeamName,

entityType: “team”

}]);

} else {

return;

}

}

 

 

Xrm.Page.getAttribute(‘new_fieldid’).setValue([{ id: ‘guid’, name: fullName, entityType: ‘entityTypeName’}]);

OR

Xrm.Page.getAttribute(“attributename”).setValue([{ id: recorid, name: recordname, entityType: entityname}]);

Original link here.

 

22) Get CRM Form Type

 

  1. var FORM_TYPE_CREATE = 1;
  2. var FORM_TYPE_UPDATE = 2;
  3. var FORM_TYPE_READ_ONLY = 3;
  4. var FORM_TYPE_DISABLED = 4;
  5. var FORM_TYPE_QUICK_CREATE = 5;
  6. var FORM_TYPE_BULK_EDIT = 6;
  7. var formType = Xrm.Page.ui.getFormType();

if(formType == FORM_TYPE_CREATE) {

}

else{

}

23)A) How to grab the selected value from an option set?

var selectedOptionSet = Xrm.Page.getAttribute(“new_fieldname”);

alert(selectedOptionSet.getSelectedOption().text);   alert(selectedOptionSet.getSelectedOption().value);

B)How to grab all the values in the option set??

var options = Xrm.Page.getAttribute(‘new_country’).getOptions();

for (var i in options)

{

alert(‘value :’ + options[i].value);

alert(‘Text :’ + options[i].text);

}

use this code to loop through the options available within the option set and get the respective value and text of the options.

 

In this tutorial we will cover how to work with crm option set value (OptionSetValue) fields using javascript in Microsoft Dynamics CRM.

This article covers examples about:

  • How to get an Option by passing value?
  • How to get selected CRM Option Set Valueitem?
  • How to get selected value of an CRM Option Set Value?
  • How to get Text of an CRM Option Set Value?
  • How to set an CRM Option Set Value (by numeric value)?
  • How to set an CRM Option Set Value by Text?

 

How to get an Option by passing value?

The code below returns an Option object with text and value properties. value argument represents the numeric value of the optinonsetvalue field for searching the Option:

1 var myOption = Xrm.Page.getAttribute(“fieldname”).getOption(value);

 

How to get selected CRM Option Set Value item?

The code below returns the Option object with text and value properties:

1 var selectedOption = Xrm.Page.getAttribute(“fieldname”).getSelectedOption();

 

How to get selected value of an CRM Option Set Value?

The code below gives us the numeric value of selected OptionSetValue field:

1 var optionsetValue = Xrm.Page.getAttribute(“fieldname”).getValue();

 

How to get Text of an CRM Option Set Value?

The code below gives us the text of selected OptionSetValue field:

1 var optionsetText = Xrm.Page.getAttribute(“fieldname”).getText();

 

How to set an CRM Option Set Value (by numeric value)?

The code below helps us to set the value of OptionSetValue by a numeric value passed to the setValue function:

1 Xrm.Page.getAttribute(“fieldname”).setValue(value);

 

How to set an CRM Option Set Value by Text?

Actually we can’t set the selected item by text but we can search all options until we find the one whose text (label) is the same as one the one you have then get its value like below:

1

2

3

4

5

6

7

function setOptionSetValueByText(“fieldname”, optionText) {

var options = Xrm.Page.getAttribute(“fieldname”).getOptions();

for (i = 0; i < options.length; i++) {

if (options[i].text == optionText) {

Xrm.Page.getAttribute(“fieldname”).setValue(options[i].value);

}

}

}

 

6) getClient

Returns a value to indicate which client the script is executing in.

JScript

context.client.getClient()

Client Value
Browser Web
Outlook Outlook
Mobile Mobile

 

7) getClientState

Use this instead of the removed isOutlookOnline method.

Returns a value to indicate the state of the client.

JScript

context.client.getClientState()

Client Values returned
Web, Outlook, Mobile Online
Outlook, Mobile Offline

 

I have one situation, login user has multiple security roles, if the login user has permission for particular role, show some buttons/fields otherwise the hide buttons/fields.

It is not stright forward to find out the security role, Soi wrote one javascript to find out the security role.

Here i am checking the user has “Content Manager” security role or not.

varCMRole = CheckUserRole(“Content Manager”);
if (CMRole) {
//true to show buttons
} else {
//false to hide buttons
}
This function will get all the user’s security role Id, passing security role id to get the security role name. If it match, it is true otherwise false.

 

functionCheckUserRole(roleName) {
var currentUserRoles = Xrm.Page.context.getUserRoles();
for (vari = 0; i<currentUserRoles.length; i++) {
var userRoleId = currentUserRoles[i];
var userRoleName = GetRoleName(userRoleId);
if (userRoleName == roleName) {
return true;
}
}
return false;
}

 

 

Advertisements

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 )

Google+ photo

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

Connecting to %s