Skip to content

Instantly share code, notes, and snippets.

@capeterson
Created August 22, 2012 02:57
Show Gist options
  • Save capeterson/3421844 to your computer and use it in GitHub Desktop.
Save capeterson/3421844 to your computer and use it in GitHub Desktop.
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>();
for(c2g__codaInvoice__c document:Trigger.new){
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);
}else{
c2g.CODAAPICommon.Reference ref = new c2g.CODAAPICommon.Reference();
ref.id = document.id;
toPost.add(ref);
}
}
}
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
postedInvoices.add(
new c2g__codaInvoice__c(id = ref.id, Action_PostDocument__c = false)
);
try{
c2g.CODAAPISalesInvoice_6_0.PostInvoice(null,ref);
}catch(Exception e){
Trigger.newMap.get(ref.id).addError('Failed to post document: '+e.getMessage());
}
}
update postedInvoices; //uncheck the action: post box
}
}
@isTest
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
@isTest(seeAllData=true)
static void invoiceTest(){
try{
Account customerAccount = [SELECT id FROM Account WHERE c2g__CODAAccountsReceivableControl__c != null LIMIT 1];
Product2 product = [SELECT product2.id 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 = customerAccount.id,
c2g__InvoiceDate__c = Date.today()
);
insert newInvoice;
List<c2g__codaInvoiceLineItem__c> newInvoiceLines = new List<c2g__codaInvoiceLineItem__c>{
new c2g__codaInvoiceLineItem__c(
c2g__Invoice__c = newInvoice.id,
c2g__Product__c = product.id
)
};
insert newInvoiceLines;
//Post it!
update new c2g__codaInvoice__c(id = newInvoice.id, Action_PostDocument__c = true);
}catch(Exception e){
if(softFail)
System.debug(LoggingLevel.Error,'Soft fail enabled. Test marked as passed but it really failed with '+e.getMessage());
else
throw e;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment