Skip to main content

CRM 2011 Useful JavaScript tidbits

http://www.powerobjects.com/blog/2011/01/14/crm-2011-useful-javascript-tidbits/

Get the value from a CRM field

var varMyValue = Xrm.Page.getAttribute(“CRMFieldSchemaName”).getValue() ;

Set the value of a CRM field

Xrm.Page.getAttribute(“po_CRMFieldSchemaName”).setValue(‘My New Value’);

Hide/Show a tab/section

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

Call the onchange event of a field

Xrm.Page.getAttribute(“CRMFieldSchemaName”).fireOnChange();

Get the selected value of picklist

Xrm.Page.getAttribute(“CRMFieldSchemaName”).getSelectedOption().text;

Set the requirement level

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

Set the focus to a field

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

Stop an on save event

event.returnValue = false;

Return array of strings of users security role GUIDs:

Xrm.Page.context.getUserRoles()

Hide/Show Tabs and Sections

function setVisibleTabSection(tabname, sectionname, show) 
var tab = Xrm.Page.ui.tabs.get(tabname); 
if (tab != null) 
if (sectionname == null) 
tab.setVisible(show); 
else { 
var section = tab.sections.get(sectionname); 
if (section != null) 
section.setVisible(show); 
if (show) 
tab.setVisible(show); 
}

Comments

Ahmet TANRIKULU said…
my crm 2011 does not run a simple code gives error
object expected
the code is Xrm.Page.data.entity.getEntityName()
hero said…
Let me check this
Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
Vee Eee Technologies
Anonymous said…
Hi i am new to crm 2011.
i have a problem. i have set a field value default during on load . but i want to change the value dynamic during ON SAVE event, can u send me java script code for it.

this is my java script
Xrm.Page.getAttribute(“po_CRMFieldSchemaName”).setValue(‘My New Value’);

Popular posts from this blog

Capture and Handle event when one entity record is attached through a Many to Many relationship - MS CRM 4

If you have already dug up too much about trying to capture the event when an entity record in MS CRM is attached ~ linked to a record in many to many relationship. For example, if you have two entities : Supplier and Company in MS CRM where a supplier can do business with many companies and at the same time a Company can order stuff from many suppliers. What if you want to send some notification whenever a new supplier or an existing supplier is linked to a company; similarly you may want to send a notification when a Supplier signs a deal with 2-3 companies and gets linked to those. Some experts from MS CRM fraternity have suggested some ways to over ride this limitation of MS CRM that it does not expose any event for the above mentioned scenario, below is one of the examples:   http://ayazahmad.wordpress.com/2009/07/23/event-on-many-to-many-relationship-nn-in-crm-4/ But making changes to the default plugin model of MS CRM to add Associate or Disassociate event is not so...

Using MS CRM Calendar in your custom ASP .NET pages *** Not fully Supported Customisations ***

Hi, Have you been creating custom pages in line with MS CRM for creating , viewing and updating entity records ? Have you placed controls such as Date picker Lookup on your custom page and have tried in vein to make those look like MS CRM ? I found a way to use the same MS CRM calendar control as it appears in the application... For getting this calendar on your asp .net page, follow the steps below: 1) Create a copy of the date.js javascript file located in your-server/crmsite-folder/_static/_controls/datetime folder, give the copy some name like date_myapp.js. 2) Open the following .js files from different sub folders in side the  your-server/crmsite-folder/_static      folder :       Global.js       encodedecode.js       xmlutil.js       util.js       remotecommand.js              Copy and whole content of ...

Creating Email through Plugin / SDK in CRM 4 - line breaks don't work

You may have noticed when you create email using a sample code like below in a plugin or for that matter any other SDK based assembley for MS CRM. Your line breaks : \n or \r or System.Environment.NewLine will not work.           email Email = new email();                          Email.to = new activityparty[] { toParty };                      Email.from = new activityparty[] { fromParty };                      Email.regardingobjectid = regardingObject;                      Email.subject = "Hello!!";                      Email.description = "Dear, line 1" +                                Syst...