Dear Team,
I want to restrict Invoice Group to be posted on the basis of condition.
Condition is, if my created UDF is true for all the invoices under the same group then should be posted else system should throw error.
I have written code which is working fine for one invoice but not working for multiple invoices under the same group.
foreach (var InvcGrp_iterator in (from InvcGrp_Row in ds.InvcGrp
where (string.Equals(InvcGrp_Row.RowMod, IceRow.ROWSTATE_ADDED, StringComparison.OrdinalIgnoreCase)) || (string.Equals(InvcGrp_Row.RowMod, IceRow.ROWSTATE_UPDATED, StringComparison.OrdinalIgnoreCase))
select InvcGrp_Row))
{
var InvcGrp1 = InvcGrp_iterator;
InvcHead = (from InvcHead_Row in Db.InvcHead
where string.Compare(InvcHead_Row.Company,InvcGrp1.Company, true) == 0 && InvcHead_Row.InvoiceNum != 0 && InvcHead_Row.GroupID == InvcGrp1.GroupID select InvcHead_Row).FirstOrDefault();
if(InvcHead != null)
{
Einvoice = InvcHead.Generate_e_Invoice_c;
TranDoc = InvcHead.TranDocTypeID;
}
if ( TranDoc == "ARGD" && Einvoice == false )
{
//Einvoice = true ;
//this.PublishInfoMessage("Einv is false ", Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "", "");
// }
//if ( Einvoice == false )
// {
object THROW_PRIVATE = null;
CallContext.Current.ExceptionManager.AddBLException("E- Invoice has not been generated , please check invoices");
THROW_PRIVATE = null;
}
}
Note: I have fixed your post above… if you include three tick marks ``` before and after your code, then the code shows in a code block.
I also have cleaned up the formatting so that it is more readable. Here are the results (I made no material changes… just aligned the code:
(string.Equals(InvcGrp_Row.RowMod, IceRow.ROWSTATE_ADDED, StringComparison.OrdinalIgnoreCase)) ||
(string.Equals(InvcGrp_Row.RowMod, IceRow.ROWSTATE_UPDATED, StringComparison.OrdinalIgnoreCase))
select InvcGrp_Row)) {
var InvcGrp1 = InvcGrp_iterator;
InvcHead = (from InvcHead_Row in Db.InvcHead where
string.Compare(InvcHead_Row.Company,InvcGrp1.Company, true) == 0 &&
InvcHead_Row.InvoiceNum != 0 &&
InvcHead_Row.GroupID == InvcGrp1.GroupID
select InvcHead_Row).FirstOrDefault();
if (InvcHead != null) {
Einvoice = InvcHead.Generate_e_Invoice_c;
TranDoc = InvcHead.TranDocTypeID;
}
if ( TranDoc == "ARGD" && Einvoice == false ) {
object THROW_PRIVATE = null;
CallContext.Current.ExceptionManager.AddBLException("E- Invoice has not been generated , please check invoices");
THROW_PRIVATE = null;
}
}
But… now that the code is aligned, and I have analyzed, I have a bunch of questions. First one: Where is this BPM attached? What business object? is it Pre or post processing (or is this a data bpm that is in-trans or standard)?
I also don’t see you itterating through the DETAILS of the invoice, yet you state that you are looking at all the invoices.
AS AN ALTERNATIVE: I have created a new code base below… THIS IS NOT TESTED AND WILL NOT WORK (until you adjust it to your needs).
Note that there is no “foreach” because we can let SQL do the work for us by having it do a query.
I also through in a “.any” query which returns a true/false if it finds or doesn’t find a condition… SInce I don’t know what our Invoice DETAIL conditions are, you will need to fill in these details.
//first lets get the curent GroupID from the record
var myInvcGroup = ds.InvcGrp.Where(x=>
x.RowMod == IceRow.ROWSTATE_Added ||
x.RowMod == IceRow.ROWSTATE_UPDATED).FirstOrDefault();
)
if (myInvcGroup != null) {
//lets get the invoice header
var myInvoiceHeader = Db.InvcHead.Where(x=>
x.Company == myInvcGroup.Company).FirstOrDefault()
)
if (myInvoiceHeader != null){
//now lets verify that ALL invoices in the group have the flag flipped
bool badInvoiceFound = Db.InvcDtl.Any(x=>
x.Company == myInvcGroup.Company &&
x.MYFLAG == false)
if (badInvoiceFound) {
//there was a bad invoice found... throw exception
object THROW_PRIVATE = null;
CallContext.Current.ExceptionManager.AddBLException("E- Invoice has not been generated , please check invoices");
THROW_PRIVATE = null;
}
}
}
That’s all I got for now… I need to get back to work, prepping for Insights in Prague next week.
You’re just taking the first Invoice of each group.
You could replace FirstOrDefault with Any and put your condition inside:
var thisGroupInvoices = //separating the query out for readability
(from InvcHead_Row in Db.InvcHead
where string.Compare(InvcHead_Row.Company,InvcGrp1.Company, true) == 0 && InvcHead_Row.InvoiceNum != 0 && InvcHead_Row.GroupID == InvcGrp1.GroupID
select InvcHead_Row)
if (thisGroupInvoices
.Any(invcHead => invcHead.TranDocTypeID == "ARGD" && invcHead.Generate_e_Invoice_c == false)) // If any invoices in this group come back false, then throw error
{
object THROW_PRIVATE = null;
CallContext.Current.ExceptionManager.AddBLException("E- Invoice has not been generated, please check invoices");
THROW_PRIVATE = null;
}