function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
GrahamGraham 

Problems with Date (more specifically, Type=Date)

I'm using VB.Net and the 2.5 sforce API. I'm writing some web forms in asp.net, basically I get passed an Opportunity ID, then query sforce for info. This info gets used in an app of ours, and new info is added. I have no problems querying, getting results, or updating an opportunity.

My problem is with creating a new Task. I'm using the partner api so I don't have the Task sObject, I just update records with a general sObject. This is fine so far, and I can query dates, but I don't know how to set the type in the xml to date. The error I get back is: java.lang.NumberFormatException: Invalid date. I know this is because I'm sending it a string instead of a date, but how do I change this?

Thanks so much!
Graham



My code (which has been pieced together mostly from the vb.net samples and this site:

Private Function MakeDateElement(ByVal fieldName As String, ByVal fieldValue As Date) As System.Xml.XmlElement

Dim frag As System.Xml.XmlDocument = New System.Xml.XmlDocument

Dim el As System.Xml.XmlNode = frag.CreateNode(Xml.XmlNodeType.Element, fieldName, "")

''This is what I want to somehow set, but how?:
''Set el Xml.Serialization.XmlElementAttribute(DataType:="date")

el.InnerXml = fieldValue

Return el

End Function

Private Sub createTask()
'Verify that we are already authenticated, if not
'call the login function to do so
If Not loggedIn Then
If Not login() Then
Return
End If
End If

Dim createdTask As New sObject

createdTask.type = "Task"

Dim record(5) As System.Xml.XmlElement

Try
Dim i As Integer
Dim taskArray(5, 1) As String
taskArray(0, 0) = "ActivityDate"
taskArray(1, 0) = "Description"
taskArray(2, 0) = "Priority"
taskArray(3, 0) = "Status"
taskArray(4, 0) = "Subject"
taskArray(5, 0) = "WhatId"

''tried this to get around the whole date/string thing: (didn't work)
''taskArray(0, 1) = "#" & DateTime.Now & "#"
taskArray(1, 1) = "Your Document has Assembled"
taskArray(2, 1) = "Normal"
taskArray(3, 1) = "Not Started"
taskArray(4, 1) = "Assembly Complete"
taskArray(5, 1) = sforceOpID

record(0) = MakeDateElement(taskArray(0, 0), DateTime.Today)
For i = 1 To 5
record(i) = MakeFieldElement(taskArray(i, 0), taskArray(i, 1))
Next

createdTask.Any = record

Dim saveResult As SaveResult() = binding.create(New sObject() {createdTask})
For i = 0 To 5 'saveResult(0).errors.GetUpperBound(0)
If saveResult(i).success Then
lbl5.Text = "A task: " & saveResult(i).id & " was updated."
Else
lbl5.Text = "Item" & i & " had an error updating."
lbl6.Text = "The error reported was: " & saveResult(i).errors(0).message
End If
Next
Catch ex As Exception

lbl6.Text = ex.Message

End Try

End Sub
GrahamGraham
Whoops, I guess the tabs don't transfer...looked fine in the preview though. If it's too hard to read let me know and I'll try to repost it.
DevAngelDevAngel

Hi Graham,

Because the partner api doesn't have the "smart" serialization that is the result of the Enterprise WSDL, the date needs to be specified as a string.  In particular, the string needs to have an ISO 8601 format.  This format is defined pretty well here http://www.w3.org/TR/NOTE-datetime.

For dot net, it's fairly easy to format a date in this way.

Dim taskDate as DateTime = Now

Dim isoDate as String = taskDate.Year & "-" & taskDate.Month.ToString().PadLeft(2, "0") & "-" & taskDate.Day.ToString().PadLeft(2, "0") & "T" & taskDate.Hour.ToString().PadLeft(2, "0") & ":" & taskDate.Minute.ToString().PadLeft(2, "0") & ":" & taskDate.Second.ToString().PadLeft(2, "0") & "+08:00"

The "+08:00" is the time zone designator and indicates in the example above that the time is PDT.

Incorportated in your code below:

TaskArray(0, 1) = isoDate

In your code, I would put the date formatting into the MakeDateElement function.

Cheers

GrahamGraham
Just what I was looking for, thanks so much Dave. I saw some posts about using ISO, but couldn't get it using DateTime.Now. This should fix everything, it's much appreciated.

Graham