locked
Crmdatetime condition checking problem in Plugin RRS feed

  • Question

  • Hi,

    I am retrieving the appointment records based on contactid, then i need to retrieve the 2 dates from these recors which are less and gretaer dates from the current date.

    I am getting the appointment records but not able to get these less and gretaer dates.

    How can i loop these dates.

    Please find the below my code

    if

     

    (context.InputParameters.Properties.Contains("Target") &&

    context.InputParameters.Properties[

    "Target"] is DynamicEntity)

    {

     

    // Obtain the target business entity from the input parmameters.

     

    DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties["Target"];

     

    if (entity.Name == EntityName.appointment.ToString())

    {

     

    //entity = context.PostEntityImages["Target"] as Microsoft.Crm.Sdk.DynamicEntity;

     

    //Retrieve the Physician contactid

     

    if (entity.Properties.Contains("regardingobjectid"))

    {

     

    if (((Lookup)entity.Properties["regardingobjectid"]).type == "contact")

    {

    _physiciancontactid = ((

    Lookup)entity.Properties["regardingobjectid"]).Value;

    }

     

    else

     

    return;

    }

    }

    }

     

    ConditionExpression con = new ConditionExpression();

    con.AttributeName =

    "regardingobjectid";

    con.Operator =

    ConditionOperator.Equal;

    con.Values =

    new object[] { _physiciancontactid };

     

    //CrmDateTime now = CrmDateTime.Now;

     

    //string date = now.ToString();

     

    //ConditionExpression con1 = new ConditionExpression();

     

    //con1.AttributeName = "scheduledstart";

     

    //con1.Operator = ConditionOperator.LessEqual;

     

    //con1.Values = new string[] { date };

     

    //ConditionExpression con2 = new ConditionExpression();

     

    //con2.AttributeName = "scheduledstart";

     

    //con2.Operator = ConditionOperator.GreaterEqual;

     

    //con2.Values = new string[] { date };

     

    FilterExpression filter = new FilterExpression();

    filter.Conditions.AddRange(

    new ConditionExpression[] { con });

    filter.FilterOperator =

    LogicalOperator.And;

     

    QueryExpression query = new QueryExpression();

     

    ColumnSet columns = new ColumnSet(new string[] { "scheduledstart" });

    query.ColumnSet = columns;

    query.Criteria = filter;

    query.EntityName =

    "appointment";

     

    RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();

    retrieve.Query = query;

    retrieve.ReturnDynamicEntities =

    true;

     

    RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)crmService.Execute(retrieve);

     

    CrmDateTime currentDate = CrmDateTime.Now;

     

    CrmDateTime lastAppDate;

     

    CrmDateTime nextAppDate;

     

    if (retrieved.BusinessEntityCollection.BusinessEntities.Count > 0)

    {

     

    foreach (Property p in retrieved)

    {

     

    if (p.Name == "scheduledstart")

    {

     

    //here how can i check the conditions for less and greater dates

    }

    }

    }

     

    Please help me, I am struck here

     

    Thanks,

    Naveen 

      

    Wednesday, July 21, 2010 4:12 PM

Answers

All replies

  • Hi Naveen,

    you can convert crmdatetime to datetime and then can compare it with current datetime

    below post should help you to accomplish the same

    http://ronaldlemmen.blogspot.com/2008/02/crmdatetime-conversion-to-datetime.html

    http://msdn.microsoft.com/en-us/library/system.datetime.compare.aspx


    Mahain
    Wednesday, July 21, 2010 5:16 PM
    Moderator
  • First of all, I'm going to recommend that you never use a for/foreach loop to find an index you know you want.

    Change:

    foreach (Property p in retrieved)
    {
     if (p.Name == "scheduledstart")
     {
      //here how can i check the conditions for less and greater dates
     }
    }
    

    Into:

    if (retrieved.Properties.Contains("scheduledstart"))
    {
     CrmDateTime scheduledStart = (CrmDateTime)retrieved.Properties["scheduledstart"];
    }
    

    I know, technically the "Contains()" method is probably doing a loop in the background, but I'm fairly certain it's more efficient than casting out each member of the collection as a Property object before validating its members.  The second line of code is how you cast the CrmDateTimeProperty into a CrmDateTime object, which is easier to work with in the code.

    Now, regarding your inquiry about dates that are greater or lesser than the current date:  which two date attributes are you trying to check, and which rule do they get (greater, lesser, or both)?  If you're familiar with SQL, or can effectively use pseudo-code, please construct your query with an equation in a language closer to what a computer would understand; as opposed to the description you have provided.

    It would be ideal to use the QueryExpression to do the filtering based on rules, which I see appear to be commented out of your code.  In the disabled code, I notice you make the same attribute apply to both the "greater than" and "lesser than" rules.  It would be a lot less complicated to write one "is not equal to" rule, since that's effectively what both "greater than" and "lesser than" mean, in combination.  The problem is that I struggle to reconcile your code with your question, so I really don't know what you want, exactly.


    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com
    Wednesday, July 21, 2010 5:19 PM
    Moderator