我正在尝试将一个CRM的查找字段复制到另一个CRM。但这失败了,因为通常我会得到一个例外,说具有查找逻辑名的属性不存在。
好吧,我知道,因为我正在尝试创建它。发生这种情况时,关系的引用属性是查找,因此查找必须在引用的属性中。
但是我尝试搜索在引用属性中具有查找逻辑名称的关系,但没有找到,我尝试了两个实体的OneToMany和ManyToOne。因此,我需要帮助来解决此问题。有人有解决办法吗?
我不想使用解决方案来解决此问题,因为解决方案无法复制托管的查找字段。而且,我拥有要在另一个CRM中创建的所有属性和实体,我只需要创建查找和您的关系即可。
我有这个基本代码,但是需要2个CRM来对其进行测试。
public void CopyLookup() {
List<EntityMetadata> OriginEntities = new List<EntityMetadata>();
bool sucess = false;
while (!sucess) {
try {
RetrieveAllEntitiesRequest metaDataRequest = new RetrieveAllEntitiesRequest();
metaDataRequest.EntityFilters = EntityFilters.All;
// Execute the request.
RetrieveAllEntitiesResponse metaDataResponse = (RetrieveAllEntitiesResponse)Origin.IOrganizationService.Execute(metaDataRequest);
OriginEntities = new List<EntityMetadata>(metaDataResponse.EntityMetadata);
sucess = true;
return entitiesMetadata;
} catch (ThreadAbortException) {
} catch (Exception _ex) {
Console.WriteLine(String.Format("Fail to find Entities - {0}", _ex.Message));
if (_ex.Message.Contains("There was no endpoint"))
sucess = false;
else
throw new Exception(String.Format("Fail to find Entities - {0}", _ex.Message));
}
}
foreach (EntityMetadata ent in OriginEntities.Where(wh => wh.LogicalName.Contains('_'))) {
foreach (OneToManyRelationshipMetadata relation in ent.OneToManyRelationships) {
LookupAttributeMetadata lookup = (LookupAttributeMetadata)OriginEntities.Where(wh => relation.ReferencingEntity == wh.LogicalName).FirstOrDefault()
.Attributes.Where(wa => wa.AttributeType == AttributeTypeCode.Lookup && wa.LogicalName == relation.ReferencingAttribute).FirstOrDefault();
if (lookup == null)
continue;
CreateOneToManyRequest createOtm = new CreateOneToManyRequest() {
OneToManyRelationship = relation,
Lookup = lookup
};
bool sucess2 = false;
while (!sucess2) {
try {
Migration.IOrganizationService.Execute(createOtm);
} catch (EndpointNotFoundException) {
sucess2 = false;
} catch (TimeoutException) {
sucess2 = false;
} catch (FaultException ex) {
if (ex.Message.Contains("endpoint")) {
sucess2 = false;
} else if (ex.Message.Contains("there is already")) {
sucess2 = true;
} else {
sucess2 = true;
}
} catch (Exception ex) {
if (ex.Message.Contains("This could be due to the service endpoint binding")) {
sucess2 = false;
} else if (ex.Message.Contains("is not unique")) {
sucess2 = true;
} else {
sucess2 = true;
}
}
}
}
}
}
答案 0 :(得分:1)
我总是手动或通过解决方案创建关系,但我查看了您的代码并获得了一些可能会有所帮助的想法...
此代码反映了其中的一些想法,下面提供了更多信息。
var metaDataRequest = new RetrieveAllEntitiesRequest
{
EntityFilters = EntityFilters.All
};
var metaDataResponse = (RetrieveAllEntitiesResponse)svc.Execute(metaDataRequest);
var OriginEntities = metaDataResponse.EntityMetadata.ToList();
foreach (var entity in OriginEntities.Where(e=> e.IsCustomizable.Value))
{
foreach (var relationship in entity.OneToManyRelationships.Where(r => r.IsCustomRelationship == true))
{
var lookup = OriginEntities
.Where(e => e.LogicalName == relationship.ReferencingEntity)
.Single()
.Attributes
.Where(a => a.AttributeType == AttributeTypeCode.Lookup && a.LogicalName == relationship.ReferencingAttribute)
.Single();
var createOtm = new CreateOneToManyRequest()
{
OneToManyRelationship = relationship,
Lookup = lookup as LookupAttributeMetadata
};
}
}
Where
循环中修改了foreach
子句的原因。IsManaged
标志重置为false
,并将其IsCustomizable
的值重置为true。