CRM Developer Form Scripting @DavidYack
Using Form Scripting Allows dynamic business rules to be implemented on forms Script can run in response to Form Events Form Script is uploaded and run from CRM Form Scripting implements rules specific to CRM Form presentation Form Scripting automates tasks to provide consistency and improved user experience Xrm.Page API provided to interact with CRM controlled Forms
Example Scripting Scenarios Field is only required when another field has a specific value Option Set list should only contain a subset of values Hide or Show Tabs, Sections and Fields based on other values Format Data e.g. Phone Number Custom validation of data content including stopping save of form
Form Events OnLoad OnSave OnChange TabStateChange OnReadyStateComplete
Developing Form Script Form Script is uploaded as a JavaScript web resource Each form then references the JavaScript library One JavaScript Web Resource can be shared across multiple forms Once Referenced events can be registered for the form and its fields
Demo Using Client Scripting Demo Time
Xrm.Page API JavaScript API for interacting with the CRM Form and associated data Xrm.Page is the only supported way to interact with the Form API has UI centric methods and data centric methods Type of form can limit what parts of the API is available for use Field must be on form for data to be available for scripting
Xrm.Page Object Model
Getting Attribute vs. Get Control Get by attribute name var titleattrib= Xrm.Page.getAttribute( snap_title"); Get all attributes var allattribs= Xrm.Page.getAttribute(); Get first control for attribute var titlecontrol = Xrm.Page.getControl( snap_title"); Get all controls for attribute var titlecontrols = Xrm.Page.getAttribute( snap_title").controls.get(); Get Attribute for Control var titleattribute = Xrm.Page.getControl( snap_title").getattribute();
Getting and Setting Values Getting an attribute value var titlevalue = Xrm.Page.getAttribute( snap_title").getvalue(); Setting an Attribute value Xrm.Page.getAttribute( snap_title").setvalue( Yellowstone Winter"); Checking if an Attribute has changed var titlechanged = Xrm.Page.getAttribute( snap_title").getisdirty(); Force a field to be required Xrm.Page.getAttribute( snap_title").setrequiredlevel("required");
Working with Controls Checking if a control is Visible var titlevisible = Xrm.Page.getControl( snap_title").getvisible(); Hide a control Xrm.Page.getControl( snap_title").setvisible(false); Disable a control Xrm.Page.getControl( snap_title").setdisabled(true); Set focus on a control Xrm.Page.getControl( snap_title").setfocus(); Set the label for a control Xrm.Page.getControl( snap_title").setlabel( Landscape Photo Title");
Working with Tabs and Sections Collapse a tab Xrm.Page.ui.tabs.get("general").setDisplayState("collapsed"); Show a tab Xrm.Page.ui.tabs.get("general").setVisible(true); Get the section for a control and hide it Xrm.Page.getControl( snap_title ).getparent().setvisible(false); Set focus to tab containing control Xrm.Page.getControl( snap_title ).getparent().getparent().setfocus();
Collection Methods foreach applies a delegate function to each item in the collection getlength returns the number of items in the collection get returns an array of items matching the parameters None returns all items in the collection String returns item where name matches string Number returns item at the index specified Delegate Function returns items that the delegate function returns true
Demo Using Attributes, Controls and Tabs Demo Time
Using the Client Context getclienturl provides a client side URL for accessing the CRM server from the client getuserid quickly get the GUID for the current user getuserroles GUID list for each role the user is part of getquerystringparameters array of key/value representing query string getuserlcid get language Id for the current user.
Working with Option Sets Getting options for an Option Set control var options = optionsetcontrol.getattribute().getoptions(); Clear all options currently configured for the display control optionsetcontrol.clearoptions(); Add only specific options back to the control list optionsetcontrol.addoption(option);
Demo Using Option sets Demo Time
Form Scripting Tips Any field involved in Hide/Show scripting should be hidden to start with Fields can be on forms multiple times Group fields that need to be hidden/shown into a common tab/section Fields must be on the form body to access their data or control properties
Wrapping Up Client Script can bring the CRM user experience to life Stick to supported use of the API for enforcing business rules and not for un-supported HTML manipulation