Developer forum

Forum » Development » Order StateId change doesn't send the email

Order StateId change doesn't send the email

Alexandru Aliu
Reply

Hey guys,

We have a project where we try to make the system send an email when the order gets a specific Order State. This works when we do it manually, but it doesn't work when we change the State Id programaticaly . Can anyone guide us to get this done ?

We tried it in different ways:

- with a custom class using DynamicWeb services

- with a data integration activity ( trying to import a XML file)

The order fields are set up currectly , even the Order state is correct , but the email doesn't go and we don't get any error. 

Any suggestion will help, thank you  


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Alexandru,

 

Can you share your code of the custom class? That should be triggered when you change the Order State of the order and then save the object.

 

The Data Integration activity will not do that, because the Order Provider is not prepared to do so. It's only mapping data. You can post it as a Feature Request so that the "Save" event is triggered on orders that are updated by an activity, which in turn would trigger order state notifications (when applicable) and any custom save notifications you'd have.

 

Best Regards,

Nuno Aguiar

 
Marcin Zajkowski
Reply

Hi Nuno!

Thanks. I work with Alexandru on this issue and I'll submit the feature request for sure. Then what is the "best practice" to change the state programmatically?

We've created this method in the custom class that loops through the orders and changes the state to the new one.

The problem is that the state is changed, the other property is set (this checkbox value just indicates if we looped through this order), but nothing else is triggered (e-mails associated with the state etc.)

Interesting is that it seems to be working on one of our other solutions using 9.14.11. Where it doesn't work is 9.15.9.

Ideas why?

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Alexandru,

 

Looking at your code, depends if the order is already in that particular state.

 

Dynamicweb tracks the StateId property of an Order, and only if it changes, then it kicks off notification emails and such. In other words, simply doing o.StateId = stateId, will not ensure the emails are sent. It can be a bit of a hack, but you could reset the o.StateId first, and then set it to the desired state. That would trigger it.

 

I can't say this is the best solution, but it's our most recent/current approach:

  • We have a scheduled task to pick up orders that have been manipulated through a data activity (aka Integration job)
    • We use a flag to determine what orders need to be "triggered"
      i.e. RequiresUpdate=true
  • We instantiate the order (using Services)
  • We update the flag (and may do some funky stuff to reset/update the stateId
  • We call Services.Orders.Save(order)

 

So it's pretty much what you plan to do, but more specifically through a Scheduled task. You could likley do it using a Notification subscriber at the end of the activity runs. The reason why we use a Scheduled task add in is because it provides some configuration options so we don't have to tweak code between projects.

 

Hope this helps,

Nuno Aguiar

 
Marcin Zajkowski
Reply

Hey Nuno!

We're on the right path then. By resetting it - you mean that I need to reset/set it to other state and Save() and then chagne and Save() again?

Can you share some code example of the tasks that would trigger the emails to be sent?

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Marcin,

 

By resetting it - you mean that I need to reset/set it to other state and Save() and then chagne and Save() again?

Yes, but that's just to get you going. If you do this to all orders every 5 minutes, then everyone will be receiving emails every 5 minutes.

 

One thing you can do is check a custom field OR a dummy Orders State (i.e. Pending Shipping Emails), then you get all orders in that "dummy" state and update them to the final State (i.e. Shipped) which triggers the emails. That would be better than the solution proposed above.

 

My code is basically the same as yours. It's mostly on how to get the collection of orders where the "magic" needs to be, and then ensuring that within that collection, the StateId really does change (whether that's from setting it to empty and then to it's proper state OR by using a dummy state).

 

Best Regards,

Nuno Aguiar

 

You must be logged in to post in the forum