以编程方式为客户/联系人实体上的自定义字段创建属性映射
本文关键字:自定义 字段 创建 映射 属性 实体 方式 编程 客户 联系人 | 更新日期: 2023-09-27 18:16:56
所以我有一个CRM集成,它向lead和contact添加了两个字段。一个整数和一个布尔值。当我们将引线转换为联系人时,我希望这些自定义字段从引线传递到新联系人。我们有超过700个实例使用我们的产品,所以这需要是一个程序化的解决方案。这就是问题所在。我还没能把我的头围绕createattributemapping类,并希望有人在这里可以启发我,告诉我我是愚蠢的…
现在我有这样的东西:
var parentEntityMapId = new v4.Sdk.Lookup();
parentEntityMapId.name = "lead";
parentEntityMapId.Value = Guid.NewGuid();
var entityMapId = new v4.Sdk.Lookup();
entityMapId.name = "contact";
//entityMapId.Value = new Guid("608861bc-50a4-4c5f-a02c-21fe1943e2cf");
entityMapId.Value = Guid.NewGuid();
var attributeMapId = new v4.Sdk.Key();
attributeMapId.Value = Guid.NewGuid();
var attributeMap = new v4.SdkTypeProxy.attributemap();
attributeMap.attributemapid = attributeMapId;
attributeMap.entitymapid = entityMapId;
attributeMap.sourceattributename = fieldNameFrom;
attributeMap.targetattributename = fieldNameTo;
//parentEntityMapId.Value = new Guid("DC6574CB-92CE-446C-A5D6-885A75107D52");
attributeMap.parentattributemapid = parentEntityMapId;
var targetAttributeMap = new v4.SdkTypeProxy.TargetCreateAttributeMap();
targetAttributeMap.AttributeMap = attributeMap;
var attributeMapCreateRequest = new v4.SdkTypeProxy.CreateRequest();
attributeMapCreateRequest.Target = targetAttributeMap;
var response = this.CrmService.Execute(attributeMapCreateRequest);
然而,这给了我这个错误信息:
0 x80046203无效的映射。属性不可映射,或者属性的类型不同,或者目标属性的大小小于源属性的大小。平台
如果您能给我任何帮助或见解,我将不胜感激。
终于想通了。需要拉现有的实体图,然后只需添加两个字段并发送请求。如此简单的事情却如此令人沮丧……
public void CreateAttributeMapping(string fieldNameFrom, string entityNameFrom, string fieldNameTo, string entityNameTo)
{
var entityMap = Retrieve("entitymap", new List<FilterCriteria> { new FilterCriteria("targetentityname", entityNameTo), new FilterCriteria("sourceentityname", entityNameFrom) }, null);
var entityMapId = new v4.Sdk.Lookup();
entityMapId.name = entityMap.GetKeyName();
entityMapId.Value = entityMap.GetKeyValue();
var attributeMap = new v4.SdkTypeProxy.attributemap();
attributeMap.entitymapid = entityMapId;
attributeMap.sourceattributename = fieldNameTo;
attributeMap.targetattributename = fieldNameFrom;
var targetAttributeMap = new v4.SdkTypeProxy.TargetCreateAttributeMap();
targetAttributeMap.AttributeMap = attributeMap;
var attributeMapCreateRequest = new v4.SdkTypeProxy.CreateRequest();
attributeMapCreateRequest.Target = targetAttributeMap;
var response = this.CrmService.Execute(attributeMapCreateRequest);
我不能说我已经做了这个编程自己,但你所做的看起来正确的创建一个attributemap
链接跨两个实体的字段。
您得到的错误将是以下两种情况之一:
属性映射字段的大小或类型可能不同。如果您说您的集成处理了创建,这似乎不太可能,但通过界面进入CRM可能是值得的。查找引线和触点之间的关系,然后手动添加映射,看看是否会得到相同的错误。
如果你得到一个错误,那么有不同的东西(类型或大小)。
如果你没有得到错误,那么这意味着你试图做的是正确的,但是在代码的某个地方会有一个问题,就它试图创建的属性映射而言。
在这种情况下,可能值得调试以了解更多信息。然后,如果您查看您试图通过代码设置的值,并将其与现在从手动输入的数据库中存在的值进行比较,它可能会指出缺少的内容。
我们可以使用PowerShell:
Import-Module Microsoft.Xrm.Data.Powershell
function Get-EntitymapDetails() {
param(
[Parameter(Mandatory)]
[string] $SourceEntityName,
[Parameter(Mandatory)]
[string] $TargetEntityName
)
$fetchxml = @"
<fetch>
<entity name="entitymap" >
<attribute name="sourceentityname" />
<attribute name="targetentityname" />
<attribute name="entitymapidunique" />
<attribute name="entitymapid" />
<filter>
<condition attribute="sourceentityname" operator="eq" value="lead" />
<condition attribute="targetentityname" operator="eq" value="contact" />
</filter>
</entity>
</fetch>
"@
$relMappings = (Get-CrmRecordsByFetch -Fetch $fetchxml -AllRows).CrmRecords
if($relMappings.Count -ne 1) {
throw "Expected single entity-map - found $($relMappings)"
}
$relMappings
}
function Test-AttributeMappingExists() {
param(
[Parameter(Mandatory)]
[Guid] $EntityMapId,
[Parameter(Mandatory)]
[string] $SourceAttribute,
[Parameter(Mandatory)]
[string] $TargetAttribute
)
$fetchxml = @"
<fetch >
<entity name="attributemap" >
<attribute name="attributemapid" />
<filter>
<condition attribute="entitymapid" operator="eq" value="$EntityMapId" />
<condition attribute="sourceattributename" operator="eq" value="$SourceAttribute" />
<condition attribute="targetattributename" operator="eq" value="$TargetAttribute" />
</filter>
</entity>
</fetch>
"@
$attrMappings = (Get-CrmRecordsByFetch -Fetch $fetchxml)
return ($attrMappings.CrmRecords.Count -gt 0)
}
function Setup-AttributeMapping() {
param(
[Parameter(Mandatory)]
[Guid] $EntityMapId,
[Parameter(Mandatory)]
[string] $SourceAttribute,
[Parameter(Mandatory)]
[string] $TargetAttribute
)
## verify that this attribute mapping is not already defined
$attrMappingExits = Test-AttributeMappingExists `
-EntityMapId $entityMap.entitymapid `
-SourceAttribute $SourceAttribute `
-TargetAttribute $TargetAttribute
if($attrMappingExits){
Write-Host "Attribute-Mapping for '$sourceAttr' -> '$targetAttr' is already defined"
return
}
$response = New-CrmRecord -EntityLogicalName 'attributemap' -Fields @{
entitymapid = New-CrmEntityReference -EntityLogicalName 'entitymap' -Id $EntityMapId
sourceattributename = $SourceAttribute;
targetattributename = $TargetAttribute;
}
if($response -eq $null) {
throw $conn.LastCrmError
}
Write-Host "Created new attribute-mapping"
}
$conn = Get-CrmConnection -InteractiveMode
$sourceAttr = 'parentaccountid'
$targetAttr = 'parentcustomerid'
$entityMap = Get-EntitymapDetails -SourceEntityName 'lead' -TargetEntityName 'contact'
Setup-AttributeMapping `
-EntityMapId $entityMap.entitymapid `
-SourceAttribute $sourceAttr `
-TargetAttribute $targetAttr