Hi Group,
I am writing a Epicor function where I am passing a EmpID, StartDate and EndDate as a parameter. I need to return the SUM value of the LaborHrs column from the LaborDtl table by ranging on the EmpID and Date range. Please see the attached picture. Can someone help me writing the C# code or if any other way of achieving this.
something like this will have sql do all of the work and send back just the sum. It is a double since sql sends back a double zero if no records are found, but you could change that and do a null check.
there is a long discussion of summing in the thread that @JasonMcD posted.
I can write the SQL but again writing s SQL within C# would be a challenge for me. Currently I am investigating how to achieve this through BAQ. So far didn’t found a direct example.
That is true - the widget you have (Set Argument…) is a one-liner.
For a block of C#, you’d need the Custom Code Block widget.
(I think that’s the name.)
Also, You have to set up the Epicor Function Library to allow custom code widgets, and then you have to create that specific kind of Function.
Now, whether that syntax works or not, that is not my forte. Someone else will have to weigh in on that.
Well, OK, I’ll weigh in a little.
So, if you want the compiler to put the values of WeekStartDate (etc.) into the string, then you have to concatenate that together. As is, it’s going to read that as the string literal of “WeekStartDate”.
@JasonMcD That is how you would execute a sql command in a standalone program which is not how Epicor needs it.
I made a function similar to yours and put this in a code block.
EDIT: The code I posted first worked if there were hours, but failed if there were no hours o r the dates were inverted. The new code works for those conditions.
/* sum hours */
Ice.Diagnostics.Log.WriteEntry($"Employee ID {EmpID} From {dateFrom} thru {dateThru}");
double? empSumHours = 0;
empSumHours = Db.LaborDtl.Where(ld => ld.Company == CompanyID && EmpID == ld.EmployeeNum && ld.ClockInDate >= dateFrom && ld.ClockInDate <= dateThru ).Sum(x=> (double?) x.LaborHrs) ?? 0;
Ice.Diagnostics.Log.WriteEntry($"Employee ID {EmpID} From {dateFrom} thru {dateThru} Hours are {empSumHours}");
empHours = (decimal?) empSumHours;
As Jason mentions you need to check custom code widgets and yo will need read db access.
IMO you should always pick Add widget with Code, so you can use either as needed.