SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

現役エンジニア直伝! 「現場」で使えるコンポーネント活用術(SPREAD)

Salesforceに表計算グリッドコントロール「SPREAD」のパワーを融合する

「CData ADO.NET Provider for Salesforce 4J」の紹介

  • このエントリーをはてなブックマークに追加

コードからSalesforceを使用

 データソースを使った例は手軽である反面、細かな調整がしづらいため、同様のことをコードで実現した例も紹介します。

参照設定

 GACにあるSystem.Data.CData.Salesforce.dllを参照設定します。

図17 参照設定
図17 参照設定

Salesforceアクセス用Model

 CDataは、ADO.NETと同様にConnectionで接続しDataAdapterでデータを取得します。

リスト2 SalesForceModel
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
  1. SalesforceConnectionでSalesforceとの接続を定義
  2. SalesforceDataAdapterでデータの取得
  3. 取得したデータはObservableCollectionに設定

Binding用ViewModel

 ModelとViewを連携するためのViewModelを作成します。

リスト3 SalesForceModel
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」を追加します。

図18 ツールボックスアイテムの選択
図18 ツールボックスアイテムの選択

 追加が終わったら、WPFウィンドウにドラッグ&ドロップしてからプロパティの微調整やその他の画面要素の設定を行います。

リスト4 MainWindow.xaml
<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が実行されてデータの取得が行われます。

図19 実行結果
図19 実行結果

まとめ

 CData ADO.NET Provider for Salesfoce 4Jを使えば、Salesforceをクラウドデータベースのように利用することができます。そして、ADO.NETライクで使えるということは、今回取り上げたSPRAEDだけではなく、ActiveReportsを使って日本のビジネスに合った帳票を作成してSalesforceから印刷するということも可能だということです。つまり、納品伝票や請求書など相手先が指定する帳票スタイルであったとしてもSalesforceから印刷できるのです。

 すべてをWindowsアプリでではなくSalesforceを核として適材適所でハイブリッド化することでSalesforceを本当の意味での自社インフラになります。もし、「SalseForceでもいいけれど、ここだけでもどうにかなれば」と躊躇する要素があるのであれば、そこに市販コンポーネントを注入した姿を検討してみてはいかがでしょうか。

この記事は参考になりましたか?

  • このエントリーをはてなブックマークに追加
現役エンジニア直伝! 「現場」で使えるコンポーネント活用術(SPREAD)連載記事一覧

もっと読む

この記事の著者

初音玲(ハツネアキラ)

 国内SIerのSEでパッケージ製品開発を主に行っており、最近は、空間認識や音声認識などを応用した製品を手掛けています。 個人的には、仕事の内容をさらに拡張したHoloLensなどのMRを中心に活動しています。 Microsoft MVP for Windows Development ブログ:http://hatsune.hatenablog.jp/

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

この記事は参考になりましたか?

この記事をシェア

  • このエントリーをはてなブックマークに追加
CodeZine(コードジン)
https://codezine.jp/article/detail/8400 2016/03/14 11:39

おすすめ

アクセスランキング

アクセスランキング

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー

アクセスランキング

アクセスランキング