Featured Post

Dynamics 365 - Business Process Flow and Things to take note before you upgrade into Dynamics 365.

In 2 months ago, my company decided to upgrade the Dynamics CRM 2016 into Dynamics 365 and there is something different about Dynamics 365 B...

Thursday, January 15, 2015

Dynamics CRM 2013 - Useful JavaScript

Get a value from CRM field

var value= Xrm.Page.getAttribute(“fieldschemaname”).getValue();

Set Value to field

Xrm.Page.getAttribute(“fieldschemaname”).setValue();

Get Entity ID
var ID= Xrm.Page.data.entity.getId();


Hide a Tab/Section from CRM form

Tab 

Xrm.Page.ui.tabs.get(tabname/SectionName).setVisible(false);

Section inside a Tab 
Xrm.Page.ui.tabs.get("tab_3").sections.get("tab_3_section_2").setVisible(false);

Show a Tab/Section from CRM form

Tab 
Xrm.Page.ui.tabs.get(tabname/SectionName).SetVisible(true);

Section inside a Tab Xrm.Page.ui.tabs.get("tab_3").sections.get("tab_3_section_2").setVisible(true);


Set Focus

Xrm.Page.getControl("fieldschemaname").setFocus(true);


Expand / Collapse Tab

Expand tab
Xrm.Page.ui.tabs.get("tabname").setDisplayState("expanded");

Collapse tab
Xrm.Page.ui.tabs.get("tabname").setDisplayState("collapsed");

Set Requirements field

Xrm.Page.getAttribute(“fieldschemaname”).setRequiredLevel("none");
Xrm.Page.getAttribute(“
fieldschemaname”).setRequiredLevel("required");
Xrm.Page.getAttribute(“
fieldschemaname”).setRequiredLevel("recommended");

Set Focus field

Xrm.Page.getControl(“fieldschemaname”).setFocus(true);

Get Security role

Xrm.Page.context.getUserRoles();

Get Lookup ID and name

var lookupvalue = new Array();

lookupvalue = Xrm.Page.getAttribute("fieldschemaname").getValue();

if (lookupvalue !=null)
{
  var name = lookupvalue [0].name;
  var id = lookupvalue [0].id;
   
}


Refresh Ribbon Button

Xrm.Page.ui.refreshRibbon()

Set a lookup value

var lookup = new Array();
lookup[0] = new Object();
lookup[0].id =valueid;
lookup[0].name = valuename;
lookup[0].entityType = entityname;
Xrm.Page.getAttribute ("fieldschemaname").setValue(lookup); 


Set error message

Xrm.Page.getControl("fieldschemaname").setNotification("message");

Get Current User

var userID = Xrm.Page.context.getUserId();
var userName = Xrm.Page.context.getUserName();

Save and Close

Save function
Xrm.Page.data.entity.save();

Save and Close function
Xrm.Page.data.entity.save("saveandclose");

Save and New function
Xrm.Page.data.entity.save("saveandnew");

Close function
Xrm.Page.ui.close();

Get Form Type

var formtype = Xrm.Page.ui.getFormType();




No comments:

Post a Comment