Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
FinancialForce Posting via checkbox
trigger Invoice_PostAction on c2g__codaInvoice__c (after insert, after update) {
List<c2g.CODAAPICommon.Reference> toPost = new List<c2g.CODAAPICommon.Reference>();
if( document.Action_PostDocument__c == true ){
if(document.c2g__invoiceStatus__c != 'In Progress'){
System.debug(LoggingLevel.Warn,'Cannot post invoice, invoice is '+document.c2g__invoiceStatus__c);
c2g.CODAAPICommon.Reference ref = new c2g.CODAAPICommon.Reference(); =;
if(toPost.size() > 0){
List<sObject> postedInvoices = new List<sObject>();
for(c2g.CODAAPICommon.Reference ref:toPost){ //post individually in order to only add errors to actually faled records
new c2g__codaInvoice__c(id =, Action_PostDocument__c = false)
}catch(Exception e){
Trigger.newMap.get('Failed to post document: '+e.getMessage());
update postedInvoices; //uncheck the action: post box
private class PostAction_Test{
static final boolean softFail = false; //if true tests will fail softly - that is they will not be marked as failed but may not contribute full code coverage
static void invoiceTest(){
Account customerAccount = [SELECT id FROM Account WHERE c2g__CODAAccountsReceivableControl__c != null LIMIT 1];
Product2 product = [SELECT FROM PricebookEntry WHERE product2.c2g__CODASalesRevenueAccount__c != null AND product2.isActive = true LIMIT 1].product2;
c2g__codaInvoice__c newInvoice = new c2g__codaInvoice__c(
c2g__Account__c =,
c2g__InvoiceDate__c =
insert newInvoice;
List<c2g__codaInvoiceLineItem__c> newInvoiceLines = new List<c2g__codaInvoiceLineItem__c>{
new c2g__codaInvoiceLineItem__c(
c2g__Invoice__c =,
c2g__Product__c =
insert newInvoiceLines;
//Post it!
update new c2g__codaInvoice__c(id =, Action_PostDocument__c = true);
}catch(Exception e){
System.debug(LoggingLevel.Error,'Soft fail enabled. Test marked as passed but it really failed with '+e.getMessage());
throw e;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment