1. Get the GUID value of a lookup field:
Note: this example
reads and pops the GUID of the primary contact on the Account form
function
AlertGUID() {
var
primaryContactGUID =
Xrm.Page.data.entity.attributes.get("primarycontactid").getValue()[0].id;
alert(primaryContactGUID);
}
2. Get
the Text value of a lookup field:
Note: this example
reads and pops the name of the primary contact on the Account form
function
AlertText() {
var
primaryContactName =
Xrm.Page.data.entity.attributes.get("primarycontactid").getValue()[0].name;
alert(primaryContactName);
}
3. Get
the value of a text field:
Note: this example
reads and pops the value of the Main Phone (telephone1) field on the Account
form
function
AlertTextField() {
var MainPhone =
Xrm.Page.data.entity.attributes.get("telephone1").getValue();
alert(MainPhone);
}
4. Get
the database value of an Option Set field:
Note: this example
reads and pops the value of the Address Type (address1_addresstypecode) field
on the Account form
function
AlertOptionSetDatabaseValue() {
var AddressType =
Xrm.Page.data.entity.attributes.get("address1_addresstypecode");
AddressTypeDisplayValue
= AddressType.getValue();
if
(AddressTypeDisplayValue != null) {
alert(AddressTypeDisplayValue);
}
}
5. Get
the text value of an Option Set field:
Note: this example
reads and pops the value of the Address Type (address1_addresstypecode) field
on the Account form
function
AlertOptionSetDisplayValue() {
var AddressType =
Xrm.Page.data.entity.attributes.get("address1_addresstypecode");
AddressTypeDisplayValue
= AddressType.getSelectedOption().text;
if
(AddressTypeDisplayValue != null) {
alert(AddressTypeDisplayValue);
}
}
6. Get
the database value of a Bit field:
// example
GetBitValue("telephone1");
function
GetBitValue(fieldname) {
return
Xrm.Page.data.entity.attributes.get(fieldname).getValue();
}
7. Get
the value of a Date field:
returns a value
like: Wed Nov 30 17:04:06 UTC+0800 2011
and reflects the
users time zone set under personal options
// example
GetDate("createdon");
function
GetDate(fieldname) {
return
Xrm.Page.data.entity.attributes.get(fieldname).getValue();
}
8. Get
the day, month and year parts from a Date field:
// This function
takes the fieldname of a date field as input and returns a DD-MM-YYYY value
// Note: the day,
month and year variables are numbers
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;
}
// An example where
the above function is called
alert(FormatDate("new_date2"));
9. Set
the value of a string field:
Note: this example
sets the Account Name field on the Account Form to “ABC”
function
SetStringField() {
var Name =
Xrm.Page.data.entity.attributes.get("name");
Name.setValue("ABC");
}
10. Set
the value of an Option Set (pick list) field:
Note: this example
sets the Address Type field on the Account Form to “Bill To”, which corresponds
to a database value of “1”
function
SetOptionSetField() {
var AddressType =
Xrm.Page.data.entity.attributes.get("address1_addresstypecode");
AddressType.setValue(1);
}
11. Set
a Date field / Default a Date field to Today:
//set date field to
now (works on date and date time fields)
Xrm.Page.data.entity.attributes.get("new_date1").setValue(new
Date());
12. Set
a Date field to 7 days from now:
function
SetDateField() {
var today = new
Date();
var futureDate =
new Date(today.setDate(today.getDate() + 7));
Xrm.Page.data.entity.attributes.get("new_date2").setValue(futureDate);
Xrm.Page.data.entity.attributes.get("new_date2").setSubmitMode("always");//
Save the Disabled Field
}
13. Set
the Time portion of a Date Field:
// This is a
function you can call to set the time portion of a date field
function
SetTime(attributeName, hour, minute) {
var attribute =
Xrm.Page.getAttribute(attributeName);
if
(attribute.getValue() == null) {
attribute.setValue(new
Date());
}
attribute.setValue(attribute.getValue().setHours(hour,
minute, 0));
}
09
// Here's an
example where I use the function to default the time to 8:30am
SetTime('new_date2',
8, 30);
14. Set
the value of a Lookup field:
Note: here I am
providing a reusable function…
// Set the value of
a lookup field
function
SetLookupValue(fieldName, id, name, entityType) {
if (fieldName !=
null) {
var lookupValue =
new Array();
lookupValue[0] =
new Object();
lookupValue[0].id =
id;
lookupValue[0].name
= name;
lookupValue[0].entityType
= entityType;
Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
}
}
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):
var ExistingCase = Xrm.Page.data.entity.attributes.get("new_existingcase");
if (ExistingCase.getValue() != null)
{
var ExistingCaseGUID =
ExistingCase.getValue()[0].id;
var ExistingCaseName =
ExistingCase.getValue()[0].name;
SetLookupValue("regardingobjectid",
ExistingCaseGUID, ExistingCaseName,"incident");
}
15. Split
a Full Name into First Name and Last Name fields:
function
PopulateNameFields() {
var ContactName =
Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].name;
var mySplitResult =
ContactName.split(" ");
var fName =
mySplitResult[0];
var lName =
mySplitResult[1];
Xrm.Page.data.entity.attributes.get("firstname").setValue(fName);
Xrm.Page.data.entity.attributes.get("lastname").setValue(lName);
}
16. Refresh subgrid using Javascript:
Xrm.Page.ui.controls.get("SubGridName").refresh();
No comments:
Post a Comment