1
votes

How to get the schema name of the particular field in a record in CRM 2011 Javascript...?

1
depends on where you get the record from. Are you iterating over controls on a form in javascript? Or did you hit the OData endpoints? Other? - BenPatterson1
I needed schema names of the fields present in that entity OnSave of record. I'm iterating using Xrm.Page.data.entity.attributes.forEach() method.. - Charan Raju C R

1 Answers

5
votes

The name for a field should be the same as the "id" property.

If you happen to be working from a field's event, you can always pass the execution context when defining the function and then in your event code use:

executionContext.getEventSource().getName();

http://msdn.microsoft.com/en-us/library/gg334332.aspx

If you need the schema name (mixed case) based on the field id/name (lower case) you can use something like this (based on http://crmxpg.nl/wp/2010/10/19/how-to-query-the-metadata-service-via-javascript)

function GetSchemaName() {

    alert(gGetAttributeList(Xrm.Page.data.entity.getEntityName(), "thefieldname"));

}

//*********************************************************
gQueryMetadataService = function (request) {

    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    xmlhttp.open("POST", '/mscrmservices/2007/MetadataService.asmx', false);
    xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlhttp.setRequestHeader("SOAPAction", 'http://schemas.microsoft.com/crm/2007/WebServices/Execute');
    var soapMessage = "<?xml version='1.0' encoding='utf-8'?>" + 
                      "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " + 
                      "xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" + 
                      "<soap:Header>" + 
                      "<CrmAuthenticationToken xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" + 
                      "<AuthenticationType xmlns='http://schemas.microsoft.com/crm/2007/CoreTypes'>" + AUTHENTICATION_TYPE + 
                      "</AuthenticationType>" + 
                      "<OrganizationName xmlns='http://schemas.microsoft.com/crm/2007/CoreTypes'>" + ORG_UNIQUE_NAME + 
                      "</OrganizationName>" + 
                      "<CallerId xmlns='http://schemas.microsoft.com/crm/2007/CoreTypes'>00000000-0000-0000-0000-000000000000</CallerId>" + 
                      "</CrmAuthenticationToken>" + 
                      "</soap:Header>" + 
                      "<soap:Body><Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" + request + 
                      "</Execute></soap:Body>" + 
                      "</soap:Envelope>";
    xmlhttp.send(soapMessage);
    return xmlhttp.responseXML;
}

gGetAttributeList = function (entityName, fieldname) {

    var request = "<Request xsi:type='RetrieveEntityRequest'>" + 
                  "<MetadataId>00000000-0000-0000-0000-000000000000</MetadataId>" + 
                  "<EntityItems>IncludeAttributes</EntityItems>" + 
                  "<LogicalName>" + entityName + "</LogicalName>" + 
                  "<IsCustomizable>1</IsCustomizable>" + 
                  "<RetrieveAsIfPublished>true</RetrieveAsIfPublished>" + 
                  "</Request>";

    var result = gQueryMetadataService(request);
    var schemaNames = result.selectNodes("//EntityMetadata/Attributes/Attribute/SchemaName");
    for (var i = 0; i < schemaNames.length; i++) {
        if (fieldname === schemaNames[i].text.toLowerCase()) {
            return schemaNames[i].text;
        }
    }
    return null;
}