locked
Crmservice date reteiving issue RRS feed

  • Question

  • when i try to retreive the date using crmservice

    i am able to get the date but the date which i get is 1 days before the actual date

     

    what is the reason behind this and how can i get the correct date

     

    Thanks

    varun kumar

     

    Thursday, March 25, 2010 10:25 AM

Answers

  • Hi Varun

    I specified usersettings.timezonecode to make you to identify your correct time zone.

    Below code will get the Local time for a Spcified time Zone number

    LocalTimeFromUtcTimeRequest request = new LocalTimeFromUtcTimeRequest();

    // Note: TimeZoneCode 4 is PacificStandardTime
    request.TimeZoneCode = <<SPECIFY YOUR TIME ZONE CODE>>;;
    request.UtcTime = new CrmDateTime(); //newly added.
    request.UtcTime.Value = <<CRM Date time field value>>;

    // Execute the request.
    LocalTimeFromUtcTimeResponse response = (LocalTimeFromUtcTimeResponse)_service.Execute(request);
    CrmDateTime LocalTime = response.LocalTime;


    refer the below link for various time zone. Provide your correct time zone code from the list http://msdn.microsoft.com/en-us/library/bb887715.aspx

    • Marked as answer by Jim Glass Jr Thursday, March 25, 2010 2:58 PM
    Thursday, March 25, 2010 11:47 AM
  • Hi Varun

    Why are you assigning only date field and omiting time?
    CrmDateTime utcTime = new CrmDateTime();
    utcTime.Value = getpatient.new_dateofbirth.date;

    Your code should be


    // Create LocalTimeFromTime request.
    LocalTimeFromUtcTimeRequest request = new LocalTimeFromUtcTimeRequest();


    // Note: TimeZoneCode 190 is indiastandardtime
    request.TimeZoneCode = 190;
    request.UtcTime = new CrmDateTime(); //newly added.
    request.UtcTime.Value = getpatient.new_dateofbirth.Value;


    // Execute the request.
    LocalTimeFromUtcTimeResponse response = (LocalTimeFromUtcTimeResponse)service.Execute(request);
    CrmDateTime LocalTime = response.LocalTime;

    txtPatDOB.Text = LocalTime.date.ToString();


    Regards
    Vinoth

    Thursday, March 25, 2010 12:15 PM
  • Retrive and Retrive mutiple is not supported for User settings entity.

    You have to use RetrieveUserSettingsSystemUser Class

    Sample:

    Below code gets the timezonecode for the current logined user

    WhoAmIRequest userRequest = new WhoAmIRequest();
    WhoAmIResponse user = (WhoAmIResponse)_service.Execute(userRequest);

    RetrieveUserSettingsSystemUserRequest set = new RetrieveUserSettingsSystemUserRequest();
    set.ColumnSet = new AllColumns();
    set.EntityId = new Guid(user.UserId.ToString());
    RetrieveUserSettingsSystemUserResponse response = (RetrieveUserSettingsSystemUserResponse)_service.Execute(set);

    usersettings userset = (usersettings)response.BusinessEntity;               
    int TimeZoneCode = userset.timezonecode.Value;


    Regards
    Vinoth

    Thursday, March 25, 2010 1:26 PM

