Tuesday, June 2, 2015

CRM 2011 – Javascript Xrm.Page Basics

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 from thetechnet really shows how Microsoft have split the functionality into logical sections, The picture from Technet shows the logical groups of functionality
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:
Here are some common Javascript functions you will need

Get the value from a field

Xrm.Page.getAttribute(“hosk_field”).getValue() ;

Set the value on a field

Xrm.Page.getAttribute(“hosk_field”).setValue(“new value”) ;

Setting requirement level for a field

Xrm.Page.getAttribute(“hosk_field”).setRequiredLevel(“none”);
There are three requirement levels “none”, “required”, “recommended”.  Required means it must be filled in (red asterisk), recommended is the blue cross and none is obvious.  It’s useful to be able to change the requirement settings dynamically because you can then make users fill in fields after they changed another field or make fields not required.

Hide/show tab

Xrm.Page.ui.tabs.get(“hosk_tab1″).sections.get(“Permanent”).setVisible(false);
hide = setVisable(false)
show = setVisable(true)
You can hide and show a section as well
Some interesting Javascript functions  from this blog tidbits at this blogs:

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 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()

No comments:

Post a Comment

How to Deploy your API as a web app or API app

  Before you can call your custom API from a logic app workflow, deploy your API as a web app or API app to Azure App Service. To make your ...