从CRM 2011中的FetchXml获取选项集和查找字段值

时间:2012-07-09 16:16:52

标签: dynamics-crm-2011 fetchxml

我正在尝试使用fetchxml从另一条记录中获取值以创建一个新记录。除了从选项集或查找字段中获取任何值之外,我已设法完成所有工作。选项集1不会给出任何错误,但只是根本不设置字段,并且查找说GUID格式不正确,可能是因为它没有通过GUID。

任何人都可以帮我弄清楚如何获得这些价值吗?

由于

编辑:我的代码如下。选项集是评分(ts_grading),以及我需要从fetch中获取的qualityStatement Guid(ts_quality_statement_id)。

string fetchXml = @"<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>
                            <entity name='ts_self_assessment_item'>
                            <attribute name='ts_evidence_of_areas_of_improvement'/><attribute name='ts_evidence_of_strengths'/><attribute name='ts_expected_completion_date'/>
                            <attribute name='ts_grading'/><attribute name='ts_quality_statement_id'/>
                            <filter type='and'>
                            <condition attribute='ts_self_assessment_id' uitype='ts_self_assessment' operator='eq' value='" + selfAssessmentID + "'/></filter></entity></fetch>";

        // Execute Fetch xml
        EntityCollection selfAssessmentItemResult = Service.RetrieveMultiple(new FetchExpression(fetchXml));

        foreach (var selfAssessment in selfAssessmentItemResult.Entities)
        {                
            string areasOfImprovement = selfAssessment["ts_evidence_of_areas_of_improvement"].ToString();
            string strengths = selfAssessment["ts_evidence_of_strengths"].ToString();
            DateTime completionDate = Convert.ToDateTime(selfAssessment["ts_expected_completion_date"].ToString());
            string grading = selfAssessment["ts_grading"].ToString();
            Guid qualityStatement = new Guid(selfAssessment["ts_quality_statement_id"].ToString());
            Guid parentSelfAssessment = new Guid(entity.Attributes["ts_self_assessmentid"].ToString());              

        }

    }

1 个答案:

答案 0 :(得分:5)

OptionSet属性以OptionSetValues的形式返回,foriegn键作为Entity References返回。尝试使用Entity.GetAttributeValue而不是执行所有ToString转换工作:

    foreach (var selfAssessment in selfAssessmentItemResult.Entities)
    {                
        string areasOfImprovement = selfAssessment.GetAttributeValue<String>("ts_evidence_of_areas_of_improvement");
        string strengths = selfAssessment.GetAttributeValue<string>("ts_evidence_of_strengths");
        DateTime completionDate = selfAssessment.GetAttributeValue<DateTime>("ts_expected_completion_date");
        var grading = selfAssessment.GetAttributeValue<OptionSetValue>("ts_grading");
        Guid qualityStatement = selfAssessment.GetAttributeValue<EntityReference>("ts_quality_statement_id").Id;

    }

如果你想要OptionSetValue Text值,你需要使用OptionSet元数据服务调用或查看Entity.FormattedAttributes属性来查找它。

同样使用早期绑定对象可以防止出现这些类型的转换问题。