chhaya_patel chhaya_patel - 1 month ago 13
Javascript Question

Get attribute names are in the main form of entity in dynamics CRM using javascript

I want to fetch logical name of attributes are in main form of account entity from systemform entity using javascript.And also Want to save that names in array or list.

Please Suggest me proper query and how to get name of each attribute from response.

Code:

var serverUrl;
serverUrl = location.protocol + "//" + location.host;
function getformfields() {
debugger;
var oDataUri = "systemforms?$select=formxml&$filter=name eq 'account'";
var data = getODataRecords(oDataUri);
if(data!=null&&data!="")
{
//how to get field name from response
}

}
function getODataRecords(ODataUrl) {
debugger;
var data;
var Query = ODataUrl;
var req = new XMLHttpRequest();

req.open("GET", serverUrl + "/api/data/v8.0/" + Query, false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 200) {
data = JSON.parse(this.response);
}
else {
var error = JSON.parse(this.response).error;
alert(error.message);
}
}
};
req.send();
return data;

}

Answer

It is just a matter of parsing the result formxml property.

The only thing to be aware of, is if an attribute is added multiple times, the control's id is appended with a numeric suffix e.g. name1, name2 etc. The datafieldname attribute points to the attribute the control is referring to.

var data = getODataRecords(oDataUri);
if (data && data.value && data.value[0] && data.value[0].formxml) {
 var xml = jQuery(data.value[0].formxml);
 var controls = xml.find('control');
 jQuery.each(controls, function(index, control) {
     console.log(jQuery(control).attr('datafieldname'));
 });
}