Stephen Smith's Blog

All things Sage 300…

Opening Sage 300 ERP Sessions

with 26 comments


Introduction

Sage 300 ERP has a number of very flexible external APIs that allow programs to access all the business logic in the program. The business logic is stored in Views that are accessed via a standard API. To start using the business logic from one of our external APIs you first need to sign-on to the API and establish a session. This article is going to only talk about the AccpacCOMAPI which is our main COM API. Sage 300 ERP has an older COM API usually referred to as a4wcom, so be sure to use the newer one we are talking about here. Many of the concepts can be adapted to other APIs like the .Net or Java APIs. However to interact with other COM components like the session manager you must be using the AccpacCOMAPI. The examples in this posting will all be in Visual Basic 6.

This API has been around for a long time, but we recently received quite a few queries through customer support on establishing connections. So I thought it might be worth while writing a blog post on some of the use cases we try to support, some of the functionality that perhaps isn’t very widely known as well as the reasons for why some aspects work like they do.

For a bit more background on the Sage 300 business logic have a look at this blog posting.

Libraries

Sage 300 ERP’s COM API can be used by any tool that understands COM and how to talk to COM objects. The first step is to add the COM object to your project. In VB6 you do this by going to Project – References and adding “ACCPAC COM API Object 1.0”. In some tools you can browse to the DLL and add that, in this case you browse to where ever you installed Sage 300 ERP and then browse for runtime\a4wcomex.dll.

Creating and Initializing

Once you have the library available for you, now you need to get started. All objects in our COM API are created via APIs in our COM API. But first you need to get started by creating and initializing a session object. This is the root object and from this everything else is derived. In VB there are a couple of ways to create the initial session object either:

Dim mSession As New AccpacCOMAPI.AccpacSession

Or

Dim mSession As AccpacCOMAPI.AccpacSession
Set mSession = CreateObject(“Accpac.Session”)

Once you have a session object then you need to initialize it:

mSession.Init “”, “XY”, “XY1000”, “61A”

If you are accessing the COM API from an external program and not an SDK application then the parameters don’t matter. The first parameter is for when an SDK application is run from the desktop to connect them up properly and the other parameters are similarly for SDK application for other APIs like getting you applications help files correctly. Generally for an external application you just want these set with valid value so things will proceed. The application ID “XY” is reserved for non-SDK application to use, so you don’t have any risk of having things confused with a third party application. It is important that you call init before doing anything else. If you do call some other method first then expect to get strange error messages.

Below is the object model of all the objects you can get from an initialized session:

objectmodel

Company List

At this point we still haven’t signed into a company. At this point you can really just sign-on, but you can also get a list of companies that you can sign-on to. This is the API used by Sage 300 to build sign-on dialogs. In the session object is an organizations collection that you can traverse to get the information on the available companies.

For i = 0 To mSession.Organizations.Count – 1
Print mSession.Organizations.ItemByIndex(i).DatabaseID,
mSession.Organizations.ItemByIndex(i).Name
Next i

As you can see by the code, this API was invented by a C programmer and not a VB programmer.

Signing On

The main way you sign-on to a company is to use the open method.

mSession.Open “ADMIN”, “ADMIN”, “SAMLTD”, Date, 0, “”

The main thing you need for this method is the user id, password, company id and session date. After calling this, the next thing you usually do is create a database link and then from the database link create your view objects. Now you can call the views and use all the Sage 300 business logic. The disadvantage of this method is that you need to know the user id and password. But otherwise you are good to go.

Session/Signon Managers

Of course with what we have discussed so far you could create your own sign-on dialog. But why re-invent the wheel. The main Sage 300 ERP COM library is intended to be called from both user interface programs or server processes, as a result it has no user interface functions itself, it will never popup a messagebox or a dialog box. It is strictly processing and no UI.

However we do provide a number of other ActiveX controls that are intended to be used as UI components. Two of these are the Signon Manager and the Session Manager. You only interact with the Session Manager and then the Session Manager uses the Signon Manager whenever it needs it.

