Accounting Apex API Developer Reference

c2g.SalesInvoiceEInvoicingService

global with sharing class SalesInvoiceEInvoicingService

API methods for sending a sales invoice via the configured electronic invoicing provider. Sending a sales invoice will update the record to set fields used for tracking the progress and make a callout to the configured electronic invoicing provider. Because of platform restrictions around making callouts, there are multiple API methods covering different parts of this process to allow you to split the work across multiple execution contexts as needed.

Methods

sendAsync

global static c2g.SalesInvoiceEInvoicingService.SendAsyncResponse sendAsync(Set<Id> salesInvoiceIds)

Performs all operations needed to send electronic invoices from sales invoices. The sales invoices will be updated synchronously to mark them as sending, and a queueable will be started that makes the callout to the electronic invoicing provider asynchronously. Because this method starts a queueable, take care using it from code that may be called from a batch or queueable process where there are tight limits on the number of queueables that can be started. Consider using the `prepareToSend` and `sendPreparedInvoices` methods instead.

Input Parameters

Name Type Description
salesInvoiceIds Set<Id> The IDs of the sales invoices that are to be sent as electronic invoices.

Return Value

An object containing the ID of the AsyncApexJob started by this method.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

Set<Id> salesInvoiceIds = new Set<Id>{ 'XYZ' };
SalesInvoiceEInvoicingService.SendResponse result = SalesInvoiceEInvoicingService.sendAsync(
  salesInvoiceIds
);
Id asyncApexJobId = result.AsyncApexJobId;

//Checking results in another execution context
AsyncApexJob job = [
  SELECT Status, ExtendedStatus, CompletedDate
  FROM AsyncApexJob
  WHERE Id = :asyncApexJobId
];

prepareToSend

global static c2g.SalesInvoiceEInvoicingService.PrepareToSendResponse prepareToSend(Set<Id> salesInvoiceIds)

Updates sales invoices to prepare them for sending. The invoices will be marked as sending and their Electronic Invoice Unique Identifier fields will be set if they have not been already. This method will not send the electronic invoices to the electronic invoicing provider. That must be done separately using either `send` or `sendPreparedInvoices`.

Input Parameters

Name Type Description
salesInvoiceIds Set<Id> The IDs of the sales invoices that are to be prepared for sending.

Return Value

An object containing information about the execution of the request.

sendPreparedInvoices

global static c2g.SalesInvoiceEInvoicingService.SendResponse sendPreparedInvoices(Set<Id> salesInvoiceIds)

Turns sales invoices into electronic invoices and sends them to the electronic invoicing provider immediately. This method will make a callout for each sales invoice that it is given, so make sure that it is only called in an execution context that allows callouts. Invoices submitted to this method must be prepared for sending first, using the `prepareToSend` method.

Input Parameters

Name Type Description
salesInvoiceIds Set<Id> The IDs of the sales invoices that are to be sent as electronic invoices.

Return Value

An object containing information about the execution of the request.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

public class ExistingProcess implements Queueable {
    public void execute(QueueableContext context) {
        System.enqueueJob(new ChainedJob());
        Set<Id> salesInvoiceIds = new Set<Id>{'XYZ'};
        //Cannot make a callout or start a queueable from this process, so we can only prepare invoices to send
        SalesInvoiceEInvoicingService.prepareToSend(salesInvoiceIds);
    }
}

//Separate batch job to send invoices after they have been prepared. Note how this is configured to
//allow callouts.
public class SendPreparedInvoices implements Database.Batchable<SObject>, Database.AllowsCallouts {
    public Database.QueryLocator start(Database.BatchableContext ctx) {
        return Database.getQueryLocator('SELECT Id FROM c2g__codaInvoice__c WHERE c2g__EInvoiceDeliveryStatus__c = \'Sending\'');
    }

    public void execute(Database.BatchableContext ctx, List<SObject> records) {
        Set<Id> salesInvoiceIds = new Map<Id, SObject>(records).keySet();
        SalesInvoiceEInvoicingService.sendPreparedInvoices(salesInvoiceIds);
    }

