Here is a simple but hopefully useful javascript function …..
The syntax differs slightly on code needed to grab the text value of a field depending on its type. Here is a little function I have often used to make your code easier to read and removes the need to worry about the type of fields.
I tend to have a number of functions of this type included in a javascript file called something like “new_common.js”, which I add to all of the forms I want to use javascript on.
Then to find the text value of a field I will simply include code such as this ….
accountName = GetText("name"); primaryContact = GetText("primarycontactid");
// *** Gets the text for any control *** function GetText(fieldName) { var control = Xrm.Page.ui.controls.get(fieldName); var value; if (control != undefined && control != null) { var type = Xrm.Page.data.entity.attributes.get(fieldName).getAttributeType(); switch (type) { case "optionset": value = control.getAttribute(); if (value != null) { return value.getText(); } else { return "" }; break; case "lookup": value = control.getAttribute().getValue(); if (value != null) { return value[0].name; } else { return "" }; break; default: value = control.getAttribute().getValue(); if (value != null) { return value; } else { return "" }; } } else { // **** Invalid control Name.. alert("Nothing found with name - " + fieldName); } };