2012-05-16 63 views
1

如何獲取CRM 2011中的記錄中特定字段的架構名稱Javascript ...?在CRM 2011中獲取架構名稱Javascript

+1

依賴你從哪裏得到記錄。你是否在迭代JavaScript中的窗體上的控件?或者你是否擊中了OData端點?其他? – BenPatterson1

+0

我需要記錄的實體OnSave中存在的字段的模式名稱。我正在迭代使用Xrm.Page.data.entity.attributes.forEach()方法.. –

回答

5

字段的名稱應該與「id」屬性相同。

如果你碰巧從現場的情況下工作,你總是可以定義函數時傳遞執行上下文,然後在你的事件代碼用途:

executionContext.getEventSource().getName(); 

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

如果您需要基於字段ID /名稱(小寫)模式名稱(混合大小寫),你可以使用這樣的事情(基於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; 
} 
+0

我正在使用OnSave事件。模式名稱不同於名稱權限..?說,new_name是字段的名稱,模式名稱就像new_Name ..是正確的..? –

+0

你是對的 - 我更新了我的答案以找到模式名稱。 –