    public void finish(Database.BatchableContext ctx) {

    }

}

validateSalesInvoices

global static c2g.SalesInvoiceEInvoicingService.ValidationResponse validateSalesInvoices(Set<Id> salesInvoiceIds)

Returns information about the validity of the sales invoices for sending as electronic invoices. For example if the sales invoice status is not Complete or the delivery status is already Delivered.

Input Parameters

Name Type Description
salesInvoiceIds Set<Id> The IDs of the sales invoices that are to be validated.

Return Value

An object containing information about the validity of the sales invoices.

c2g.SalesInvoiceEInvoicingService.EInvoicingResponse

global abstract with sharing class EInvoicingResponse

Abstract base class that all other e-invoicing response classes extend. Customers are not expected to extend this themselves, but you can refer to other responses using this type to build common error-handling code. Provides a map of errors by record id.

Properties

Name Type Description
Errors Map<Id, List<String>> A map from an invoice's Id to a list of errors associated with that invoice.

c2g.SalesInvoiceEInvoicingService.SendAsyncResponse

global with sharing class SendAsyncResponse extends EInvoicingResponse

Response to a request to the `sendAsync` method. This contains information about what has happened when executing `sendAsync`.

This class extends c2g.SalesInvoiceEInvoicingService.EInvoicingResponse

Properties

Name Type Description
AsyncApexJobId Id Id of the AsyncApexJob that has been created to send the e-invoice. This can be used to track the progress of the job.

Methods

SendAsyncResponse

global SendAsyncResponse(Id asyncApexJobId)

Constructs a SendAsyncResponse.

Input Parameters

Name Type Description
asyncApexJobId Id Id of the AsyncApexJob that has been created to send the e-invoice.

SendAsyncResponse

global SendAsyncResponse(Map<Id, List<String>> errorsById)

Constructs a SendAsyncResponse.

Input Parameters

Name Type Description
errorsById Map<Id, List<String>> Map of errors by invoice Id.

c2g.SalesInvoiceEInvoicingService.PrepareToSendResponse

global with sharing class PrepareToSendResponse extends EInvoicingResponse

Response to a request to the `prepareToSend` method. This contains information about what has happened when executing `prepareToSend`. This response currently contains no information, and is present as a placeholder to allow us to add information to the response in future.

This class extends c2g.SalesInvoiceEInvoicingService.EInvoicingResponse

Methods

PrepareToSendResponse

global PrepareToSendResponse()

Constructs a PrepareToSendResponse with no errors set.

PrepareToSendResponse

global PrepareToSendResponse(Map<Id, List<String>> errorsByIds)

Constructs a PrepareToSendResponse.

Input Parameters

Name Type Description
errorsByIds Map<Id, List<String>> Map of errors by invoice Id.

c2g.SalesInvoiceEInvoicingService.SendResponse

global with sharing class SendResponse extends EInvoicingResponse

Response to a request to the `sendPreparedInvoices` method. This contains information about what has happened when executing `sendPreparedInvoices`. This response currently contains no information, and is present as a placeholder to allow us to add information to the response in future.

This class extends c2g.SalesInvoiceEInvoicingService.EInvoicingResponse

Methods

SendResponse

global SendResponse()

Constructs a SendResponse with no errors set.

SendResponse

global SendResponse(Map<Id, List<String>> errorsById)

Constructs a SendResponse.

Input Parameters

Name Type Description
errorsById Map<Id, List<String>> Map of errors by invoice Id.

c2g.SalesInvoiceEInvoicingService.ValidationResponse

global with sharing class ValidationResponse extends EInvoicingResponse

Returns information on the validity of sales invoices.

This class extends c2g.SalesInvoiceEInvoicingService.EInvoicingResponse

Properties

Name Type Description
IsValid Boolean A boolean describing the validity of all sales invoices. This will be false if one or more of the invoices are invalid.

Methods

ValidationResponse

global ValidationResponse(Map<Id, List<String>> errorMap)

ValidationResponse

global ValidationResponse()

© Copyright 2009–2022 FinancialForce.com, inc. All rights reserved. Various trademarks held by their respective owners.