UNPKG

dynamicsnode

Version:

Create simple scripts to interact with Dynamics CRM using Node.js

66 lines 3.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var CRMDataTypes_1 = require("./CRMDataTypes"); var Messages_1 = require("./Messages"); var MetadataUtil = (function () { function MetadataUtil() { } MetadataUtil.getEntityMetadataFromCrm = function (crm, entityName) { var request = new Messages_1.RetrieveEntityRequest(entityName, CRMDataTypes_1.EntityFilters.All); var response = crm.Execute(request); return response.EntityMetadata; }; MetadataUtil.getEntityMetadata = function (crm, entityName) { if (this._metadataCache[entityName] === undefined) { var metadata = this.getEntityMetadataFromCrm(crm, entityName); this._metadataCache[entityName] = metadata; } return this._metadataCache[entityName]; }; MetadataUtil.getAttributeMetadata = function (crm, entityName, attributeName) { var attributeMetadata = null; var entityMetadata = this.getEntityMetadata(crm, entityName); if (entityMetadata && entityMetadata.Attributes && entityMetadata.Attributes.length > 0) { for (var i = 0; i < entityMetadata.Attributes.length; i++) { if (entityMetadata.Attributes[i].LogicalName == attributeName) { attributeMetadata = entityMetadata.Attributes[i]; break; } } // In case there isn't an attribute with the specified name, then try to find it by displayname if (attributeMetadata === null) { for (var i = 0; i < entityMetadata.Attributes.length; i++) { if (entityMetadata.Attributes[i].DisplayName != null && entityMetadata.Attributes[i].DisplayName.UserLocalizedLabel != null && entityMetadata.Attributes[i].DisplayName.UserLocalizedLabel.Label != null && entityMetadata.Attributes[i].DisplayName.UserLocalizedLabel.Label.toLowerCase() == attributeName.toLowerCase()) { attributeMetadata = entityMetadata.Attributes[i]; break; } } } } return attributeMetadata; }; /** Gets the optionset number value from its label value. If not found returns null */ MetadataUtil.getOptionsetValue = function (crm, entityName, attributeName, optionsetText) { var optionsetValue = null; var attribute = this.getAttributeMetadata(crm, entityName, attributeName); if (attribute === null) throw new Error("Attribute " + attributeName + " not found in entity '" + entityName + "'"); var options = attribute.OptionSet.Options; for (var i = 0; i < options.length; i++) { var option = options[i]; if (option && option.Label && option.Label.UserLocalizedLabel && option.Label.UserLocalizedLabel.Label && option.Label.UserLocalizedLabel.Label === optionsetText) { optionsetValue = option.Value; break; } } return optionsetValue; }; return MetadataUtil; }()); MetadataUtil._metadataCache = {}; exports.MetadataUtil = MetadataUtil; //# sourceMappingURL=MetadataUtil.js.map