Get option set and lookup field values from FetchXml in CRM 2011

jimminybob picture jimminybob · Jul 9, 2012 · Viewed 9.6k times · Source

I'm attempting to use fetchxml to obtain values from another record to create a new one. I have managed to get it all working apart from getting any values from option set or lookup fields. The option set ones dont give any error but just dont set the field at all and the lookups say that the GUID is not in the correct format, presumably because it is not passing through a GUID.

Can anyone help me figure out how to get these values?

Thanks

EDIT: My code is below. The option set is grading (ts_grading) and its the qualityStatement Guid (ts_quality_statement_id) i need to get from the fetch.

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

        }

    }

Answer

Daryl picture Daryl · Jul 10, 2012

OptionSet Attributes are returned as OptionSetValues and foriegn keys are returned as Entity References. Try using the Entity.GetAttributeValue instead of doing all of that ToString casting work:

    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;

    }

And if you want the OptionSetValue Text value, you'll need to look it up using the OptionSet metadata service call or looking in the Entity.FormattedAttributes property.

Also using early bound objects will keep you from having these types of casting issues.