以编程方式为潜在客户/联系人实体上的自定义字段创建属性映射

时间:2011-12-05 21:41:29

标签: c# mapping dynamics-crm-2011 dynamics-crm dynamics-crm-4

所以我有一个CRM集成,可以为潜在客户和联系人添加两个字段。整数和布尔值。当我们将潜在客户转换为联系人时,我希望来自潜在客户的那些自定义字段转移到新联系人。我们有超过700个实例使用我们的产品,因此这需要一个程序化的解决方案。那就是问题所在。我无法绕过CreateAttributeMappings类,并希望有人能够启发我并告诉我我在哪里愚蠢......

现在我有这样的事情:

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);

然而,这给了我这个错误信息:

0x80046203   无效的映射。属性不可映射,或属性属于不同类型,或者目标属性的大小小于源属性的大小。   平台

非常感谢您给我的任何帮助或见解。

3 个答案:

答案 0 :(得分:3)

终于明白了。需要拉出现有的实体地图,然后只需添加两个字段并发送请求。如此简单的事情令人沮丧......

    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);

答案 1 :(得分:0)

我不能说我自己已经以编程方式完成了这项工作,但是在创建attributemap方面,您所做的工作看起来是正确的。{{1}}链接两个实体的字段。

你得到的错误将是两件事之一。

它与之匹配的属性映射字段的大小或类型可能不同。如果你说你的集成负责创建,这似乎不太可能,但它可能值得通过接口进入CRM。查找潜在客户和联系人之间的关系,然后手动添加映射以查看是否收到相同的错误。

如果您收到错误,则会有不同的(类型或大小)。

如果您没有收到错误,那么这意味着您尝试做的事情是正确的,但是代码中的某个地方根据它尝试创建的属性地图会出现问题。

在这种情况下,可能需要通过调试来了解更多信息。然后,如果您查看您尝试通过代码设置的值,并与手动条目中数据库中现在存在的值进行比较,则可能会指出缺少的值。

答案 2 :(得分:0)

我们可以使用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
相关问题