So if you don’t want to have to know the user id and password then you use the Session Manager to create your session for you and you get back a session that has been created, initialized and opened for you. The user will be able to enter their user id, password and select the company and session date to use for processing.

To use the Sesion Manager you need to add a reference for “ACCPAC Session Manager 1.0” or access the runtime\a4wSessionMgr.dll. Then you would write some code like:

Dim signonID As Long
Dim mSession As AccpacCOMAPI.AccpacSession
Dim sessMgr As New AccpacSessionMgr

sessMgr.AppID = “XY”
sessMgr.ProgramName = “XY1000”
sessMgr.AppVersion = “54A”
sessMgr.CreateSession “”, signonID, mSession

The intent of the session manager was to facilitate things like workflow management. So the first time someone accesses it, it will create new session and the user will get a signon dialog. However the next time it is accessed, you will just get back the session the user opened the first time. This allows applications to be strung together in a workflow type manner without each step requiring the user to sign-on. If you do want a fresh sign-on, you can set the ForceNewSignon property to true. If there are two desktops signed in and ForceNewSignon is false, then the user will get a dialog box to choose which session they want.

Summary

The external APIs to Sage 300 ERP are very powerful. Since the AccpacCOMAPI is used exclusively by our VB forms to access the Sage 300 business logic, you know that from this interface you can do anything that can be done from a regular UI. All business logic is exposed this way. So the intent of this posting was just to give you a little help in getting started to get at all that business logic.

26 Responses

