Sep 20, 2024
88 Views
Comments Off on Discard Invoices Of FinancialForce through Api
0 0

Discard Invoices Of FinancialForce through Api

Written by

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() );}}

Article Categories:
Coding and Scripting
Ramya Singh
https://thestarbiznews.com

Ramya Singh isn't your average tech blogger. Sure, she's got the brains to understand the latest algorithms and the jargon to explain them in plain English. But she's also got a twinkle in her eye and a way of weaving technology into the fabric of everyday life that makes it nothing short of fascinating. Whether she's reviewing the latest smartphone, exploring the potential of virtual reality, or delving into the ethical implications of artificial intelligence, Ramya does it with a contagious enthusiasm that makes you want to learn more, do more, and be a part of the exciting world of tech. So, if you're looking for a tech blog that's informative, inspiring, and just plain fun, follow Ramya Singh. She'll take you on a journey through the ever-evolving landscape of technology, and you might just find yourself a little bit more tech-savvy (and a lot more excited) along the way.