Sunday, January 17, 2010

Some Javascript Functions in CRM 2011

Javascript Functions in CRM 2011

Most of the examples are provided as functions so that you can easily test them out in CRM.

Get the GUID value of a lookup field:

Note: this example reads and pops the GUID of the primary contact on the Account form

1function AlertGUID() {
2 var primaryContactGUID = Xrm.Page.data.entity.attributes.get("primarycontactid").getValue()[0].id;
3 alert(primaryContactGUID);
4}

Get the Text value of a lookup field:

Note: this example reads and pops the name of the primary contact on the Account form

1function AlertText() {
2 var primaryContactName = Xrm.Page.data.entity.attributes.get("primarycontactid").getValue()[0].name;
3 alert(primaryContactName);
4}

Get the string value of a text field:

Note: this example reads and pops the value of the Main Phone (telephone1) field on the Account form

1function AlertTextField() {
2 var MainPhone = Xrm.Page.data.entity.attributes.get("telephone1").getValue();
3 alert(MainPhone);
4}

Get the database value (i.e. the integer value) of an Option Set (pick list) field:

Note: this example reads and pops the value of the Address Type (address1_addresstypecode) field on the Account form

1function AlertOptionSetDatabaseValue() {
2 var AddressType = Xrm.Page.data.entity.attributes.get("address1_addresstypecode");
3 AddressTypeDisplayValue = AddressType.getValue();
4 if (AddressTypeDisplayValue != null) {
5 alert(AddressTypeDisplayValue);
6 }
7}

Get the display value (i.e. the text displayed in the drop down) of an Option Set (pick list) field:

Note: this example reads and pops the value of the Address Type (address1_addresstypecode) field on the Account form

1function AlertOptionSetDisplayValue() {
2 var AddressType = Xrm.Page.data.entity.attributes.get("address1_addresstypecode");
3 AddressTypeDisplayValue = AddressType.getText();
4 if (AddressTypeDisplayValue != null) {
5 alert(AddressTypeDisplayValue);
6 }
7}

Set the value of a string field:

Note: this example sets the Account Name field on the Account Form to “ABC”

1function SetStringField() {
2 var Name = Xrm.Page.data.entity.attributes.get("name");
3 Name.setValue("ABC");
4}

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”

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

Set the value of a Date field:

Note: this example sets the Birthday field on the Contact form to 1 day from today

1function SetDateField() {
2 var BirthDate = Xrm.Page.data.entity.attributes.get("birthdate");
3 var today = new Date();
4 var futureDate = new Date(today.setDate(today.getDate() + 1));
5 BirthDate.setValue(futureDate);
6}

Set the value of a Lookup field:

Note: here I am providing a reusable function…

01// Set the value of a lookup field
02function SetLookupValue(fieldName, id, name, entityType) {
03 if (fieldName != null) {
04 var lookupValue = new Array();
05 lookupValue[0] = new Object();
06 lookupValue[0].id = id;
07 lookupValue[0].name = name;
08 lookupValue[0].entityType = entityType;
09 Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
10 }
11}


Regards,

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