Most common tasks javascript in CRM 2011
Index:
Get GUID value from lookup
1
| var primaryContactGUID = Xrm.Page.data.entity.attributes.get("primarycontactid").getValue()[0].id;
|
Get Text value from lookup field
1
| var primaryContactName = Xrm.Page.data.entity.attributes.get("primarycontactid").getValue()[0].name;
|
Set value in Lookup field
1
| // Set the value of a lookup field
|
2
| function SetLookupValue(fieldName, id, name, entityType) {
|
3
| if (fieldName != null) {
|
4
| var lookupValue = new Array();
|
5
| lookupValue[0] = new LookupControlItem(id, entityType, name);
|
6
| Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
|
Get value from text field
1
| var MainPhone = Xrm.Page.data.entity.attributes.get("telephone1").getValue();
|
Set value in text field
1
| var Name = Xrm.Page.data.entity.attributes.get("name");
|
2
| Name.setValue("mytext");
|
Split Full Name into First and Last Names
1
| function PopulateNameFields() {
|
2
| var ContactName = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].name;
|
3
| var mySplitResult = ContactName.split(" ");
|
4
| Xrm.Page.data.entity.attributes.get("firstname").setValue(mySplitResult[0]);
|
5
| Xrm.Page.data.entity.attributes.get("lastname").setValue(mySplitResult[1]);
|
6
| }
|
Get the numeric value from Option Set
1
| var AddressType = Xrm.Page.data.entity.attributes.get("address1_addresstypecode").getValue();
|
Get the text value from Option Set field
1
| var AddressType = Xrm.Page.data.entity.attributes.get("address1_addresstypecode").getSelectedOption().text;
|
Set value in Option Set
1
| var AddressType = Xrm.Page.data.entity.attributes.get("address1_addresstypecode").setValue(1);
|
Get value from Bit field
1
| Xrm.Page.data.entity.attributes.get(fieldname).getValue();
|
Get value fro Date field
1
| return Xrm.Page.data.entity.attributes.get("createdon").getValue();
|
Get the day, month and year parts from a Date field
01
| function FormatDate(fieldname) {
|
02
| var d = Xrm.Page.data.entity.attributes.get(fieldname).getValue();
|
03
| if (d != null) {
|
04
| var curr_date = d.getDate();
|
05
| var curr_month = d.getMonth();
|
06
| curr_month++; // getMonth() considers Jan month 0, need to add 1
|
07
| var curr_year = d.getFullYear();
|
08
| return curr_date + "-" + curr_month + "-" + curr_year;
|
Set date into Date field
1
| //Today's date
|
2
| Xrm.Page.data.entity.attributes.get("new_date1").setValue(new Date());
|
5
| Xrm.Page.data.entity.attributes.get("new_date2").setValue(new Date(today.setDate(today.getDate() + 7)));
|
6
| |
7
| //Set the Time portion of a Date Field
|
8
| Xrm.Page.getAttribute(attributeName).setValue(new Date().setHours(8, 30, 0)) //today at 8:30
|
Change Requirement Level to fields
1
| Xrm.Page.data.entity.attributes.get("address1_addresstypecode").setRequiredLevel("required");
|
2
| Xrm.Page.data.entity.attributes.get("address1_addresstypecode").setRequiredLevel("none");
|
Disable a field
1
| Xrm.Page.ui.controls.get("address1_addresstypecode").setDisabled(true);
|
Force Submit the Save of a Disabled Field
1
| // Save the Disabled Field
|
2
| Xrm.Page.data.entity.attributes.get("new_date1").setSubmitMode("always");
|
Show/Hide a field
1
| Xrm.Page.ui.controls.get("name").setVisible(false);
|
Show/Hide a field based on a Bit field
1
| var ExistingCustomerBit = Xrm.Page.data.entity.attributes.get("new_existingcustomer").getValue();
|
2
| if (ExistingCustomerBit == false)
|
3
| Xrm.Page.ui.controls.get("customerid").setVisible(false);
|
4
| else
|
5
| Xrm.Page.ui.controls.get("customerid").setVisible(true);
|
Show/Hide a nav item
1
| Xrm.Page.ui.navigation.items.get("navContacts").setVisible(false);
|
Show/Hide a Section
1
| Xrm.Page.ui.tabs.get("tab0").sections.get("general").setVisible(false);
|
Show/Hide a Tab
1
| Xrm.Page.ui.tabs.get("tab0").setVisible(visible);
|
Determine which fields on the form are dirty
1
| var attributes = Xrm.Page.data.entity.attributes.get()
|
2
| for (var i in attributes)
|
3
| {
|
4
| var attribute = attributes[i];
|
5
| if (attribute.getIsDirty())
|
6
| alert("attribute dirty: " + attribute.getName());
|
Refresh a Sub-Grid
1
| Xrm.Page.ui.controls.get("target_grid").refresh();
|
Change the default entity in the lookup window of a Customer or Regarding field
Note: I am setting the customerid field’s lookup window to offer Contacts (entityid 2) by default (rather than Accounts). I have also hardcoded the GUID of the default view I wish displayed in the lookup window.
1
| document.getElementById("customerid").setAttribute("defaulttype", "2");
|
2
| var ViewGUID= "A2D479C5-53E3-4C69-ADDD-802327E67A0D";
|
3
| Xrm.Page.getControl("customerid").setDefaultView(ViewGUID);
|
Determine the Form Type
Note: Form type codes: Create (1), Update (2), Read Only (3), Disabled (4), Bulk Edit (6)
1
| var FormType = Xrm.Page.ui.getFormType();
|
Get the GUID of the current record
1
| var GUIDvalue = Xrm.Page.data.entity.getId();
|
Get the GUID of the current user
1
| var UserGUID = Xrm.Page.context.getUserId();
|
Get Security Roles from the current user
1
| Xrm.Page.context.getUserRoles()
|
Determine the CRM server URL
1
| var serverUrl = Xrm.Page.context.getServerUrl();
|
Save the form
1
| Xrm.Page.data.entity.save();
|
Save and close the form
1
| Xrm.Page.data.entity.save("saveandclose");
|
Close the form
Note: the user will be prompted for confirmation if unsaved changes exist
Pop existing lookup window from field
1
| window.document.getElementById('new_existingcase').click();
|
Pop an existing CRM record
01
| //Set features for how the window will appear
|
02
| var features = "location=no,menubar=no,status=no,toolbar=no";
|
05
| var serverUrl = Xrm.Page.context.getServerUrl();
|
06
| |
07
| // Cater for URL differences between on premise and online
|
08
| if (serverUrl.match(/\/$/)) {
|
09
| serverUrl = serverUrl.substring(0, serverUrl.length - 1);
|
10
| }
|
11
| |
12
| window.open(serverUrl + "/main.aspx?etn=incident&pagetype=entityrecord&id=" + encodeURIComponent(IncidentId), "_blank", features, false);
|
Pop the Create form of a CRM record type
01
| //Collect values from the existing CRM form that you want to default onto the new record
|
02
| var parententity = Xrm.Page.getAttribute("from").getValue();
|
03
| |
04
| //Set the parameter values
|
05
| var extraqs = "&title=New Case";
|
06
| extraqs += "&customerid=" + parententity.id;
|
07
| extraqs += "&customeridname=" + parententity.name;
|
08
| //if you need to send parameters from personal entities this parameter send it without type
|
09
| extraqs += "&customeridtype=contact";
|
10
| |
11
| //Set features for how the window will appear
|
12
| var features = "location=no,menubar=no,status=no,toolbar=no";
|
15
| var serverUrl = Xrm.Page.context.getServerUrl();
|
16
| |
17
| // Cater for URL differences between on premise and online
|
18
| if (serverUrl.match(/\/$/))
|
19
| serverUrl = serverUrl.substring(0, serverUrl.length - 1);
|
20
| |
21
| //Pop the window
|
22
| window.open(serverUrl + "/main.aspx?etn=incident&pagetype=entityrecord&extraqs=" + encodeURIComponent(extraqs), "_blank", features, false);
|
Pop Dialog from a ribbon button
03
| serverUrl = serverUrl + "cs/dialog/rundialog.aspx?DialogId=" + "{" + DialogGUID + "}" + "&EntityName=lead&ObjectId=" + sLeadID;
|
04
| PopupCenter(serverUrl, "mywindow", 400, 400);
|
05
| window.location.reload(true);
|
06
| |
07
| function PopupCenter(pageURL, title, w, h) {
|
08
| var left = (screen.width / 2) - (w / 2);
|
09
| var top = (screen.height / 2) - (h / 2);
|
10
| var targetWin = window.showModalDialog(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
|
Thanks & Regards,
K V SambasivaRao
No comments:
Post a Comment