'-------THIS EXAMPLE IS USED TO SYNCHRONIZE A SERVICE ACTIVITY TO CHANGES THAT TAKE PLACE '-------ON A CASE ENTITY
'-------DESERIALIZE THE PREIMAGE XML INTO A DYNAMIC ENTITY
Imports System
Imports System.Collections.Specialized
Imports System.IO
Imports System.Xml
Imports System.Xml.Xsl
Imports System.Xml.Serialization
Imports ServiceSyncToCase.CrmSdk
Imports ServiceSyncToCase.CrmMeta
Imports Microsoft.Crm.Callout
Imports RelatedServiceActivity.ServiceActivity
Public Class SyncService
Inherits CrmCalloutBase
Public Overrides Sub PostUpdate( _
ByVal userContext As CalloutUserContext, _
ByVal entityContext As CalloutEntityContext, _
ByVal preImageEntityXml As String, _
ByVal postImageEntityXml As String)
Dim CourtDateChanged As New Integer
'------------LOG THE PARAMETERS-------------------------------------------------------------
Dim sw As New StreamWriter("c:\calloutlog.txt", True)
With sw
.Write(userContext.UserId)
.Write(entityContext.EntityTypeCode)
.Write(preImageEntityXml.ToString)
.Write(postImageEntityXml.ToString)
End With
sw.Close()
'-------------SETUP THE SERVICE-----------------------------------------------------------------
Dim service As New CrmService
service.Credentials = New System.Net.NetworkCredential("username", "password", "domain")
service.Url = "http://crm.daviddomain.com/mscrmservices/2006/crmservice.asmx"
Dim meta As New MetadataService
meta.Credentials = New System.Net.NetworkCredential("username", "password", "domain")
meta.Url = "http://crm.daviddomain.com/mscrmservices/2006/metadataservice.asmx"
'----------------------------------------------------------------------------------------------
Dim preCase As New incident
'----------------DESERIALIZE THE PREIMAGE XML INTO A DYNAMIC ENTITY-----------------------------
Dim sr As TextReader = New StringReader(preImageEntityXml)
Dim root As New XmlRootAttribute("BusinessEntity")
root.[Namespace] = "http://schemas.microsoft.com/crm/2006/WebServices"
Dim xmlSerializer As New XmlSerializer(GetType(BusinessEntity), root)
Dim entity As CrmSdk.BusinessEntity = DirectCast(xmlSerializer.Deserialize(sr), BusinessEntity)
Dim myDE As CrmSdk.DynamicEntity = CType(entity, DynamicEntity)
'----------------DESERIALIZE THE POSTIMAGE XML INTO A DYNAMIC ENTITY-----------------------------
Dim sr2 As TextReader = New StringReader(postImageEntityXml)
Dim root2 As New XmlRootAttribute("BusinessEntity")
root2.[Namespace] = "http://schemas.microsoft.com/crm/2006/WebServices"
Dim xmlSerializer2 As New XmlSerializer(GetType(BusinessEntity), root2)
Dim entity2 As CrmSdk.BusinessEntity = DirectCast(xmlSerializer.Deserialize(sr2), BusinessEntity)
Dim myDE2 As CrmSdk.DynamicEntity = CType(entity2, DynamicEntity)
'-----------LOOP THROUGH THE PRE IMAGE DYNAMIC ENTITY PROPERTY BAG-----------------------------------------
Dim preCaseTitle As String
Dim preCaseCourtDate As New CrmDateTime
Dim preCaseStatusCode As New Status
Dim preCaseStateCode As String
Dim i = 0
Dim sw2 As New StreamWriter("c:\calloutlog2.txt", True)
For Each entityProperty As [Property] In myDE.Properties
If entityProperty.Name = "title" Then
'0
Dim [Property] As StringProperty = CType(entityProperty, StringProperty)
preCaseTitle = [Property].Value
sw2.WriteLine(i.ToString & ":" & preCaseTitle.ToString)
ElseIf entityProperty.Name = "new_courtdate" Then
'1
Dim [Property] As CrmDateTimeProperty = CType(entityProperty, CrmDateTimeProperty)
preCaseCourtDate = [Property].Value
sw2.WriteLine(i.ToString & ":" & preCaseCourtDate.Value.ToString)
ElseIf entityProperty.Name = "statuscode" Then
'2
Dim [Property] As StatusProperty = CType(entityProperty, StatusProperty)
preCaseStatusCode = [Property].Value
sw2.WriteLine(i.ToString & ":" & preCaseStatusCode.Value.ToString)
ElseIf entityProperty.Name = "statecode" Then
'3
Dim [Property] As StateProperty = CType(entityProperty, StateProperty)
preCaseStateCode = [Property].value
sw2.WriteLine(i.ToString & ":" & preCaseStateCode.ToString)
Else
With sw2
.WriteLine(i.ToString & ":" & ":nothing here")
End With
End If
i = i + 1
Next
If Not i = 4 Then
sw2.WriteLine(i.ToString & ":" & "Properties were missing in the preImage XML")
End If
'sw2.Close()
'-----------LOOP THROUGH THE POST IMAGE DYNAMIC ENTITY PROPERTY BAG-----------------------------------------
Dim postCaseTitle As String
Dim postCaseCourtDate As New CrmDateTime
Dim postCaseStatusCode As New Status
Dim postCaseStateCode As String
'Dim sw2 As New StreamWriter("c:\calloutlog2.txt")
'&":"&
For Each entityProperty2 As [Property] In myDE2.Properties
If entityProperty2.Name = "title" Then
Dim [Property] As StringProperty = CType(entityProperty2, StringProperty)
postCaseTitle = [Property].Value
sw2.WriteLine(i.ToString & ":" & postCaseTitle.ToString)
ElseIf entityProperty2.Name = "new_courtdate" Then
Dim [Property] As CrmDateTimeProperty = CType(entityProperty2, CrmDateTimeProperty)
postCaseCourtDate = [Property].Value
sw2.WriteLine(i.ToString & ":" & postCaseCourtDate.Value.ToString)
'---------------COMPARE THE STATUS REASONS FIRST TO FIND OUT IF ANYTHING HAS CHANGED---------------------------------
If Not postCaseCourtDate.Value Is String.Empty Then
If Not preCaseCourtDate.Value Is Nothing Then
CourtDateChanged = preCaseCourtDate.Value.ToString.CompareTo(postCaseCourtDate.Value.ToString)
Else : CourtDateChanged = -1
End If
If CourtDateChanged = -1 Then
Dim thiscase As New incident
thiscase = service.Retrieve(EntityName.incident.ToString, entityContext.InstanceId, New AllColumns)
Dim pccd As DateTime = postCaseCourtDate.Value.ToString
pccd = pccd.ToUniversalTime
Dim tccid As New Guid(thiscase.new_courtid.Value.ToString)
Dim tcoid As New Guid(thiscase.ownerid.Value.ToString)
Dim tccustid As New Guid(thiscase.customerid.Value.ToString)
'-----------STATUS UPDATE OF OLD SERVICE ACTIVITIES GOES HERE--------------------------------
'PERFORM STATUS UPDATES ON ALL OF THE OPEN SERVICE ACTIVITIES PRIOR TO CREATING A NEW ONE
'FILTER EXPRESSION SHOULD FIND ALL SERVICE ACTIVITIES WHERE
'REGARDING = THISCASE.GUID
'STATUS = ACTIVE
'SET THE UPDATED SERVICE ACTIVITY STATUS TO:
'IF STATE OF THISCASE IS COMPLETED THEN DON'T PREPEND THE STRING STATUS WITH "RESET:"
'IF STATE OF THISCASE IS ACTIVE THEN PREPEND THE STRING WITH "RESET:" AND DO ALSO CREATE SERVICE ACTIVITY (CODE BELOW)
'---ADD IF CONSTRUCTURE LOGIC TO PREVENT THE CREATION OF THIS SERVICE ACTIVITY IF THE POST
'---IMAGE XML STATE IS SET TO INACTIVE
'------WILL THE POST IMAGE XML CARRY FORTH A CLOSED STATUS????-------
'---THIS CALLS ANOTHER .DLL THAT ADDS NEW SERVICE ACTIVITIES CLICK HERE TO SEE THE WORKFLOW .DLL OF THIS FILE
NewServiceActivity(entityContext.InstanceId, pccd, tccid, tcoid, postCaseTitle.ToString, _
thiscase.new_courtid.name.ToString, tccustid)
sw2.WriteLine(i.ToString & ":" & "The courtdate has been reset to:" & pccd.ToString)
Else : sw2.WriteLine(i.ToString & ":" & "The courtdate is the same:" & postCaseCourtDate.Value.ToString)
End If
Else : sw2.WriteLine(i.ToString & ":" & "The courtdate was set to empty")
End If
'-------------END OF COMPARE OF STATUS REASON FOR CREATE NEW IF THEY ARE DIFFERENT------------------------------
ElseIf entityProperty2.Name = "statuscode" Then
Dim [Property] As StatusProperty = CType(entityProperty2, StatusProperty)
postCaseStatusCode = [Property].Value
sw2.WriteLine(i.ToString & ":" & postCaseStatusCode.Value.ToString)
ElseIf entityProperty2.Name = "statecode" Then
Dim [Property] As StateProperty = CType(entityProperty2, StateProperty)
postCaseStateCode = [Property].value
sw2.WriteLine(i.ToString & ":" & postCaseStateCode.ToString)
End If
i = i + 1
Next
With sw2
.WriteLine(i.ToString & ":" & ":nothing here")
End With
sw2.WriteLine(i.ToString & ":" & "If nothing else then for each was skipped")
sw2.Close()
End Sub
End Class
|