2

Im having some trouble with DateTime: Using Fetch

  <attribute name='new_startdate' groupby='true' dategrouping='day' alias='new_startdate' /> 
  <attribute name='new_enddate' groupby='true' dategrouping='day' alias='new_enddate' /> 
  <attribute name='new_duedate' groupby='true' dategrouping='day' alias='new_duedate' /> 

Think this is the bit thats Wrong....

  DateTime scheduledstart = ((DateTime)((AliasedValue)a["new_startdate"]).Value);
  tracer.Trace("DateTime 1 Done");
  DateTime enddate = ((DateTime)((AliasedValue)a["new_enddate"]).Value);
  tracer.Trace("DateTime 2 Done");
  DateTime scheduledend = ((DateTime)((AliasedValue)a["new_duedate"]).Value);

Then i add to the new Entity...

    if (scheduledstart != null)
    {
      Activity.Attributes.Add("scheduledstart", scheduledstart);
    }
    if (enddate != null)
    {
       Activity.Attributes.Add("scheduledend", enddate);
    }
    if (scheduledend != null)
    {
     Activity.Attributes.Add("scheduledend", scheduledend);
    }

Any Ideas How i write the DateTime using AliasedValue from fetch? or a better way to do this>

Thanks

Richard Dewhirst
  • 229
  • 7
  • 20
  • I read this... think its the same issue... When you read any CRM DateTime value using OData Service, the value comes in “/Date(1314763200000)/“ format, which we can’t directly set to DateTime field. http://rajeevpentyala.wordpress.com/2011/08/21/read-odata-service-date-field-in-crm-2011/. How would i do the concversion from a Fetch using AliasedValue? – Richard Dewhirst Mar 08 '13 at 09:57
  • 2
    What is the error or problem? – glosrob Mar 08 '13 at 12:44
  • if it's aliased, you would do result["."]; you can see this better if you debug into your code and look through the result.attributes – Mike_Matthews_II Mar 11 '13 at 19:08

2 Answers2

1

I used to hate dealing with aliased values, but then I wrote some extension methods and now I don't worry about them any more. Check out my blog Simplifying Retrieval of Aliased Values in CRM 2011. I'm thinking it'll help solve your current problem.

Daryl
  • 18,592
  • 9
  • 78
  • 145
  • Yeah thats useful, I am looking at dates and seems to be an issue, down to "dategrouping" i have learnt that CRM brings the date in as Int32, im hoping that there is a better way then dategrouping "day" "month" "year" and putting it all back together. i have tried to convert to datetime but then crm dosent like the format from plugin... & cant pass int32 to datetime field. – Richard Dewhirst Mar 08 '13 at 15:20
  • @RichardDewhirst I've never used DateGrouping before. So (AliasedValue)a["new_duedate"]).Value is an int? What example values are you seeing? Is your problem converting the int into a DateTime? – Daryl Mar 08 '13 at 16:04
  • +1 for promoting EMs. Also, did you get the message about licensing I've sent you? And for some reason I can't follow your blog... :( – Konrad Viltersten Mar 08 '13 at 17:32
  • @KonradViltersten I haven't posted an entry to my blog in over 2 years before today... Is it giving you an error message? I don't remember the message on licensing... – Daryl Mar 08 '13 at 17:46
-1

Thanks for the help, I have decided to just use a seperate fetch for the date values

string Date_Gather = string.Format(@"         
      <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
         <entity name='new_import'>                       
                 <attribute name='new_enddate'/>         
         </entity>
       </fetch>", entity.Id);

foreach (var b in Date_Gather_result.Entities)
{

   if (b.Attributes.Contains("new_enddate"))
      {
          enddate = ((DateTime)(b["new_enddate"]));
          Entity.Attributes.Add("scheduledend", enddate);
      }
}

Thanks anyway hope it helps others

Richard Dewhirst
  • 229
  • 7
  • 20