Accounting API Developer's Reference
Writing Apex Tests

Writing Apex Tests

When developing against the API you will likely be doing one or more of the following types of API integrations:

  • Use Case 1: Calling an FFA API directly to perform a task
  • Use Case 2: Calling a Salesforce DML API directly to perform a record operation
  • Use Case 3: Implementing a custom Apex Trigger

This code sample illustrates how to setup a basic environment within an Apex Test, to allow this type of code to be covered and tested as per the platform requirements and best practices.


The CODAYearWebService and CODACompanyWebService classes are only supported in Apex Test context in accordance with this example.

Code Sample

private class CreateTransactionTest {
private static void createTransaction() {
// Setup Company and User association within runAs to avoid mixed DML restriction
Group companyGroup = null;
System.runAs([select Id from User where Id = :UserInfo.getUserId()][0]) {
// Create Company
c2g__codaCompany__c company = new c2g__codaCompany__c();
company.Name = 'ApexTestCompany';
company.RecordTypeId = Schema.SObjectType.c2g__codaCompany__c.RecordTypeInfosByName.get('VAT').RecordTypeId;
insert company;
// Create Company Queue
c2g.CODACompanyWebService.createQueue(company.Id, 'USD', company.Name);
// Activate the Company
c2g.CODAYearWebService.calculatePeriods(null); // Workaround to bug in company API's, safe to remain once fixed
c2g.CODACompanyWebService.activateCompany(company.Id, 'USD', company.Name);
// Assign the User to the Company
c2g__codaUserCompany__c userCompany = new c2g__codaUserCompany__c();
userCompany.c2g__Company__c = company.Id;
userCompany.c2g__User__c = UserInfo.getUserId();
insert userCompany;
// Assign the User to the Company Queue
String queueName = 'FF ' + company.Name;
companyGroup = [select Id from Group where Name = :queueName And Type = 'Queue'];
insert new GroupMember( GroupId = companyGroup.Id, UseroRGroupId = UserInfo.getUserId());
// Assert user logged into the company
System.assertEquals('ApexTestCompany', c2g.CODAAPICompany_2_0.GetCurrentCompanies(null)[0]);
// Create Year and Periods
c2g__codaYear__c yr = new c2g__codaYear__c();
yr.Name = String.valueOf(;
yr.OwnerId = companyGroup.Id;
yr.c2g__NumberOfPeriods__c = 12;
yr.c2g__AutomaticPeriodList__c = true;
yr.c2g__StartDate__c = Date.valueOf( + '-01-01 00:00:00');
yr.c2g__EndDate__c = Date.valueOf( + '-12-31 00:00:00');
yr.c2g__PeriodCalculationBasis__c = 'Month End';
insert yr;
// Create Accounting Currency?
if(UserInfo.isMultiCurrencyOrganization()) {
c2g__codaAccountingCurrency__c testCurrency = new c2g__codaAccountingCurrency__c();
testCurrency.Name = 'USD';
testCurrency.c2g__DecimalPlaces__c = 2;
testCurrency.c2g__Home__c = true;
testCurrency.c2g__Dual__c = true;
insert testCurrency;
// Create General Ledger Account
c2g__codaGeneralLedgerAccount__c gla = new c2g__codaGeneralLedgerAccount__c();
gla.Name = 'Brochures';
gla.c2g__ReportingCode__c = 'xyz1';
gla.c2g__Type__c = 'Balance Sheet';
insert gla;
// Create and Post a test Journal
c2g__codaJournal__c journal = new c2g__codaJournal__c();
journal.c2g__JournalDate__c =;
journal.OwnerId = companyGroup.Id;
insert journal;
c2g__codaJournalLineItem__c journalLine1 = new c2g__codaJournalLineItem__c();
journalLine1.c2g__LineType__c = 'General Ledger Account';
journalLine1.c2g__Journal__c = journal.Id;
journalLine1.c2g__GeneralLedgerAccount__c = gla.Id;
journalLine1.c2g__Value__c = 42;
c2g__codaJournalLineItem__c journalLine2 = new c2g__codaJournalLineItem__c();
journalLine2.c2g__LineType__c = 'General Ledger Account';
journalLine2.c2g__Journal__c = journal.Id;
journalLine2.c2g__GeneralLedgerAccount__c = gla.Id;
journalLine2.c2g__Value__c = -42;
insert new List { journalLine1, journalLine2 };
c2g.CODAAPIJournal_12_0.PostJournal(null, c2g.CODAAPICommon.getRef(journal.Id, null));
System.assertEquals(0, [select c2g__DocumentTotal__c from c2g__codaTransaction__c LIMIT 1][0].c2g__DocumentTotal__c);
} }

IMPORTANT NOTE #1: The above example uses the Salesforce API to create Journals. For this part you must have the FinancialForce Journal Extension package installed if you are using FinancialForce Accounting prior to V14. From V14 the Journal extension functionality is merged into the core package so there is no separate extension package to install.

IMPORTANT NOTE #2: The above code is only supported from v13.2 onwards. As a workaround on earlier versions, when needing to cover code in a custom Apex Trigger (as per Use Case 3) consider leveraging an Apex Test like the one below. However once upgraded you should move to the recommended approach above. Unfortunately there are no workarounds on earlier versions for Use Cases 1 and 2.

private class UpdateTransactionTest {
// This test is designed to allow for coverage and testing of custom Apex Triggers
// (For v13.2 onwards, please see API documentation for an example not requiring SeeAllData)
private static void createTransaction() {
List records = [select Id from c2g__codaTransaction__c limit 1];
System.assert(1 == records.size(),
'You must have at least one transaction in the system for this test to work');
update records;