Cry about...
Dynamics CRM 2011 How To


How to get type of customer (JavaScript)


The customerid field can refer to an account or a contact or neither, but how to know which?

var customer = Xrm.Page.getAttribute("customerid").getValue();

testing whether the customer refers to anything is easy:

if (customer != null) {
    . . .
}

You can see whether the customer is an account or a contact by looking at the entityType field:

if (customer[0].entityType == "account") {
    // It is an account.
} else if (customer[0].entityType == "contact") {
    // It is a contact.
}

Obviously the JavaScript above didn't need the "else if" because the entityType can be only "account" or "contact" so an "else" would have sufficed, but I wanted to show the two values explicitly.


These notes have been tested with Microsoft Dynamics CRM 2011, and may apply to other versions as well.



About the author: is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.