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

3
  • 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. rajeevpentyala.wordpress.com/2011/08/21/…. How would i do the concversion from a Fetch using AliasedValue? Commented Mar 8, 2013 at 9:57
  • 2
    What is the error or problem?
    – glosrob
    Commented Mar 8, 2013 at 12:44
  • if it's aliased, you would do result["<alias>.<field>"]; you can see this better if you debug into your code and look through the result.attributes Commented Mar 11, 2013 at 19:08

2 Answers 2

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.

4
  • 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. Commented Mar 8, 2013 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
    Commented Mar 8, 2013 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... :( Commented Mar 8, 2013 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
    Commented Mar 8, 2013 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

1
  • Also, why fetchXml instead of query expression? Commented Mar 12, 2013 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.