locked
Retrieving a single Guid in CRM 4.0 RRS feed

  • Question

  • I'm new to CRM (version 4.0) and i'm trying to return a 'yearid' guide based on a given year (which is also stored in the entity).So far i've got:

        public static Guid GetYearID(string yearName)
       
    {

           
    ICrmService service = CrmServiceFactory.GetCrmService();

           
    // Create the query object.
           
    QueryExpression query = new QueryExpression("year");
           
    ColumnSet columns = new ColumnSet();
            columns
    .AddColumn("yearid");
            query
    .ColumnSet = columns;      

           
    FilterExpression filter = new FilterExpression();

            filter
    .FilterOperator = LogicalOperator.And;
            filter
    .AddCondition(new ConditionExpression
           
    {
               
    AttributeName = "yearName",
               
    Operator = ConditionOperator.Equal,
               
    Values = new object[] { yearName}
           
    });

            query
    .Criteria = filter;

       
    }

    But my questions are:


    A) What code do in addition to this to actually store the Guid? 
    B) Is using a QueryExpression the most efficient way to do this?

    Thursday, October 18, 2012 11:44 AM

Answers

  • Hi,

    A query expression is fine for this purpose.

    You can then execute the query using:

    RetrieveMultipleRequest request = new RetrieveMultipleRequest();
    request.Query = query; 
    request.ReturnDynamicEntities  = true;
    RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request); 
    
    DynamicEntity year = response.BusinessEntityCollection.BusinessEntities[0];
    
    UniqueIdentifierProperty yearid = year.Properties["yearid"];

    You should also check that there is at least one result before referencing it in the array of business entities.

    hth,

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public     Follow Me on Twitter
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by dizzy1979 Thursday, October 18, 2012 12:44 PM
    Thursday, October 18, 2012 12:02 PM
    Answerer
  • Hi,

    here is the complete function

    public static Guid GetYearID(string yearName)
    {
            Guid YearId=Guid.Empty;
            ICrmService service = CrmServiceFactory.GetCrmService();

            // Create the query object.
            QueryExpression query = new QueryExpression("year");
            ColumnSet columns = new ColumnSet();
            columns.AddColumn("yearid");
            query.ColumnSet = columns;       

            FilterExpression filter = new FilterExpression();

            filter.FilterOperator = LogicalOperator.And;
            filter.AddCondition(new ConditionExpression
            {
                AttributeName = "yearName",
                Operator = ConditionOperator.Equal,
                Values = new object[] { yearName}
            });

            query.Criteria = filter;


    RetrieveMultipleRequest request = new RetrieveMultipleRequest();
    request.Query = query; 
    request.ReturnDynamicEntities  = true;
    RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request); 
    if (response.BusinessEntityCollection.BusinessEntities.Count > 0)

    {

    DynamicEntity year = response.BusinessEntityCollection.BusinessEntities[0];

     YearId= ((Key)(year.Properties["yearid"])).Value;
    }
    return YearId;

    }



    • Edited by _Vikram Thursday, October 18, 2012 12:18 PM
    • Proposed as answer by _Vikram Thursday, October 18, 2012 12:21 PM
    • Marked as answer by dizzy1979 Thursday, October 18, 2012 12:44 PM
    Thursday, October 18, 2012 12:13 PM

All replies

  • Hi,

    A query expression is fine for this purpose.

    You can then execute the query using:

    RetrieveMultipleRequest request = new RetrieveMultipleRequest();
    request.Query = query; 
    request.ReturnDynamicEntities  = true;
    RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request); 
    
    DynamicEntity year = response.BusinessEntityCollection.BusinessEntities[0];
    
    UniqueIdentifierProperty yearid = year.Properties["yearid"];

    You should also check that there is at least one result before referencing it in the array of business entities.

    hth,

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public     Follow Me on Twitter
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by dizzy1979 Thursday, October 18, 2012 12:44 PM
    Thursday, October 18, 2012 12:02 PM
    Answerer
  • Hi,

    here is the complete function

    public static Guid GetYearID(string yearName)
    {
            Guid YearId=Guid.Empty;
            ICrmService service = CrmServiceFactory.GetCrmService();

            // Create the query object.
            QueryExpression query = new QueryExpression("year");
            ColumnSet columns = new ColumnSet();
            columns.AddColumn("yearid");
            query.ColumnSet = columns;       

            FilterExpression filter = new FilterExpression();

            filter.FilterOperator = LogicalOperator.And;
            filter.AddCondition(new ConditionExpression
            {
                AttributeName = "yearName",
                Operator = ConditionOperator.Equal,
                Values = new object[] { yearName}
            });

            query.Criteria = filter;


    RetrieveMultipleRequest request = new RetrieveMultipleRequest();
    request.Query = query; 
    request.ReturnDynamicEntities  = true;
    RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request); 
    if (response.BusinessEntityCollection.BusinessEntities.Count > 0)

    {

    DynamicEntity year = response.BusinessEntityCollection.BusinessEntities[0];

     YearId= ((Key)(year.Properties["yearid"])).Value;
    }
    return YearId;

    }



    • Edited by _Vikram Thursday, October 18, 2012 12:18 PM
    • Proposed as answer by _Vikram Thursday, October 18, 2012 12:21 PM
    • Marked as answer by dizzy1979 Thursday, October 18, 2012 12:44 PM
    Thursday, October 18, 2012 12:13 PM