CRM 2011 – Javascript Xrm.Page Basics

Posted on February 17, 2011

68


I have been looking at the CRM 2011 Javascript SDK, samples. Comparing it to CRM 4 code and getting very angry.  It just wasn’t the same and I wasn’t understanding the SDK, it was written in an odd way and the examples were just much more complex than I needed to get started.  I have also written a good blog entry about the comparison between Javascript CRM 2011 and CRM 4

I could hide sections and tabs ok, using the Page ui but setting variables was really starting to bug me. I could get it too work using crmForm but I wanted to move on and use the new code.

I think the problem is highlighted looking at the new Javascript layout

The UI bit makes sense but looking at this I couldn’t see how to get the actual fields!!!

there is also a big difference with the actual Javascript you write everything has to get something

Xrm.Page.getAttribute(“hosk_display2″)

Xrm.Page.ui.tabs.get(“hosk_tab1″)

once you get the variable you can then getValue or setValue

Xrm.Page.getAttribute(“hosk_display2″).setValue(“HOSK SET THIS CRM 2011 STYLE”);

Xrm.Page.ui.tabs.get(“hosk_tab1″).sections.get(“Permanent”).setVisible(false);

JavaScript CRM 4 to CRM 2011 converter tool:

http://crm2011scriptconvert.codeplex.com/

and looking at these tidbits at this blog

Here are common 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()

god bless the boys from Power Objects

About these ads