コードからSalesforceを使用
データソースを使った例は手軽である反面、細かな調整がしづらいため、同様のことをコードで実現した例も紹介します。
参照設定
GACにあるSystem.Data.CData.Salesforce.dllを参照設定します。
Salesforceアクセス用Model
CDataは、ADO.NETと同様にConnectionで接続しDataAdapterでデータを取得します。
Namespace Models
Public Class Campaign
Public Property Id As String
Public Property Name As String
Public Property Type As String
Public Property Status As String
Public Property StartDate As DateTime?
Public Property EndDate As DateTime?
Public Property ExpectedRevenue As Double?
Public Property BudgetedCost As Double?
Public Property ActualCost As Double?
End Class
Public Class SalesForceModel
Implements INotifyPropertyChanged
Private ConnectionString As String =
"Offline=False;" &
"Security Token=wxFN7ZEITyiU0hvcvTUQ5I80a;" &
"User={0};" &
"Password={1};"
Public Property Items As New ObservableCollection(Of Campaign)
Public Sub GetCampaign(userID As String, password As String)
Dim connString = String.Format(ConnectionString, userID, password)
Using conn As New SalesforceConnection(connString)
Dim sql As String = "SELECT * FROM Campaign WHERE IsDeleted=false"
Using da As New SalesforceDataAdapter(sql, conn)
Using tb As New DataTable
da.Fill(tb)
For Each row As DataRow In tb.Rows
Me.Items.Add(
New Campaign With
{
.Id = row("Id"),
.Name = row("Name"),
.Type = row("Type"),
.Status = row("Status"),
.StartDate = row("StartDate"),
.EndDate = row("EndDate"),
.ExpectedRevenue = row("ExpectedRevenue"),
.BudgetedCost = row("BudgetedCost"),
.ActualCost = If(IsDBNull(row("ActualCost")),
Nothing,
row("ActualCost"))
}
)
Next
End Using
End Using
End Using
End Sub
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Private Sub OnPropertyChanged(<CallerMemberName> Optional propertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
End Namespace
- SalesforceConnectionでSalesforceとの接続を定義
- SalesforceDataAdapterでデータの取得
- 取得したデータはObservableCollectionに設定
Binding用ViewModel
ModelとViewを連携するためのViewModelを作成します。
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Imports CZ1411Spread.Models
Imports System.Collections.ObjectModel
Imports CZ1411Spread.Common
Namespace ViewModels
Public Class MainViewModel
Implements INotifyPropertyChanged
Private WithEvents Model As New SalesForceModel
Public Property Items As ObservableCollection(Of Campaign)
Get
Return Me.Model.Items
End Get
Set(value As ObservableCollection(Of Campaign))
Me.Model.Items = value
End Set
End Property
Private _GetCommand As RelayCommand
Public Property GetCommand() As RelayCommand
Get
If _GetCommand Is Nothing Then
_GetCommand = New RelayCommand(AddressOf Me.GetCampaign)
End If
Return _GetCommand
End Get
Set(value As RelayCommand)
_GetCommand = value
End Set
End Property
Public Sub GetCampaign()
Dim userID = "hatsune@wankuma.com"
Dim password = "hogehoge"
Me.Model.GetCampaign(userID, password)
End Sub
Private Sub Model_PropertyChanged(sender As Object, …
OnPropertyChanged(e.PropertyName)
End Sub
Public Event PropertyChanged(sender As Object, …
Private Sub OnPropertyChanged(<CallerMemberName> Optional propertyName …
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
End Namespace
SPREADを使ったView
ObservableCollectionをSPREADに表示するViewを定義します。そのためにはツールボックスに「GcSpreadGrid」を追加します。
追加が終わったら、WPFウィンドウにドラッグ&ドロップしてからプロパティの微調整やその他の画面要素の設定を行います。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sg="http://schemas.grapecity.com/windows/spreadgrid/2012"
x:Class="Views.MainWindow"
Title="CZ1411Spread" Height="300" Width="498">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<sg:GcSpreadGrid HorizontalAlignment="Left"
VerticalAlignment="Top"
ItemsSource="{Binding Items}"
CanUserSortColumns="True"
CanUserFilterColumns="True"/>
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="10">
<Button Content="GET" Command="{Binding GetCommand}" />
</StackPanel>
</Grid>
</Window>
実行結果
このサンプルを実行するとデータが表示されていない画面が表示されます。[GET]ボタンをクリックするとViewModelのGetCommandが実行されてデータの取得が行われます。
まとめ
CData ADO.NET Provider for Salesfoce 4Jを使えば、Salesforceをクラウドデータベースのように利用することができます。そして、ADO.NETライクで使えるということは、今回取り上げたSPRAEDだけではなく、ActiveReportsを使って日本のビジネスに合った帳票を作成してSalesforceから印刷するということも可能だということです。つまり、納品伝票や請求書など相手先が指定する帳票スタイルであったとしてもSalesforceから印刷できるのです。
すべてをWindowsアプリでではなくSalesforceを核として適材適所でハイブリッド化することでSalesforceを本当の意味での自社インフラになります。もし、「SalseForceでもいいけれど、ここだけでもどうにかなれば」と躊躇する要素があるのであれば、そこに市販コンポーネントを注入した姿を検討してみてはいかがでしょうか。



