Get the GUID value of a lookup field:
Note: this example reads and pops the GUID of the primary contact on the Account form
2 | var primaryContactGUID = Xrm.Page.data.entity.attributes.get( "primarycontactid" ).getValue()[0].id; |
3 | alert(primaryContactGUID); |
Get the Text value of a lookup field:
Note: this example reads and pops the name of the primary contact on the Account form
2 | var primaryContactName = Xrm.Page.data.entity.attributes.get( "primarycontactid" ).getValue()[0].name; |
3 | alert(primaryContactName); |
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
1 | function AlertTextField() { |
2 | var MainPhone = Xrm.Page.data.entity.attributes.get( "telephone1" ).getValue(); |
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
1 | function AlertOptionSetDatabaseValue() { |
2 | var AddressType = Xrm.Page.data.entity.attributes.get( "address1_addresstypecode" ); |
3 | AddressTypeDisplayValue = AddressType.getValue(); |
4 | if (AddressTypeDisplayValue != null ) { |
5 | alert(AddressTypeDisplayValue); |
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
1 | function AlertOptionSetDisplayValue() { |
2 | var AddressType = Xrm.Page.data.entity.attributes.get( "address1_addresstypecode" ); |
3 | AddressTypeDisplayValue = AddressType.getText(); |
4 | if (AddressTypeDisplayValue != null ) { |
5 | alert(AddressTypeDisplayValue); |
Set the value of a string field:
Note: this example sets the Account Name field on the Account Form to “ABC”
1 | function SetStringField() { |
2 | var Name = Xrm.Page.data.entity.attributes.get( "name" ); |
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”
1 | function SetOptionSetField() { |
2 | var AddressType = Xrm.Page.data.entity.attributes.get( "address1_addresstypecode" ); |
3 | AddressType.setValue(1); |
Set the value of a Date field:
Note: this example sets the Birthday field on the Contact form to 1 day from today
1 | function 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); |
Set the value of a Lookup field:
Note: here I am providing a reusable function…
02 | function 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); |
Regards,