- Programmatically creating a service appointment
- Posted by Steve Ware on July 20th, 2006
I'm trying to create a "recurring" service appointment by allowing users to
select an existing appointment, and specifying a number of times to copy it
with an interval of days to add each time.
I'm getting stuck on statecode/statusccode. No matter what combination I
enter I get
"0x80048408 State code is invalid or state code is valid but status code is
invalid for a specified state code"
It's strange to me because essentially all I'm trying to do is copy the
status and state of an already existing, un-closed service activity
(referencing an un-closed case, etc.
Here's my code. you can see all sorts of commented out attempts to "book" or
change codes.
Any thoughts?
-------------------------
Dim lsWork As String
Dim lRet As Integer
Dim llLCV As Int16
Dim llDayGap As Int16
Dim llFound As MailWork.HermiaCRM.serviceappointment
Dim lNewStartDate As Date
Dim lNewEndDate As Date
Dim llMax As Int16
If Me.lstMain.SelectedIndex < 1 Then
MsgBox("Please select an item", MsgBoxStyle.OKOnly)
GoTo Exit_Sub
End If
llFound = Me.gServiceActivityCol.Item(Me.lstMain.SelectedInd ex + 1)
lsWork = "Duplicate item " & llFound.activityid.Value.ToString & vbCrLf
lsWork = lsWork & "Subject: " & llFound.subject & vbCrLf
lsWork = lsWork & "Description: " & llFound.description & vbCrLf & vbCrLf
lsWork = lsWork & "Repeat count:" & Me.txtRepeatCount.Text.ToString & "
times "
lsWork = lsWork & "Gap of " & Me.txtApptDayGap.Text & " days between each"
lsWork = lsWork & "?????????????????????"
lRet = MsgBox(lsWork, MsgBoxStyle.YesNo)
If (lRet = Microsoft.VisualBasic.MsgBoxResult.Yes) And
IsNumeric(Me.txtRepeatCount.Text.ToString) And
IsNumeric(Me.txtApptDayGap.Text.ToString) Then
llMax = Int16.Parse(Me.txtRepeatCount.Text.ToString)
llDayGap = Int16.Parse(Me.txtApptDayGap.Text.ToString)
llLCV = 1
While llLCV <= llMax
'First time
If llLCV = 1 Then
lNewStartDate = llFound.scheduledstart.Value
lNewStartDate = lNewStartDate.AddDays(llDayGap)
lNewEndDate = llFound.scheduledend.Value
lNewEndDate = lNewEndDate.AddDays(llDayGap)
Else
lNewStartDate = lNewStartDate.AddDays(llDayGap)
lNewEndDate = lNewEndDate.AddDays(llDayGap)
End If
'If we're on sat now, add 2 days
If lNewStartDate.DayOfWeek = DayOfWeek.Saturday Then
lNewStartDate = lNewStartDate.AddDays(2)
'If we're on sun now, add 1 day
ElseIf lNewStartDate.DayOfWeek = DayOfWeek.Sunday Then
lNewStartDate = lNewStartDate.AddDays(1)
End If
'If we're on sat now, add 2 days
If lNewEndDate.DayOfWeek = DayOfWeek.Saturday Then
lNewEndDate = lNewEndDate.AddDays(2)
'If we're on sun now, add 1 day
ElseIf lNewEndDate.DayOfWeek = DayOfWeek.Sunday Then
lNewEndDate = lNewEndDate.AddDays(1)
End If
Dim lNewServiceAppt As New serviceappointment
lNewServiceAppt.actualdurationminutes = llFound.actualdurationminutes
lNewServiceAppt.actualend = New MailWork.HermiaCRM.CrmDateTime
lNewServiceAppt.actualend.Value = lNewEndDate
lNewServiceAppt.actualstart = New MailWork.HermiaCRM.CrmDateTime
lNewServiceAppt.actualstart.Value = lNewStartDate
lNewServiceAppt.scheduledend = New MailWork.HermiaCRM.CrmDateTime
lNewServiceAppt.scheduledend.Value =
lNewEndDate.ToUniversalTime.ToString()
lNewServiceAppt.scheduledstart = New MailWork.HermiaCRM.CrmDateTime
lNewServiceAppt.scheduledstart.Value =
lNewStartDate.ToUniversalTime.ToString()
lNewServiceAppt.category = llFound.category
lNewServiceAppt.createdby = llFound.createdby
lNewServiceAppt.createdon = New MailWork.HermiaCRM.CrmDateTime
lNewServiceAppt.createdon.Value = Date.Now
lNewServiceAppt.customers = llFound.customers
lNewServiceAppt.description = llFound.description
lNewServiceAppt.isalldayevent = llFound.isalldayevent
lNewServiceAppt.isbilled = llFound.isbilled
lNewServiceAppt.isworkflowcreated = llFound.isworkflowcreated
lNewServiceAppt.location = llFound.location
lNewServiceAppt.modifiedby = llFound.modifiedby
lNewServiceAppt.modifiedon = New MailWork.HermiaCRM.CrmDateTime
lNewServiceAppt.modifiedon.Value = Date.Now
lNewServiceAppt.ownerid = llFound.ownerid
lNewServiceAppt.owningbusinessunit = llFound.owningbusinessunit
lNewServiceAppt.prioritycode = llFound.prioritycode
lNewServiceAppt.regardingobjectid = llFound.regardingobjectid
lNewServiceAppt.resources = llFound.resources
lNewServiceAppt.scheduleddurationminutes =
llFound.scheduleddurationminutes
lNewServiceAppt.serviceid = llFound.serviceid
lNewServiceAppt.siteid = llFound.siteid
lNewServiceAppt.statuscode = llFound.statuscode
lNewServiceAppt.statecode = llFound.statecode
'lNewServiceAppt.statuscode.Value = 3
'lNewServiceAppt.statuscode.name = "Pending"
'Dim lState As New ServiceAppointmentStateInfo
'lState.Value = ServiceAppointmentState.Open
'lState.Open = ServiceAppointmentState.Open
'lNewServiceAppt.statecode = New
MailWork.HermiaCRM.ServiceAppointmentStateInfo
'lNewServiceAppt.statecode.Value = 3
'ServiceAppointmentState.Scheduled
'lNewServiceAppt.statuscode = llFound.statuscode
'lNewServiceAppt.statuscode = llFound.statuscode
'lNewServiceAppt.statuscode.Value = 3
'lNewServiceAppt.subcategory = llFound.subcategory
lNewServiceAppt.subject = llFound.subject
'lNewServiceAppt.subscriptionid = llFound.subscriptionid
Dim Target As New TargetCreateServiceAppointment
Target.ServiceAppointment = lNewServiceAppt
'Dim Target As New TargetScheduleServiceAppointment
'Dim book As New BookRequest
'book.Target = Target
'Dim booked As BookResponse
Dim create As New CreateRequest
create.Target = Target
Dim created As CreateResponse
Try
created = CType(svcCRM.Execute(create), CreateResponse)
'booked = CType(svcCRM.Execute(book), BookResponse)
Catch ex As System.Web.Services.Protocols.SoapException
MsgBox(ex.Message & " - " & ex.Detail.InnerText)
GoTo exit_sub
End Try
llLCV += 1
End While
End If
- Posted by Michael Höhne on July 21st, 2006
Steve,
Try creating the new service appointment without setting the status/state
values and use SetStateServiceAppointment afterwards to set the state to the
desired value.
--
Michael
http://www.stunnware.com/crm2
----------------------------------------------------------
"Steve Ware" <SteveWare@discussions.microsoft.com> schrieb im Newsbeitrag
news:C4F78B59-1D6B-4CD8-A3F8-16CDB0D0C9B7@microsoft.com...
- Posted by Steve Ware on July 21st, 2006
Thanks for the thought.
You mean comment out the 2 lines
lNewServiceAppt.statuscode = llFound.statuscode
lNewServiceAppt.statecode = llFound.statecode
???
That leads to "80040237 - Operation failed due to a SQL integrity violation".
Trying many combinations of status and state, that was the 2nd most common
error (after the one below)
Other ideas?
Steve
"Michael Höhne" wrote:
- Posted by Michael Höhne on July 21st, 2006
Remove the assignments for createdby, createdon, modifiedby and modifiedon,
as they will be set automatically by the web service. Do you receive this
error every time, or when creating the first copy? If this is the case, then
there likely is a field that CRM doesn't like to see in the request. You
should be able to find out which on it is by looking at the server logs
(http://www.stunnware.com/crm2/topic.aspx?id=ErrorInfo).
--
Michael
http://www.stunnware.com/crm2
----------------------------------------------------------
"Steve Ware" <SteveWare@discussions.microsoft.com> schrieb im Newsbeitrag
news:1B3B133F-9B3C-4A01-97F3-BB111792A441@microsoft.com...
- Posted by Steve Ware on July 21st, 2006
Neat stuff there. Ok the error on the server in
"w3wp-MSCRMServices-20060721.log" I think is relevant amounts to:
Microsoft.Crm.CrmArgumentException: 4 is not a valid status code for state
code ServiceAppointmentState.Open on serviceappointment.
Which is strange because stepping the code, I'm sure the statecode is
getting set to a "value" of "Scheduled" and the statuscode has a "name" of
"Reserved" and a "value" of 4.
Another strange thing is that I can manually force the state to run the
range from 1 to 9. But any attempts to change the status with code like
lNewServiceAppt.statecode.Value = ServiceAppointmentState.Closed
still always shows in the log an error of "[whatever I had set the status to
be] is not a valid status code for state code ServiceAppointmentState.Open"
Even if I verify in the Command Window at runtime that the assignment of
statecode to "closed", or "scheduled" seemed to have "worked". Why would it
still think (or report a log error because) the state is "Open"?
While we're at this, is there someplace that lists valid status/state
combinations for entities?
Thanks,
Steve
"Michael Höhne" wrote:
- Posted by Steve Ware on July 21st, 2006
Sorry, forgot to answer: I get the error every time. I have yet to get one
new activity to save.
"Steve Ware" wrote: