locked
Setting the datetime using SDK RRS feed

  • Question

  • Hi All,

    I am trying to set the datetime of the 'Last Used In Campaign' field on the Lead form programmatically. This is the code i have used, but it doesn't set it! what am i doing wrong!?:

                Dim lead_lastusedinccampaign As CrmDateTimeProperty = New CrmDateTimeProperty
                lead_lastusedinccampaign.Name = "lastusedincampaign"
                lead_lastusedinccampaign.Value = New CrmDateTime(New Date(Year, Month, Day).ToUniversalTime().ToString())

    Where year, month and day are integers...

    Any help is much appreciated!

    Thanks in advance!

    Wednesday, April 14, 2010 9:08 AM

Answers

  • Hi,

    As the CrmDateTime class expects the date in a certain format. like below

    CrmDateTime dateTime = new CrmDateTime();
    dateTime.Value = "2010/4/15T17:00:00";

    So try to change your code like

    Dim Dt As New Date(Year, Month, Day)
    lead_lastusedinccampaign.Value =Dt.ToString("s") 


    Mahain
    Thursday, April 15, 2010 8:19 AM
    Moderator
  • For my part, I use the CultureInfo 

    Dt.ToString(CultureInfo.InvariantCulture.DateTimeFormat)


    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)
    • Marked as answer by bshah1985 Thursday, April 15, 2010 8:40 AM
    Thursday, April 15, 2010 8:22 AM
    Moderator

All replies

  • How do you add the property to the entity after having created this CrmDateTimeProperty
    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)
    Wednesday, April 14, 2010 9:10 AM
    Moderator
  •             DebugMsg(New DateTime(Year, Month, Day, 0, 0, 0).ToUniversalTime().ToString())
                Dim lead_lastusedinccampaign As CrmDateTimeProperty = New CrmDateTimeProperty
                lead_lastusedinccampaign.Name = "lastusedincampaign"
                lead_lastusedinccampaign.Value = New CrmDateTime(New Date(Year, Month, Day).ToUniversalTime().ToString())
    
                newlead.Properties.Add(lead_lastusedinccampaign)
    Sorry - last line above...
    Wednesday, April 14, 2010 9:23 AM
  • Your code seems ok...

    I guess you use the Update method of the crmService with that "newlead" dynamicentity?

    Unless you are in a plugin and in this case, if you are on post stage, then you do need to use the update method...


    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)
    Wednesday, April 14, 2010 9:32 AM
    Moderator
  • no, im doing it on create...
                'create new lead in CRM
                Dim createDynamic As TargetCreateDynamic = New TargetCreateDynamic()
                createDynamic.Entity = newlead
                Dim createlead As CreateRequest = New CreateRequest()
                createlead.Target = createDynamic
                DebugMsg("about to create")
                Dim newleadcr As CreateResponse = CType(service.Execute(createlead), CreateResponse)

    ...and i have just seen - not valid for create!!! aarrggghhh!!!

    will post back if it works...

    thanks for the heads up Tanguy!! :-)

    Wednesday, April 14, 2010 9:36 AM
  • I thought it was an update, that's why I didn't mention the notvalidforcreate property...
    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)
    Wednesday, April 14, 2010 9:41 AM
    Moderator
  • Now i'm getting a 'Server unable to process request' error: updated code:

                Dim new_created_lead As DynamicEntity = GetSingleDynamic("lead", New Guid(newleadid))
    
                Dim lead_lastusedinccampaign As CrmDateTimeProperty = New CrmDateTimeProperty
                lead_lastusedinccampaign.Name = "lastusedincampaign"
                lead_lastusedinccampaign.Value = New CrmDateTime(New Date(Year, Month, Day).ToUniversalTime().ToString())
                new_created_lead.Properties.Add(lead_lastusedinccampaign)
    
                DebugMsg("about to update lead")
                Dim updateDynamic As TargetUpdateDynamic = New TargetUpdateDynamic
                updateDynamic.Entity = new_created_lead
                Dim update As UpdateRequest = New UpdateRequest
                update.Target = updateDynamic
                Try
                    Dim updated As UpdateResponse = CType(service.Execute(update), UpdateResponse)
                Catch ex As Exception
                    DebugMsg(ex.Message.ToString())
                End Try
    
                DebugMsg("lead updated")

    I basically create the lead, the fetch it again and update the 'last used in campaign' field and execute the update response...but getting the above error...

    Any ideas anyone?

    Wednesday, April 14, 2010 9:57 AM
  • to have the true error message use this try catch block

    try
    {
    // your code
    }
    catch(SoapException error)
    {
    throw new Exception(error.Detail.SelectSingleNode("//description").InnerText);
    }


    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)
    Wednesday, April 14, 2010 10:01 AM
    Moderator
  • "The date-time format for 18/01/2010 00:00:00 is invalid, or value is outside the supported range."

    this is the error i am getting! :-( is anyone able to explain why this is wrong? what is wrong with that date time format? i'm in the UK.

    Thursday, April 15, 2010 7:54 AM
  • Hi,

    As the CrmDateTime class expects the date in a certain format. like below

    CrmDateTime dateTime = new CrmDateTime();
    dateTime.Value = "2010/4/15T17:00:00";

    So try to change your code like

    Dim Dt As New Date(Year, Month, Day)
    lead_lastusedinccampaign.Value =Dt.ToString("s") 


    Mahain
    Thursday, April 15, 2010 8:19 AM
    Moderator
  • For my part, I use the CultureInfo 

    Dt.ToString(CultureInfo.InvariantCulture.DateTimeFormat)


    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)
    • Marked as answer by bshah1985 Thursday, April 15, 2010 8:40 AM
    Thursday, April 15, 2010 8:22 AM
    Moderator
  • Thank you Mahain and Tanguy!

    I was already receiveing the datetime in the format "2010/4/15T17:00:00" - but was just being stupid! this is now working great! that you both again! :-)

    Thursday, April 15, 2010 8:40 AM