Skip to content

Get All fields on a Form using JavaScript – Microsoft Dynamics CRM

We can access all fields on a Microsoft dynamics form using JavaScript easily. Dynamics provide client side SDK and using this SDK which can be accessed using Xrm reference in JavaScript.

You can write below codes in developer console of any browser and test it instantly.

Below code snippet lists all the fields present on the current displayed form:

Xrm.Page.data.entity.attributes.forEach((attribute, index) => console.log(attribute.getName()))

There are many other methods provided by Xrm SDK, that can give you additional information about these fields, like if they are required or not, what is there submit mode, etc.

Xrm.Page.data.entity.attributes.forEach((attribute, index) => {console.log("Name: "attribute.getName() + " , Required Level: " +attribute.getRequiredLevel())})

For example, Below code shows all the fields that are mandatory:

Xrm.Page.data.entity.attributes.forEach((attribute, index) => {if(attribute.getRequiredLevel() == "required"){console.log(attribute.getName())}})

Same way, at any point we can get what fields are changed of a form, by checking if field is dirty:

Xrm.Page.data.entity.attributes.forEach((attribute, index) => {if(attribute.getIsDirty()){console.log(attribute.getName())}})

As we are using data.entity.attributes and it gives lots of helpful methods, but many times we don’t get all information from it, so say for example, what if we wanted to know if a field is readonly on not on a form. At that time we can use Xrm.Page.getControls.

Below code lists all controls along with their read-only status:

Xrm.Page.data.entity.attributes.forEach((attribute, index) => {
    if (Xrm.Page.getControl(attribute.getName())
        && Xrm.Page.getControl(attribute.getName()).getControlType() !== "webresource") {
        console.log("Name: " + attribute.getName() + " ,is read only: " + Xrm.Page.getControl(attribute.getName()).getDisabled())
    }
})

Happy coding!

One Comment

  1. Karman Karman

    This was really smart.
    Thank you so much, saved me a lot of manual work

Leave a Reply

Your email address will not be published.