All replies

  • Hum... Depending of your timezone, the date stored in DB can be a day before.

    For example, in France, if I set a date to 01/02/2010 00:00:00, it is stored as 31/01/2010 23:00:00 in UTC

    What property of the datetime are you reading to get that date shifted of one day? 


    My blog : http://mscrmtools.blogspot.com
    You will find:
    Bulk Delete LauncherView Layout replicator
    ISV.Config Manager Form Javascript Manager
    Assembly RecoveryAnd others (use tool tag on my blog)
    Thursday, March 25, 2010 10:29 AM
    Moderator
  • Varun,

    What time you are accessing???

    you need to access usertime instead of utc time.


    Mahain
    Thursday, March 25, 2010 10:31 AM
    Moderator
  • Hi Varun

    Either access usertime (CrmDateTime.UserTime) as suggested by Mahain or obtain local time by using LocalTimeFromUtcTimeRequest

    CrmDateTime utcTime = new CrmDateTime();
    utcTime.Value = "2007-01-01T12:00:00";

    // Create LocalTimeFromTime request.
    LocalTimeFromUtcTimeRequest request = new LocalTimeFromUtcTimeRequest();
                   
    // Note: TimeZoneCode 4 is PacificStandardTime
    request.TimeZoneCode = 4;
    request.UtcTime = utcTime;

    // Execute the request.
    LocalTimeFromUtcTimeResponse response = (LocalTimeFromUtcTimeResponse) service.Execute(request);
    CrmDateTime LocalTime = response.LocalTime;

    Regards
    Vinoth

    Thursday, March 25, 2010 10:37 AM
  • my crm server and sql server are on the same machine

     

    i am reading only date

    Thursday, March 25, 2010 11:00 AM
  • I don't understand your last post, with our three previous pots, you have everything you need
    My blog : http://mscrmtools.blogspot.com
    You will find:
    Bulk Delete LauncherView Layout replicator
    ISV.Config Manager Form Javascript Manager
    Assembly RecoveryAnd others (use tool tag on my blog)
    Thursday, March 25, 2010 11:05 AM
    Moderator
  • My crm server and sql server are on the same machine......i mean on the same time zone

    i have tried Vinoth B solution 

     

    this one takes my date 2 days before

    Thursday, March 25, 2010 11:08 AM
  • Varun,

    Could you Paste your code here ???


    Mahain
    Thursday, March 25, 2010 11:19 AM
    Moderator
  • Hi Varun

    What time Zone is the user in? usersettings.timezonecode.

    Did you tried to execute the code by changing it to usersettings.timezonecode?

    request.TimeZoneCode = usersettings.timezonecode;

    Regards
    Vinoth

    Thursday, March 25, 2010 11:21 AM
  • i am getting the following error

    An object reference is required for the non-static field, method, or property 'crmsdk.crmservice.usersettings.timezonecode.get'

    Thursday, March 25, 2010 11:27 AM
  • CrmDateTime utcTime = new CrmDateTime();
            utcTime.Value = getpatient.new_dateofbirth.date;

            // Create LocalTimeFromTime request.
            LocalTimeFromUtcTimeRequest request = new LocalTimeFromUtcTimeRequest();

            // Note: TimeZoneCode 4 is PacificStandardTime
            request.TimeZoneCode = usersettings.timezonecode;
            request.UtcTime = utcTime;

            // Execute the request.
            LocalTimeFromUtcTimeResponse response = (LocalTimeFromUtcTimeResponse)service.Execute(request);
            CrmDateTime LocalTime = response.LocalTime;

            txtPatDOB.Text = LocalTime.date.ToString();
    Thursday, March 25, 2010 11:33 AM
  • Hi Varun

    I specified usersettings.timezonecode to make you to identify your correct time zone.

    Below code will get the Local time for a Spcified time Zone number

    LocalTimeFromUtcTimeRequest request = new LocalTimeFromUtcTimeRequest();

    // Note: TimeZoneCode 4 is PacificStandardTime
    request.TimeZoneCode = <<SPECIFY YOUR TIME ZONE CODE>>;;
    request.UtcTime = new CrmDateTime(); //newly added.
    request.UtcTime.Value = <<CRM Date time field value>>;

    // Execute the request.
    LocalTimeFromUtcTimeResponse response = (LocalTimeFromUtcTimeResponse)_service.Execute(request);
    CrmDateTime LocalTime = response.LocalTime;


    refer the below link for various time zone. Provide your correct time zone code from the list http://msdn.microsoft.com/en-us/library/bb887715.aspx

    Thursday, March 25, 2010 11:47 AM
  • Hi Varun

    I specified usersettings.timezonecode to make you to identify your correct time zone.

    Below code will get the Local time for a Spcified time Zone number

    LocalTimeFromUtcTimeRequest request = new LocalTimeFromUtcTimeRequest();

    // Note: TimeZoneCode 4 is PacificStandardTime
    request.TimeZoneCode = <<SPECIFY YOUR TIME ZONE CODE>>;;
    request.UtcTime = new CrmDateTime(); //newly added.
    request.UtcTime.Value = <<CRM Date time field value>>;

    // Execute the request.
    LocalTimeFromUtcTimeResponse response = (LocalTimeFromUtcTimeResponse)_service.Execute(request);
    CrmDateTime LocalTime = response.LocalTime;


    refer the below link for various time zone. Provide your correct time zone code from the list http://msdn.microsoft.com/en-us/library/bb887715.aspx

    • Marked as answer by Jim Glass Jr Thursday, March 25, 2010 2:58 PM
    Thursday, March 25, 2010 11:47 AM
  • CrmDateTime utcTime = new CrmDateTime();
            utcTime.Value = getpatient.new_dateofbirth.date;

            // Create LocalTimeFromTime request.
            LocalTimeFromUtcTimeRequest request = new LocalTimeFromUtcTimeRequest();

            // Note: TimeZoneCode 190 is indiastandardtime
            request.TimeZoneCode = 190;
            request.UtcTime = new CrmDateTime(); //newly added.
            request.UtcTime.Value = utcTime.ToString();

            // Execute the request.
            LocalTimeFromUtcTimeResponse response = (LocalTimeFromUtcTimeResponse)service.Execute(request);
            CrmDateTime LocalTime = response.LocalTime;

            txtPatDOB.Text = LocalTime.date.ToString();


    Above is my code

    i am getting error "unable to process the request" at  LocalTimeFromUtcTimeResponse response = (LocalTimeFromUtcTimeResponse)service.Execute(request);
    Thursday, March 25, 2010 12:08 PM
  • solved the problem as of now

     

    but can we make the timezone code dynamic instead of hardcoding the value

     

    if the user changes the time zone then this code will not work correctly

     

    Thursday, March 25, 2010 12:12 PM
  • Hi Varun

    Why are you assigning only date field and omiting time?
    CrmDateTime utcTime = new CrmDateTime();
    utcTime.Value = getpatient.new_dateofbirth.date;

    Your code should be


    // Create LocalTimeFromTime request.
    LocalTimeFromUtcTimeRequest request = new LocalTimeFromUtcTimeRequest();


    // Note: TimeZoneCode 190 is indiastandardtime
    request.TimeZoneCode = 190;
    request.UtcTime = new CrmDateTime(); //newly added.
    request.UtcTime.Value = getpatient.new_dateofbirth.Value;


    // Execute the request.
    LocalTimeFromUtcTimeResponse response = (LocalTimeFromUtcTimeResponse)service.Execute(request);
    CrmDateTime LocalTime = response.LocalTime;

    txtPatDOB.Text = LocalTime.date.ToString();


    Regards
    Vinoth

    Thursday, March 25, 2010 12:15 PM
  • how to get the time zone dynamically
    Thursday, March 25, 2010 12:36 PM
  • You need to query the usersettings class
    My blog : http://mscrmtools.blogspot.com
    You will find:
    Bulk Delete LauncherView Layout replicator
    ISV.Config Manager Form Javascript Manager
    Assembly RecoveryAnd others (use tool tag on my blog)
    Thursday, March 25, 2010 12:38 PM
    Moderator
  • As i said earlier you can obtain the values from the field usersettings.timezonecode from usersettings entity.

     

    Thursday, March 25, 2010 12:50 PM
  • Retrive and Retrive mutiple is not supported for User settings entity.

    You have to use RetrieveUserSettingsSystemUser Class

    Sample:

    Below code gets the timezonecode for the current logined user

    WhoAmIRequest userRequest = new WhoAmIRequest();
    WhoAmIResponse user = (WhoAmIResponse)_service.Execute(userRequest);

    RetrieveUserSettingsSystemUserRequest set = new RetrieveUserSettingsSystemUserRequest();
    set.ColumnSet = new AllColumns();
    set.EntityId = new Guid(user.UserId.ToString());
    RetrieveUserSettingsSystemUserResponse response = (RetrieveUserSettingsSystemUserResponse)_service.Execute(set);

    usersettings userset = (usersettings)response.BusinessEntity;               
    int TimeZoneCode = userset.timezonecode.Value;


    Regards
    Vinoth

    Thursday, March 25, 2010 1:26 PM