Wednesday, January 27, 2010

Javascript functions for CRM 2011 - Part 2

Here’s an example of how to call the function (I retrieve the details of one lookup field and then call the above function to populate another lookup field):

1var ExistingCase = Xrm.Page.data.entity.attributes.get("new_existingcase");
2if (ExistingCase.getValue() != null) {
3 var ExistingCaseGUID = ExistingCase.getValue()[0].id;
4 var ExistingCaseName = ExistingCase.getValue()[0].name;
5 SetLookupValue("regardingobjectid", ExistingCaseGUID, ExistingCaseName,"incident");
6}

Set the Requirement Level of a Field:

Note: this example sets the requirement level of the Address Type field on the Account form to Required.

Note: setRequiredLevel(“none”) would make the field optional again.

1function SetRequirementLevel() {
2 var AddressType = Xrm.Page.data.entity.attributes.get("address1_addresstypecode");
3 AddressType.setRequiredLevel("required");
4}

Disable a field:

1function SetEnabledState() {
2 var AddressType = Xrm.Page.ui.controls.get("address1_addresstypecode");
3 AddressType.setDisabled(true);
4}

Hide a field:

1function hideName() {
2 var name = Xrm.Page.ui.controls.get("name");
3 name.setVisible(false);
4}

Hide a nav item:

Note: you need to refer to the nav id of the link, use F12 developer tools in IE to determine this

1function hideContacts() {
2 var objNavItem = Xrm.Page.ui.navigation.items.get("navContacts");
3 objNavItem.setVisible(false);
4}

Hide a Section:

Note: Here I provide a function you can use. Below the function is a sample.

1function HideShowSection(tabName, sectionName, visible) {
2 Xrm.Page.ui.tabs.get(tabName).sections.get(sectionName).setVisible(visible);
3}
4
5HideShowSection("tab_5", "tab_5_section_4", true);

Save the form:

1function SaveAndClose() {
2 Xrm.Page.data.entity.save();
3}

Save and close the form:

1function SaveAndClose() {
2 Xrm.Page.data.entity.save("saveandclose");
3}

Close the form:

Note: the user will be prompted for confirmation if unsaved changes exist

1function Close() {
2 Xrm.Page.ui.close();
3}

Determine the Form Type:

Note: Form type codes: Create (1), Update (2), Read Only (3), Disabled (4), Bulk Edit (6)

1function AlertFormType() {
2 var FormType = Xrm.Page.ui.getFormType();
3 if (FormType != null) {
4 alert(FormType);
5 }
6}

Determine the GUID of the current record:

1function AlertGUID() {
2 var GUIDvalue = Xrm.Page.data.entity.getId();
3 if (GUIDvalue != null) {
4 alert(GUIDvalue);
5 }
6}

Determine the GUID of the current user:

1function AlertGUIDofCurrentUser() {
2 var UserGUID = Xrm.Page.context.getUserId();
3 if (UserGUID != null) {
4 alert(UserGUID);
5 }
6}


Regards,
Imran
[MVP CRM] = https://mvp.support.microsoft.com/profile/imran.mustafa

MSN/IM= mscrmexpert@gmail.com
SKYPE= mscrmexpert
BLOG= http://microsoftcrm3.blogspot.com
Linkedin = http://www.linkedin.com/in/mscrmexpert
Twitter = @mscrmexpert