Most common tasks javascript in CRM 2011
- Lookups
- Text fields
- Option Set
- Bits
- Date fields
- Frecuent form tasks
- Get Context data
- Call form events
- Pop records
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
2
3
4
5
6
7
8
|
//
Set the value of a lookup field
function SetLookupValue(fieldName,
id, name, entityType) {
if (fieldName != null) {
var lookupValue = new Array();
lookupValue[0]
= new LookupControlItem(id, entityType,
name);
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
2
|
var Name =
Xrm.Page.data.entity.attributes.get("name");
Name.setValue("mytext");
|
Split Full Name
into First and Last Names
|
1
2
3
4
5
6
|
function PopulateNameFields() {
var ContactName =
Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].name;
var mySplitResult =
ContactName.split(" ");
Xrm.Page.data.entity.attributes.get("firstname").setValue(mySplitResult[0]);
Xrm.Page.data.entity.attributes.get("lastname").setValue(mySplitResult[1]);
}
|
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);
|
Remove value
from Optionset
|
1
|
Xrm.Page.getControl("fieldName").removeOption(Option_Value);
|
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
|
1
2
3
4
5
6
7
8
9
10
11
|
function FormatDate(fieldname)
{
var d =
Xrm.Page.data.entity.attributes.get(fieldname).getValue();
if (d != null) {
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
// getMonth() considers Jan month 0, need to add 1
var curr_year =
d.getFullYear();
return curr_date + "-"
+ curr_month + "-" + curr_year;
}
else return null;
}
|
Set date into
Date field
|
1
2
3
4
5
6
7
8
|
//Today's
date
Xrm.Page.data.entity.attributes.get("new_date1").setValue(new Date());
//Next
week
Xrm.Page.data.entity.attributes.get("new_date2").setValue(new Date(today.setDate(today.getDate()
+ 7)));
//Set
the Time portion of a Date Field
Xrm.Page.getAttribute(attributeName).setValue(new Date().setHours(8, 30, 0)) //today
at 8:30
|
Change
Requirement Level to fields
|
1
2
|
Xrm.Page.data.entity.attributes.get("address1_addresstypecode").setRequiredLevel("required");
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
2
|
//
Save the Disabled Field
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
2
3
4
5
|
var ExistingCustomerBit =
Xrm.Page.data.entity.attributes.get("new_existingcustomer").getValue();
if (ExistingCustomerBit == false)
Xrm.Page.ui.controls.get("customerid").setVisible(false);
else
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
2
3
4
5
6
7
8
|
var attributes =
Xrm.Page.data.entity.attributes.get()
for (var i in attributes)
{
var attribute = attributes[i];
if (attribute.getIsDirty())
alert("attribute
dirty: " + attribute.getName());
}
|
Refresh a
Sub-Grid
|
1
|
Xrm.Page.ui.controls.get("target_grid").refresh();
|
Add OnChange to
field
|
1
|
Xrm.Page.data.entity.attributes.get("target_field").addOnChange(MyFunction);
|
Fire OnChange on
field
|
1
|
Xrm.Page.getAttribute("target_field").fireOnChange();
|
Remove OnChange
to field
|
1
|
Xrm.Page.getAttribute("target_field").removeOnChange(MyFunction);
|
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
2
3
|
document.getElementById("customerid").setAttribute("defaulttype", "2");
var ViewGUID= "A2D479C5-53E3-4C69-ADDD-802327E67A0D";
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
|
1
|
Xrm.Page.ui.close();
|
Pop existing
lookup window from field
|
1
|
window.document.getElementById('new_existingcase').click();
|
Pop an existing
CRM record
|
1
2
3
4
5
6
7
8
9
10
11
12
|
//Set
features for how the window will appear
var features = "location=no,menubar=no,status=no,toolbar=no";
//
Get the CRM URL
var serverUrl =
Xrm.Page.context.getServerUrl();
//
Cater for URL differences between on premise and online
if (serverUrl.match(/\/$/)) {
serverUrl
= serverUrl.substring(0, serverUrl.length - 1);
}
window.open(serverUrl + "/main.aspx?etn=incident&pagetype=entityrecord&id="
+ encodeURIComponent(IncidentId), "_blank", features, false);
|
Pop the Create
form of a CRM record type
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//Collect
values from the existing CRM form that you want to default onto the new
record
var parententity =
Xrm.Page.getAttribute("from").getValue();
//Set
the parameter values
var extraqs = "&title=New
Case";
extraqs += "&customerid="
+ parententity.id;
extraqs += "&customeridname="
+ parententity.name;
//if
you need to send parameters from personal entities this parameter send it
without type
extraqs += "&customeridtype=contact";
//Set
features for how the window will appear
var features = "location=no,menubar=no,status=no,toolbar=no";
//
Get the CRM URL
var serverUrl =
Xrm.Page.context.getServerUrl();
//
Cater for URL differences between on premise and online
if (serverUrl.match(/\/$/))
serverUrl
= serverUrl.substring(0, serverUrl.length - 1);
//Pop
the window
window.open(serverUrl + "/main.aspx?etn=incident&pagetype=entityrecord&extraqs="
+ encodeURIComponent(extraqs), "_blank", features, false);
|
Pop Dialog from
a ribbon button
|
1
2
3
4
5
6
7
8
9
10
11
|
var DialogGUID = "128CEEDC-2763-4FA9-AB89-35BBB7D5517D";
serverUrl = serverUrl + "cs/dialog/rundialog.aspx?DialogId="
+ "{" + DialogGUID + "}" + "&EntityName=lead&ObjectId="
+ sLeadID;
PopupCenter(serverUrl, "mywindow", 400, 400);
window.location.reload(true);
function PopupCenter(pageURL,
title, w, h) {
var left = (screen.width / 2) -
(w / 2);
var top = (screen.height / 2) -
(h / 2);
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);
}
|
No comments:
Post a Comment