Subscribe to comments with RSS.

  1. […] you read my last blog post, this might appear a bit backwards to the COM API where this looks like the session.Init call that […]

  2. Hi Stephen,
    Good Blog as always. tell me one thing how you can get to know which company to login. For eg. lets say I create a New exe using a standered accpac screen, like AP Invoice entry screen. now if I run that evan without writing a single line of code it will automaticty pick the session of the company already loged in. But if I log into two accpac company and then run the same exe it got confused and ask to login again. so I want to know is how can I use the accpac screen dlls to login to the company from where I open them, if I open it from Company A it opens from company A if I open from comp B it opens from Company B…

    aryadeep

    January 24, 2013 at 5:07 am

    • Unfortunately, right now the only way to do this is as an SDK application. For custom EXEs that use our API there isn’t a way to get the right info from the desktop that launched them. Perhaps we can improve this in the future.

      smist08

      January 24, 2013 at 11:40 pm

  3. […] When we designed the current UI framework, we had the intention that our UIs could be run from many places, such as VBA macros or hosted inside Internet Explorer. We also envisioned them being strung together in workflow type applications. Towards this we created the Session Manager and the Signon Manager to help tie together programs running inside the desktop with programs running outside the desktop. For information on using the Session Manager, check out this blog posting. […]

  4. Hi Stephen, your blog is very resourceful and essential to my understanding of Accpac without being a development partner. I was wondering what the best method to communicate to Accpac with PHP would be? I know that not all functionality for SDATA exist currently and perhaps the COM API would work best with PHP? What are your thoughts, I am trying to choose the best way to integrate our ecommerce website with Accpac. We want to do things like add inventory, create orders, view account balances and view Accpac invoices online.

    Aaron

    July 29, 2013 at 12:12 am

    • I think your best bet is to use the COM API. Then you know you can do anything that Sage 300 supports. Especially if the Sage 300 server and PHP server are in the same location.

      smist08

      July 29, 2013 at 9:27 pm

      • Thanks Stephen. I have done exactly, that PHP and Sage are on the same server. I have established a connection and have signed on. Now I am looking how to enter orders, I know how to record a VBA macro but am not sure how to convert that into COM. Do you know where I can find some COM examples of manipulating views?

        Aaron

        July 31, 2013 at 12:13 am

      • The SDK has some examples. Also checkout this blog posting: https://smist08.wordpress.com/2010/11/07/entering-orders-and-headerdetail-views/

        smist08

        August 3, 2013 at 1:02 am

  5. […] talked a lot about creating sessions in this article, mostly from the COM point of view. Now we’ll recap a bit, but from the .Net point of view. The […]

  6. Hi Stephen,

    I have encountered an odd issue when calling Session.Init. I am using C# and the Sage 300 .NET Libraries. It is sporadic, but what happens is that the call to Session.Init just hangs and never moves forward. My process that is making the Session.Init call doesn’t encounter a hard crash, it just seems to “freeze” and doesn’t come back (or at least, not in a reasonable amount of time — I’ve never given it more than 5 – 10 minutes since there’s no reason it should take more than seconds).

    I realize this isn’t likely something you can simply diagnose, but I am wondering if you have any tips on what I can do to troubleshoot the issue? The challenge here seems to be that since it’s before I even have an open Session, I don’t have the insights to be gained by things like Session.Errors, or any of the various S300 spy tools.

    Thanks.

    Nathan

    September 11, 2015 at 8:48 pm

  7. Hello

    I have a problem about read Accpac Session

    I use ACCPAC COMAPI to call SessionManager My function :

    LogonWithSessionMgr(“”, 1, “XY”, “61A”, “XY0001”, “”)

    I have to specify a SessionID, here the 1 is SessionID .

    if I have 2-3 sessions each session has a different company

    Is there Any Accpac API function could show a screen about all the session I have and let me choose one

    Thanks for help

    rzhang

    September 24, 2015 at 8:05 pm

    • I think if you do it more like in the article above, without specifying an exact session then it will prompt you.

      smist08

      September 24, 2015 at 8:10 pm

      • it a API parameter , I have to feed it

        sessionManager.CreateSession(_objectHandle, ref signOnID,out mySession );

        rzhang

        September 24, 2015 at 8:18 pm

  8. Hello Smist, thank you very much for your documentations and sample code at https://drive.google.com/folderview?id=0B3d1md_6mdBRTlV6dklhVGVwdW8&usp=sharing

    I am just new with Sage 300 from VietNam now – and now try to understand and research by looking at your document and sample code.

    But when I download the source – that build successfully – but I have exception when running

    “An unhandled exception of type ‘System.IO.FileLoadException’ occurred in System.Windows.Forms.dll

    Additional information: Could not load file or assembly ‘ACCPAC.Advantage.Types, Version=6.2.0.0, Culture=neutral, PublicKeyToken=4d7048ecf2312a7c’ or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A)”

    I think this *might* be cause of wrong configuration of the build with 32bits or 64bits – I try to make change on my Vs2015 but got no luck

    Could you please to let me know – is there any thing that I need to do to make it work? Does my machine need to run in same machine with Sage300 – or where i make the configuration to link to my Sage300?

    Thank you very much 🙂
    Regards
    Huy

    Huy

    June 1, 2016 at 3:47 am

    • The project has to be set to 32 bits. The other problem might be version, perhaps try removing and re-adding the references to the two Sage 300 .Net libraries, then they will be set with the correct versions and file locations. File locations shouldn’t really matter since these are added to the GAC. If you don’t see the references to the Accpac libraries then perhaps you didn’t install them, depending on the version of Sage 300 these may or may not be installed by default.

      smist08

      June 1, 2016 at 4:13 pm

      • Hello smist08,

        Yeah, I have it worked now – thank you very much for your great help 🙂

        Regards
        Huy

        Huy

        June 2, 2016 at 2:33 am

  9. Hi Stephen,

    I am using visual studio 2013 using c#, how can I call or what are the ways so that I can use AccpacCOMAPI in calling those views from sage and connecting to sage.

    Thank you.

    Regards,

    Roz

    Rose

    July 19, 2016 at 5:31 pm

    • I think you might want to have a look at the .Net API instead. Have a look at my blog postings on using the Sage 300 .Net API like https://smist08.wordpress.com/2013/10/12/an-introduction-to-the-sage-300-erp-net-api/

      smist08

      July 19, 2016 at 11:16 pm

      • What would be the requirements needed if I am going to create a windows application using c# and integrate the PO data to sage 300 6.0.

        Rose

        July 20, 2016 at 3:38 pm

      • Create your C# project and then add the two Sage 300 .Net libraries as references. They are installed to the GAC so should be easily visible (probably called Accpac.stuff). If you don’t see them run the Sage 300 installation to ensure the .Net libraries were selected as an installation option.

        smist08

        July 20, 2016 at 5:50 pm

  10. Hi Smith,

    I want to convert the code below to c#, can you help me for this. This is coded by vb I run a macro to let me know the process, however it’s not easy for me to convert it. If you can help with this I can handle the other convertion.

    Dim mDBLinkSysRW As AccpacCOMAPI.AccpacDBLink
    Set mDBLinkSysRW = OpenDBLink(DBLINK_SYSTEM, DBLINK_FLG_READWRITE)

    Dim temp As Boolean
    Dim POPOR1header As AccpacCOMAPI.AccpacView
    Dim POPOR1headerFields As AccpacCOMAPI.AccpacViewFields
    mDBLinkCmpRW.OpenView “PO0620”, POPOR1header
    Set POPOR1headerFields = POPOR1header.Fields

    POPOR1header.Compose Array(POPOR1detail2, POPOR1detail1, POPOR1detail3, POPOR1detail4, POPOR1detail5)
    POPOR1detail1.Compose Array(POPOR1header, POPOR1detail2, POPOR1detail4, Nothing, Nothing, POPOR1detail6)

    POPOR1headerFields(“VDCODE”).Value = “10001SAR”
    POPOR1headerFields(“PROCESSCMD”).PutWithoutVerification (“1”)

    POPOR1header.Process
    temp = POPOR1detail1.Exists
    POPOR1detail1.RecordClear
    temp = POPOR1detail1.Exists
    POPOR1detail1.RecordCreate 0

    Regards,

    Jessa

    Jessa

    July 21, 2016 at 12:35 pm

  11. Hi Stephen,

    Urgent help.

    I am using visual studio 2013 using c# and created an intermediate table to migrate the data from one system, then what will I do is to create a program and get all data from the other system and push/integrate it to sage. In order to let me know the process during the posting/creating of PO, I ran the macro in sage 300 6.0 and it showing vb code, can you help me in converting the code below to c#, it would be a big help for me.

    Dim mDBLinkSysRW As AccpacCOMAPI.AccpacDBLink
    Set mDBLinkSysRW = OpenDBLink(DBLINK_SYSTEM, DBLINK_FLG_READWRITE)

    Dim temp As Boolean
    Dim POPOR1header As AccpacCOMAPI.AccpacView
    Dim POPOR1headerFields As AccpacCOMAPI.AccpacViewFields
    mDBLinkCmpRW.OpenView “PO0620”, POPOR1header
    Set POPOR1headerFields = POPOR1header.Fields

    POPOR1header.Compose Array(POPOR1detail2, POPOR1detail1, POPOR1detail3, POPOR1detail4, POPOR1detail5)

    POPOR1headerFields(“VDCODE”).Value = “10001SAR” ‘ Vendor
    POPOR1headerFields(“PROCESSCMD”).PutWithoutVerification (“1”) ‘ Command

    POPOR1header.Process
    temp = POPOR1detail1.Exists
    POPOR1detail1.RecordClear
    temp = POPOR1detail1.Exists
    POPOR1detail1.RecordCreate 0

    Regards,

    Jessa

    Jessa

    July 21, 2016 at 7:34 pm

    • Hi Stephen,

      I am waiting for your reply, hope you can help me this code in this week.

      Regards,

      Jessa

      Jessa

      July 25, 2016 at 5:32 am

      • I’m retired now, so I don’t have a system to play with. I still answer general questions, but I don’t like to answer coding questions without being able to test. Perhaps have a look at some of the samples I’ve posted (none for PO though). Also you might consider hiring one of the Sage 300 3rd party developers, or sign up for the Sage 300 developer program so you can ask programming questions through tech support.

        smist08

        July 25, 2016 at 7:17 pm

      • Hi Stephen,

        Thanks for your response, I managed it already. The connection and integration was okay.

        Regards,

        Jessa

        Jessa

        July 26, 2016 at 11:17 am

  12. Hi Stephen,

    We have 10 max users for Sage 300. What is the best way to ensure that there is always one user available for the Accpac COM Sign-on Session?

    Aaron B

    December 7, 2016 at 12:57 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: