C++ API

From RangerMSP Wiki - PSA software for MSPs and IT services providers
Jump to navigation Jump to search

Disclaimer

This documentation refers to CommitCRM 5.6 or later version and assumes CommitCRM is installed and accessible on the local computer.

Introduction

This document demonstrates how to use the CommitCRM C++ API library in order to programmatically connect to your CommitCRM server and query or manipulate the CommitCRM database.


System Requirements

  • CommitCRM 5.6 or later.
  • Visual C++ 2008 or Visual C++ 2010.
  • CommitLib C++ header files.
  • CommitLib.lib (the CommitCRM C++ static library).


Getting Started

After you create your C++ project, you'll need to add the CommitCRM Source folder to your include path and link with the CommitLib.lib static library file, in order to have access to the CommitCRM library classes.

Each application using the library will have to initialize on startup the CommitCRM::Application object and terminate it on exit. Initialization method requires that you pass an object instance of CommitCRM::Config class configured with the following settings:

  • AppName
This is free text, preferably the name of your application.
  • CommitDllFolder
Behind the scenes the library uses the two CommitCRM API dlls: CmtDbEng.dll and CmtDbQry.dll.
In the default CommitCRM installation you'll find these files in 'C:\\Commit\\ThirdParty\\UserDev'.
Important Note: Always point to this folder and do not copy the dll files elsewhere. This is because when the CommitCRM version upgrade runs it also upgrades the dll files stored in this folder. This verifies that you will always be using the latest release.
  • CommitDbFolder
Path to the CommitCRM database, default is 'C:\\Commit\\db'.

Assuming these default values, we can configure the CommitCRM::Config object like this:

CommitCRM::Config config;
config.AppName = "C# Demo";
config.CommitDllFolder = "C:\\Commit\\ThirdParty\\UserDev";
config.CommitDbFolder = "C:\\Commit\\db";

You should of course check where these paths are exactly on your disk and modify these values accordingly.

Now we can initialize the CommitCRM::Application object with these settings:

CommitCRM::Application::Initialize(config);

If anything goes wrong, the above line will throw an exception of the CommitCRM::Exception class. To prevent unexpected termination of the program execution, we recommend having any call to the CommitCRM library enclosed in a try/catch block.

Before exit, we terminate the CommitCRM::Application object:

CommitCRM::Application::Terminate();

The most basic C++ application that just connects to CommitCRM and terminates could look something like this:

try
{
    CommitCRM::Config config;
    config.AppName = "C# Demo";
    config.CommitDllFolder = "C:\\Commit\\ThirdParty\\UserDev";
    config.CommitDbFolder = "C:\\Commit\\db";

    CommitCRM::Application::Initialize(config);

    //At this point we have successfully initialized the CommitCRM.Application
    //and can start using the other library classes
}
catch (CommitCRM::Exception exc)
{
    std::cerr << exc.error() << std::endl;
}
CommitCRM::Application::Terminate();

Now that we have confirmed the connectivity to the CommitCRM server (if the above code successfully runs), we can continue adding more functionality to the example.

The library exposes as C++ classes the same CommitCRM objects (Account, Ticket etc.) available through the native CommitCRM API and you can refer to the API Reference Manual for database fields reference.


With any of these objects you can:

  • Search and query for objects with CommitCRM::ObjectQuery that satisfy certain criteria.
  • Read and display the properties of the retrieved objects.
  • Update and save the properties of the retrieved objects.
  • Create and save new objects.



Now let's see how we can search for CommitCRM::Account objects. We instantiate an object of the CommitCRM::ObjectQuery class and pass CommitCRM::Account class as generics parameter.

CommitCRM::ObjectQuery<CommitCRM::Account> accountSearch;

CommitCRM::ObjectQuery class can accept any of the CommitCRM objects in this parameter, but we want to search for accounts now.

Next, we need to set criteria (or more than one) we want to search for:

accountSearch.AddCriteria(CommitCRM::Account::Fields::City, CommitCRM::opEqual, "New York");

Тhe first parameter to the AddCriteria method is either a static object instance of CommitCRM.CmtField class representing the field we want to look in or the internal API field name. Refer to the API Field Name column in the Account Class table for a complete list of the available fields for the CommitCRM::Account class.

The second parameter is a compare operator. We here use the CommitCRM::OperatorEnum::opEqual to get only exact matches. In order to get a broader match in the results you can use CommitCRM::OperatorEnum::opLike operator.

The third parameter is the value we want to find. Prepending and/or appending % (percent) sign at the beginning and/or at the end while using CommitCRM::OperatorEnum::opLike operator, will match the phrase even if in the middle of a sentence.

Now we can execute the search and retrieve the CommitCRM::Account objects (if any):

CommitCRM::ObjectQuery<CommitCRM::Account>::COLLECTION accounts = accountSearch.FetchObjects();

The above line will populate a list with all CommitCRM::Account objects that were found. Now we can iterate through the accounts like this:

CommitCRM::ObjectQuery<CommitCRM::Account>::COLLECTION::iterator itAccount = accounts.begin();
while (itAccount != accounts.end())
{
    std::cout << account.CompanyName() << "\r\n";
    ++itAccount;
}

Or we can manipulate these accounts:

CommitCRM::ObjectQuery<CommitCRM::Account>::COLLECTION::iterator itAccount = accounts.begin();
while (itAccount != accounts.end())
{
    if (account.Zip().empty())
    {
         account.Zip("10001");
         account.Save();
    }

    ++itAccount;
}


We invoke the CommitCRM::Account's Save method on both new or existing accounts. For a new account, invoking the Save method would insert a new account in the CommitCRM database. For an existing account, invoking the Save method would update the fields we modified in the existing account. This rule applies to all CommitCRM objects.

Another option is to add a new ticket for each of the accounts:

CommitCRM::ObjectQuery<CommitCRM::Account>::COLLECTION::iterator itAccount = accounts.begin();
while (itAccount != accounts.end())
{
    CommitCRM::Ticket ticket;
    ticket.AccountREC_ID(account.AccountREC_ID());
    ticket.Description("Sample ticket for a NewYork account");
    ticket.Save();

    ++itAccount;
}

GetFieldValue and SetFieldValue methods

Each of the CommitCRM library objects have a set of methods that are exposed as C++ getters/setters methods that you can use to manipulate or read the data. You already saw few examples of these methods in the above examples, as: account.Zip or ticket.Description. This is the preferred and more intuitive way of accessing the CommitCRM fields.


However, there is also another way of achieving the same results, by invoking GetFieldValue and SetFieldValue and specifying the internal field name. These methods should only be used if necessary, for example, when updating user-defined custom fields which are not part of the class predefined basic fields.

Here is an equivalent of the above example that uses these two generic methods, instead of the object's getters/setters methods:

CommitCRM::ObjectQuery<CommitCRM::Account>::COLLECTION::iterator itAccount = accounts.begin();
while (itAccount != accounts.end())
{
    CommitCRM::Ticket ticket;
    ticket.SetFieldValue("FLDTKTCARDID", account.GetFieldValue("FLDCRDRECID"));
    ticket.SetFieldValue("FLDTKTPROBLEM", "Sample ticket for a NewYork account");
    ticket.Save();

    ++itAccount;
}

All internal field names are listed in Classes and Objects below.

Exception Handling

While working with the CommitCRM C++ library, some operations can fail. In this case the library will throw an exception of the CommitCRM::Exception class. We recommend enclosing all calls to the CommitCRM library in a try/catch block.

To find out more about the exact error that ocured when an exception is thrown, you can use the CommitCRM::Exception::Status method that returns the last CommitCRM Status value, or use the CommitCRM::Exception.Codes method to return the list of last codes (if any). Please refer to Error Codes Description for the description of these values.

Complete Program Sample

#include "stdafx.h"
#include <iostream>

#include <CommitLib/CommitCRM/Application.h>
#include <CommitLib/CommitCRM/Account.h>
#include <CommitLib/CommitCRM/Ticket.h>
#include <CommitLib/CommitCRM/Charge.h>
#include <CommitLib/CommitCRM/Item.h>
#include <CommitLib/CommitCRM/QueryCommand.h>
#include <CommitLib/Exception.h>

#include <CommitLib/Helper.h>
#include <CommitLib/IO/File.h>

int _tmain(int argc, _TCHAR* argv[])
{
    try
    {
        CommitCRM::Config config;
        config.AppName = "C++ Demo";
        config.CommitDllFolder = "C:\\Commit\\ThirdParty\\UserDev";
        config.CommitDbFolder = "C:\\Commit\\db";

        CommitCRM::Application::Initialize(config);

        //At this point we have successfully initialized the CommitCRM.Application
        //and can start using the other library classes

        CommitCRM::ObjectQuery<CommitCRM::Account> accountSearch;
        accountSearch.AddCriteria(CommitCRM::Account::Fields::City, CommitCRM::opEqual, "New York");
        CommitCRM::ObjectQuery<CommitCRM::Account>::COLLECTION accounts = accountSearch.FetchObjects();

        CommitCRM::ObjectQuery<CommitCRM::Account>::COLLECTION::iterator itAccount = accounts.begin();
        while (itAccount != accounts.end())
        {
            std::cout << account.CompanyName() << "\r\n";
            ++itAccount;
        }
    }
    catch (CommitCRM::Exception exc)
    {
        //we can inspect the Commit status (exc.Status) 
        //refer to Error Codes Description
        //exc.Codes contains all error codes last call generated
        //also refer to Error Codes Description
        std::cerr << exc.error() << std::endl;
    }

    //Terminate connection with CommitCRM
    CommitCRM::Application::Terminate();

    return 0;
}

Classes and Objects

The CommitCRM C++ API provides easy access to all objects using a class for each entity in the system (such as Account, Ticket, Asset, etc.).

All classes include:

  1. API functions and routines (which are derived from the base CommitCRM::Object class).
  2. Class data fields, providing an easy way to update the object's data.


All CommitCRM's objects should be updated by setting the class data fields (through setter metthods) and then calling the Save method. For example, to create a new account programmatically (assuming CommitCRM::Application was initialized successfully), one would use the following code snippet:

CommitCRM::Account account;
account.FileAs("ACME Company");
account.Dear("Mr.");
account.Contact("John Doe");
account.Save();


Another option is using SetFieldData and GetFieldData as mentioned earlier, which provide low-level access to data fields. This option should only be used if necessary, for example, when updating user-defined fields which are not part of the class basic fields.