Developer forum

Forum » Development » Saving OrderState

Saving OrderState

Casper Andersen
Reply

Hi DynamicWeb

 

when using the following code to save my OrderState on an order, it does not seem to update. Any suggestion on what i am doing wrong?

 

Dynamicweb.eCommerce.Orders.Order order = Dynamicweb.eCommerce.Orders.Order.GetOrderByID(ord.OrderID);
order.StateID = "OS11";
order.OrderState.ID = "OS11"
order.Save(ord.OrderID);

and OS11 does exist in EcomOrderStates.


Replies

 
Nicolai Pedersen
Reply

Well, it should. try add a profiler to the SQL and see if data comes in.

If you update like this, and tries to use i.e. the cart from the context, things are unsynced.

So - in what context do you run the code above? And in what context do you see that the orderstate is not updated?

 
Casper Andersen
Reply

I run the code in a custom data integration i have made basically in the integration i am exporting my order to a CRM system, and then if the order exists i grab the order from the CRM system and check if its pipeline has changed, if it has then i have some code that determines the OrderState to set on the order, and then use the code i showed earlier to set a new OrderState and save that back to the order.

Then when i go to the Ecommerce section in the backend i dont see any orderstate change.

 
Casper Andersen
Reply

Ok so i used some time to debug everything that is happening. The problem is not that the OrderStates are not saved, the problem is that they are set back to their initial value when the integration runs GetSchema() at the end.

as soon as the value _schema is returned by the GetSchema() method all values are set back to their initial value.

Also even if i go into the Ecommerce section of DynamicWeb and set the order state there, then when i run my integration it resets all of them to the Default value. which is OS11

 

Any idea what is going on?

 
Nicolai Pedersen
Reply

Nope - I am totally blank... You could try updating the orderstate using direct SQL. Order object etc. can have issues when not in a httpcontext (which is the case when running an integration job...).

BR Nicolai

 
Casper Andersen
Reply

Ok ill try that, although to be honest i dont know how to run standard SQL statements in DynamicWeb, is there any specific code you use to run things like select and update statements?

 
Nicolai Pedersen
 
Casper Andersen
Reply

Nevermind the last question, i got the result i wanted with the required code:

var command = Dynamicweb.Database.CreateConnection().CreateCommand();
                        command.CommandText = "UPDATE EcomOrders SET OrderStateID = '" + ordsid + "' WHERE OrderID = '" + order.ID + "'";
                        command.ExecuteNonQuery();

But this did not change anything.

The reset happens between returning true after my runjob method but before getting to the GetSchema() method.

Does anything special happen inbetween? i tried running my debugger through but i dont see anything special happening?

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Casper,
I guess you are using your custom destination provider. The GetSchema() is not a good place to use for updating your orders. This method is used for getting the tables and their columns to generate the schema structure and can be called several times during the job execution.
So if you are using custom destination provider you should place your code for updating orders somewhere in the RunJob method(where your logic requires it).
If you are using custom source provider you should place your code in the Close() method, that method runs after the destination provider RunJob() execution.

Regards, Dmitrij

 
Casper Andersen
Reply

Hi Dmitriy

What i am trying to say in my post is that of course i am running all my custom code in the RunJob method, basically i am taking all the orders in dynamicweb with the built in Order Provider, then i pass them into my custom Destination provider, then u run through each order and check if that order is created in the CRM system, if it is not, then i create it there, if it is, i check its pipeline level and adjust my orders OrderState accordingly, for an example a pipeline level of 1 is = OS11 and level 2 = OS12 and so on, and each time i run through an order and update the OrderState, i can see after i save my order that the OrderState has changed in the Database, but then as soon as a return true after the RunJob is complete, all orders are set back to their default OrderState e.g. OS11.

Hope i made myself a bit more clear

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply
This post has been marked as an answer

Hi Casper,
this can be because you are using the OrderProvider as a source and it has a code that is updating the fields: OrderIsExported is set to 1
and OrderStateID is set to "Order state after export" option selected value, so check this option in your OrderProvider and set it to "None" so it will skip updating this OrderStateID field.
Regards, Dmitrij
 

Votes for this answer: 1
 
Casper Andersen
Reply

This is the part where i smash my head at the table for not remembring this when i set up the integration...

 

Thanks ton for the help everyone, now all the order states change as they should!

 

You must be logged in to post in the forum