public Guid GetRecordUniqueId(EntityReference record, bool validate = true) { if (string.IsNullOrWhiteSpace(record.LogicalName)) { throw new InvalidOperationException("The entity logical name must not be null or empty."); } // Don't fail with invalid operation exception, if no record of this entity exists, but entity is known if (!Data.ContainsKey(record.LogicalName) && !EntityMetadata.ContainsKey(record.LogicalName)) { if (ProxyTypesAssembly == null) { throw new InvalidOperationException($"The entity logical name {record.LogicalName} is not valid."); } if (!ProxyTypesAssembly.GetTypes().Any(type => FindReflectedType(record.LogicalName) != null)) { throw new InvalidOperationException($"The entity logical name {record.LogicalName} is not valid."); } } #if !FAKE_XRM_EASY && !FAKE_XRM_EASY_2013 && !FAKE_XRM_EASY_2015 if (record.Id == Guid.Empty && record.HasKeyAttributes()) { if (EntityMetadata.ContainsKey(record.LogicalName)) { var entityMetadata = EntityMetadata[record.LogicalName]; foreach (var key in entityMetadata.Keys) { if (record.KeyAttributes.Keys.Count == key.KeyAttributes.Length && key.KeyAttributes.All(x => record.KeyAttributes.Keys.Contains(x))) { if (Data.ContainsKey(record.LogicalName)) { var matchedRecord = Data[record.LogicalName].Values.SingleOrDefault(x => record.KeyAttributes.All(k => x.Attributes.ContainsKey(k.Key) && x.Attributes[k.Key] != null && x.Attributes[k.Key].Equals(k.Value))); if (matchedRecord != null) { return(matchedRecord.Id); } } if (validate) { new FaultException <OrganizationServiceFault>(new OrganizationServiceFault(), $"{record.LogicalName} with the specified Alternate Keys Does Not Exist"); } } } } if (validate) { throw new InvalidOperationException($"The requested key attributes do not exist for the entity {record.LogicalName}"); } } #endif /* * if (validate && record.Id == Guid.Empty) * { * throw new InvalidOperationException("The id must not be empty."); * } */ return(record.Id); }