It’s the Sample code of Discarding FinancialForce Invoices through API .
While this isn’t an official Mass Discard API, we have got this to work. It is essentially discarding one at a time but in a batch method
public class InvoiceDiscardHelper implements IBatch
{
private Map<String,List> statefulMessages;
// —————————————————————–
// @name: InvoiceDiscardHelper
// @description:default constructor
// —————————————————————–
public InvoiceDiscardHelper()
{
this.statefulMessages = new Map<String,List> { ‘Error’=>new List(),’Warning’=>new List(), ‘Success’=>new List(), ‘General’=>new List() };
this.statefulMessages.get(‘General’).add(‘FinancialForce Invoice Discard’ );
this.statefulMessages.get(‘General’).add(‘——————————‘ );
this.statefulMessages.get(‘General’).add(‘Start Timestamp: ‘ + System.now() );
}
// —————————————————————–
// @name: start
// @description:implements IBatch.start
// —————————————————————–
public Object start( BatchJob job )
{
Set filter = job.getFilterSet();
Id recordId;
for( Id filterId : filter )
{
recordId = filterId;
}
String invoiceObject = recordId.getSObjectType().getDescribe().getName();
String query = ‘ Select Id, Name From ‘+ invoiceObject +’ Where Id IN :filter’;
return Database.getQueryLocator( query );
}
// —————————————————————–
// @name: execute
// @description:implementas IBatch.execute
// —————————————————————–
public void execute( BatchJob job, List scope ){String discardReason = (String)job.getState().get(‘Reason’);c2g.CODAAPICommon_6_0.Context context = new c2g.CODAAPICommon_6_0.Context();for( SObject record : (List)scope ){String name = (String)record.get(‘Name’);c2g.CODAAPICommon.Reference ref = new c2g.CODAAPICommon.Reference();ref.Id = record.Id;try{if( record instanceof c2g__codaInvoice__c ){delete [ Select Id From c2g__codaInvoiceLineItem__c Where c2g__Invoice__c = :record.Id ];// To delete related Line Itemsc2g.CODAAPIInvoiceTypes_6_0.Invoice invoice = c2g.CODAAPISalesInvoice_6_0.GetInvoice( context, ref );invoice.DiscardReason = discardReason;invoice.InvoiceStatus = c2g.CODAAPIInvoiceTypes_6_0.enumInvoiceStatus.Discarded;c2g.CODAAPISalesInvoice_6_0.UpdateInvoice( context, invoice );}else if( record instanceof c2g__codaPurchaseInvoice__c ){delete [ Select Id From c2g__codaPurchaseInvoiceLineItem__c Where c2g__PurchaseInvoice__c = :record.Id ];// To delete related Line Itemsdelete [ Select Id From c2g__codaPurchaseInvoiceExpenseLineItem__c Where c2g__PurchaseInvoice__c = :record.Id ];// To delete related Line Itemsc2g.CODAAPIPurchaseInvoiceTypes_6_0.PurchaseInvoice invoice = c2g.CODAAPIPurchaseInvoice_6_0.GetPurchaseInvoice( context, ref );invoice.DiscardReason = discardReason;invoice.InvoiceStatus = c2g.CODAAPIPurchaseInvoiceTypes_6_0.enumInvoiceStatus.Discarded;c2g.CODAAPIPurchaseInvoice_6_0.UpdatePurchaseInvoice( context, invoice );}statefulMessages.get(‘Success’).add(‘Discarded [‘+name+’] with reason [‘+discardReason+’].’);}catch(Exception ex){String error = ‘An error occurred updating [‘+name+’]. Error: ‘+ex.getMessage();List errorMessages = c2g.CODAExtensionContext.getErrorMessages();for( String errorMessage : errorMessages ){if( errorMessage != null ) error += ‘; ‘ + errorMessage;}statefulMessages.get(‘Error’).add(error);}}}// —————————————————————–// @name: finish// @description:implements IBatch.finish// —————————————————————–public void finish( BatchJob job ){this.statefulMessages.get(‘General’).add(‘End Timestamp: ‘ + System.now() );}}
Leave a comment