You are on page 1of 7

Working with Data Sources in the SAP Business One UI API

Applies to:
Business One For more information, visit the Business One homepage.

Summary
Data sources provide a means of managing values that are displayed on a form when working with the SAP Business One User Interface API. Author: James Kofalt

Company: SAP Created on: 13 May 2009

Author Bio
As Program Manager for SAP Business One Software Solution Partners, Jim Kofalt is responsible for the industry solutions program and other initiatives that drive new business for SAP and its channel partners. Jim has 20 years experience in the software industry, including roles in product management and strategy, business development, and systems implementation.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 1

Working with Data Sources in the SAP Business One UI API

Table of Contents
Introduction .........................................................................................................................................................3 DBDataSources ..................................................................................................................................................3 UserDataSources ...............................................................................................................................................4 Related Contents ................................................................................................................................................6 Copyright.............................................................................................................................................................7

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 2

Working with Data Sources in the SAP Business One UI API

Introduction
You use data sources when working with the SAP Business One User Interface API because they serve several purposes: Data sources provide a means of working with data in a form without updating user interface elements every time data changes. In this respect, they improve performance because they allow you to manipulate data without constantly refreshing the UI. Data sources allow you to query SAP Business One data and quickly and easily populate a matrix (grid) item with data. Data sources are required for some items, such as for checkbox items.

There are two types of data sources in SAP Business One: DBDataSources and UserDataSources.

DBDataSources
DBDataSources contain tabular data that has been queried from a system table or user table in the SAP Business One company database. They are always associated with matrix objects, because they represent "two-dimensional" recordset data. In other words, DBDataSources contain data that is organized into columns and rows. DBDataSources serve these purposes: Allow you to work with data without necessarily refreshing the user interface with every change. DBDataSources are like a recordset object variable that has scope within the form that contains the DBDataSource. Allow you to query SAP Business One data and populate a matrix with the data Required in a matrix object

To add a DBDataSource to a form, use the Add method of the DBDataSources collection:
Dim oDBds as SAPbouiCOM.DBDataSource Set oDBds = oForm.DataSources.DBDataSources.Add ("OCRD") oDBds.Query

The parameter value "OCRD" passed with the Add method specifies the database table associated with the data source. OCRD contains Business Partner master data. You may filter the records returned to the data source by creating a Conditions object and passing it with the Query method. For example, if you wanted to limit the records returned to Customers and Leads, you could modify the code to read as follows:
Dim oDBds as SAPbouiCOM.DBDataSource Set oDBds = oForm.DataSources.DBDataSources.Add ("OCRD") Dim oConditions As SAPbouiCOM.Conditions Dim oCondition As SAPbouiCOM.Condition Set oConditions = New SAPbouiCOM.Conditions Set oCondition = oConditions.Add oCondition.BracketOpenNum = 2 oCondition.Alias = "CardType" oCondition.Operation = co_EQUAL oCondition.CondVal = "C" oCondition.BracketCloseNum = 1 oCondition.Relationship = cr_OR Set oCondition = oConditions.Add oCondition.BracketOpenNum = 1 oCondition.Alias = "CardType" oCondition.Operation = co_EQUAL oCondition.CondVal = "L"

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 3

Working with Data Sources in the SAP Business One UI API

oCondition.BracketCloseNum = 2 oDBds.Query (oConditions)

Next, you can bind the data source to a matrix item:


Set oColumns = oMatrix.Columns Set oColumn = oColumns.Item("CardCode") oColumn.DataBind.SetBound True, "OCRD", "CardCode" Set oColumn = oColumns.Item("Name") oColumn.DataBind.SetBound True, "OCRD", "CardName"

Finally, populate the matrix with data from the data source:
oMatrix.Clear Dim i As Integer For i = 0 To oDBDataSource.Size - 1 oDBds.Offset = i oMatrix.AddRow Next i

It is very important to note that DBDataSources do not write back to the database. When you create a DBDataSource, you are querying the database. However, to insert new records, update existing records, or delete records from the database, you need to use the business objects that are available through the Data Interface API (DI-API). This will ensure that referential integrity is maintained, and that business rules are applied properly.

UserDataSources
A UserDataSource holds a single data value. In this respect, it acts like a simple, form-level variable. It cannot behave like a recordset or array, although it can be bound to a matrix column, which will be discussed below. Keep in mind, however, that it can only hold one value at a time. UserDataSources hold values that are assigned by a user; that is, they are not associated with a table, and they do not query data directly from the SAP Business One database. A simple example of when you would use a UserDataSource is for checkbox items. A checkbox cannot hold a value until it is bound to a data source of the type "short text". If you add a checkbox to a form in SAP Business One without binding it to a UserDataSource, you will not be able to set its Check property to True.

Dim itm As SAPbouiCOM.Item Dim chkCheck As SAPbouiCOM.checkbox Dim uds As UserDataSource Set itm = frm.Items.Add("chk1", it_CHECK_BOX) Set chkCheck = itm.Specific chkCheck.Caption = "On Hold?" Set uds = frm.DataSources.UserDataSources.Add("ds1", dt_SHORT_TEXT, 1) chkCheck.DataBind.SetBound True, "", "ds1" chkCheck.Checked = True

Using a UserDataSource in a matrix is somewhat more difficult to understand, because a UserDataSource it not designed to hold tabular data or arrays. A UserDataSource only holds one value at a time. The key to understanding its usage in a matrix lies in the GetLineData and SetLineData methods.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 4

Working with Data Sources in the SAP Business One UI API

You can add a UserDataSource to a form, then to a column in a matrix item:


Dim oUds as SAPbouiCOM.UserDataSource Set oUds = _ oFrm.DataSources.UserDataSources.Add "Remarks", dt_LONG_TEXT, 30 Set oColumn = oColumns.Item("Comments") oColumn.DataBind.SetBound True, "", " Remarks"

Now I can set the value of the data source: oUds.Value = "Always pays on time." Use the GetLineData method to populate your UserDataSources with values from a particular row in the matrix. For example, the following code would update row 3 of the matrix with the values of the bound data sources for each column in the row (including all DBDataSources and all UserDataSources that are bound to columns in the matrix.)
oMatrix.SetLineData(3)

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 5

Working with Data Sources in the SAP Business One UI API

Related Contents
For more information, visit the Business One homepage.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 6

Working with Data Sources in the SAP Business One UI API

Copyright
Copyright 2009 SAP AG. All rights reserved. No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation. IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9, iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER, OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation. Linux is the registered trademark of Linus Torvalds in the U.S. and other countries. Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other countries. Oracle is a registered trademark of Oracle Corporation. UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C, World Wide Web Consortium, Massachusetts Institute of Technology. Java is a registered trademark of Sun Microsystems, Inc. JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape. SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP Business ByDesign, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries. Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects S.A. in the United States and in other countries. Business Objects is an SAP company. All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary. These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 7

You might also like