Email a list of Sales Order Ack at night

We would like to email a list of Sales Orders that were created during the day using a Breaking/Routing rule to send the emails.

I am struggling to make this happen.

I added a Boolean UD field called OrderHed_SendOrdAckEmail_c and i created an autoprint that is triggered by the OrderHed_SendOrdAckEmail_c being changed from false to true. That works.

I then created a function that sets the OrderHed_SendOrdAckEmail_c to true and added that function to a schedule. That works with a hard coded sales order number.

I am struggling to find a way to set mutiple OrderHed_SendOrdAckEmail_c based on a query of orders sent that day.

Has anyone done anything like this?

I thought i was close wrote some custom code in the function to set the OrderHed_SendOrdAckEmail_c however, i could not get the code to check syntax - errro on Db.Validate(); I then tried the Db.SaveChanges(); however, when i run the function i get an error.

Any help greatly appreciated.
DaveO

Yup, the crux of what you want (sorry on my mobile)

  1. Loop thru todays orders

Foreach (Var todaysOrders in Db.OrderHed.Where(r => r.Company == CurrentCompany && r.OrderDate == Today)

  1. Invoke the SalesOrderSvc

  2. GetByID

  3. Change UD field to true

  4. Update

Since you already have the autoprint set up when your UD field is changed from false to true, something like this would probably do the job:

var OrdersToEmail = Db.OrderHed.Where( x => x.OrderDate == DateTime.Today ).Select( s => s.OrderNum ).ToList();

if ( OrdersToEmail == null || OrdersToEmail.Count <=0 ) return;

foreach ( int orderNum in OrdersToEmail )
{
    CallService<SalesOrderSvcContract>( svc =>
    {
        var orderDS = svc.GetByID( orderNum );
        var orderHed = orderDS.OrderHed.FirstOrDefault();
        
        orderHed.SendOrdAckEmail_c = true;
        orderHed.RowMod = "U";

        svc.Update( ref orderDS );
    });
}
1 Like

Why would you not just use a report that has a filter on it for Order Date, and schedule it dynamically, for example, put it on a schedule at 12:30am and set the filter to “yesterday”? No need for UD fields or functions.

3 Likes

The only reason I could think of you needed to resend the Ack again, like a sudo printed flag.

Like the solution though so simple.

we did it like this:

a data directive on the Print Checkbox, which creates a print task (including routing rule with email) in a process set. We schedule the process set at night. with a bpm on the systask table we remove a successful print from the process set.

1 Like

Mr. @kve : Thank you for the code sample that looks just like what i need.

I will try later today.
DaveO

Ms. @aosemwengie1: @Hally has it right - my intention is to use the UD field as an “Emailed already” flag so we do not send the same OrderAck twice.

DaveO

Ms. @aosemwengie1: I must confess i “Simplified the description here”.

In actual practice we need to look at all of the shipments made for the day - filter by Pack Slip Shipped flag = true, lookup the Sales order and if the email has NOT been sent before AND there is an assigned customer contact valid email address (another UD field i added) THEN send the email.

DaveO

I had something similar at my previous job (I had used the AutoPrintReady field) because ours had to have attachments along with the acknowledgement, but the attachment file had to be dynamic based on the PartNum and Customer.

I see - when you said email a list it sounded like it was just a report listing the orders, makes sense.

Mr. @kve : Please excuse my script kiddie understanding. I am having trouble getting the function to “Check Syntax”. It is saying the namespace SalesOrderSvcContract could not be found.

I tried adding a Usings of “Erp.Contracts.SalesOrderSvcContract” however that does not seem to help the error.

Any help greatly appreciated:

So to get that added, exit the Code Editor (it will ask you if you want to save with an error, you can say yes). Go to the tab on the library labeled “References” and the sub-tab “Services”, click Add. Find the service called “SalesOrder” and double click.

If it still throws the error, change the call service line to below:

CallService<Erp.Contracts.SalesOrderSvcContract>( svc =>

2 Likes

Mr. Kevin: Thank you so much for the help. Yes, adding the Erp.Contracts prefix in the code did get me past that error.

Now i am seeing an error referencing my UD Field OrderHed.SendOrdAckEmail_c

You have no idea how bright his little eyes get when you call him that.

Proud Of You Yes GIF

try orderHed["SendOrdAckEmail_c"] = true

Mr. Kevin: Thank you for the suggestion. Hopefully one more help.
Identifier expected

I Finally got it to Check Syntax.

I made changed the one line to orderHed[“SendOrdAckEmail_c”] = true;

I took out the period after the orderHed. Hopefully that is correct? Here is the full code now.

var OrdersToEmail = Db.OrderHed.Where( x => x.OrderDate == DateTime.Today ).Select( s => s.OrderNum ).ToList();

if ( OrdersToEmail == null || OrdersToEmail.Count <=0 ) return;

foreach ( int orderNum in OrdersToEmail )
{
    CallService<Erp.Contracts.SalesOrderSvcContract>( svc =>
    {
        var orderDS = svc.GetByID( orderNum );
        var orderHed = orderDS.OrderHed.FirstOrDefault();
        
        orderHed["SendOrdAckEmail_c"] = true; 
        orderHed.RowMod = "U";

        svc.Update( ref orderDS );
    });
}
1 Like

That’s correct, no . after the object when using ["FieldName"] syntax.

The Original Series GIF by Star Trek