How to update existing warehouse from code

Goran Sneperger
Member since: 2012
 

Hi, 

is there any way of updating fields like IsActive, IsFullfilmentCenter and so on from code? Or address details?

I am using Get(string code) on instance of IWarehouseRepository. 

After checking if the warehouse was not null I cannot set any of the values. Is there some kind of substitue for writable clone that is present in CMS?

#189010 Mar 08, 2018 14:17
  • Erik Norberg
    Member since: 2012
     

    Have you tried casting it to Warehouse?

    if (_warehouseRepository.Get(warehouseCode) is Warehouse warehouse)
    {
        warehouse.IsActive = true;
        _warehouseRepository.Save(warehouse);
    }

    I haven't actually tried it and it is a bit ugly but as the Get function only returns IWarehouse you can't make changes to it.

    #189012 Mar 08, 2018 14:30
  • Goran Sneperger
    Member since: 2012
     

    When casting to Warehouse interface remains null. Although it looked like a valid answer it still remains open to suggestions.

    #189014 Edited, Mar 08, 2018 14:35
  • Quan Mai
    Member since: 2011
     

    I'm commuting now so can't check to be sure, but you can do like this:

    var warehouse = new Warehouse(readOnlyWarehouse);

    Then just update the warehouse and save it with IWarehouseRepository

    #189020 Mar 08, 2018 18:06
  • Goran Sneperger
    Member since: 2012
     

    I have tried that but on stepping over the actual save method VS hangs for a bit only to show me pop-up with the following message:

    The debugger attempted to call a functin within the debugged process but this function call is being aborted.

    #189042 Mar 09, 2018 10:01
  • Quan Mai
    Member since: 2011
     

    I can confirm that code above is indeed correct. 

    The error you see is a problem with the debugger in VS and has nothing to do with the API. 

    #189043 Mar 09, 2018 10:07
  • Goran Sneperger
    Member since: 2012
     

    That's right. Thank you.

    #189044 Mar 09, 2018 10:28
  • Erik Norberg
    Member since: 2012
     
    var warehouse = new Warehouse(readOnlyWarehouse);

    Wait..what?

    new Warehouse(copyFrom: loadedWarehouse) won't give you a new warehouse with new/blank identifiers and the rest is copied?

    It will copy everything including identifiers to get an existing entity that can be modified, not really what you would expect from a constructor or is it? surprised

    #189060 Mar 09, 2018 13:53
  • Quan Mai
    Member since: 2011
     

    I didn't write the code. cool

    Well, it would be better if it implemented IReadonly, but we have to live with what we have today.

    I'm not saying no to improvements,but in the end it always comes to priorty 

    #189061 Edited, Mar 09, 2018 13:56
  • Erik Norberg
    Member since: 2012
     

    All change comes to priorities.

    I still think someone actually looking for creating a new Warehouse by copying an existing one is in for a very rude surprise. yell

    You might want to mention the constructor here:

    https://world.episerver.com/documentation/developer-guides/commerce/warehouses-and-inventories/Warehouses-and-inventories-examples/

    Only example for saving a Warehouse there creates a new one with the empty constructor. It doesn't give any pointers for this modify an existing solution.

    #189062 Mar 09, 2018 14:04
  • Quan Mai
    Member since: 2011
     

    Fair enough. I filed a bug report for that - let's see if someone can sneak that bug fix in.

    #189063 Mar 09, 2018 14:25
  • Quan Mai
    Member since: 2011
     

    I updated the documentation as well - someone from Doc team (probably Bob) will review and publish it. 

    #189064 Mar 09, 2018 14:30
  • Goran Sneperger
    Member since: 2012
     

    Great! Thanks.

    #189065 Mar 09, 2018 14:40
  • Bob Bolt
    Member since: 2014
     

    Updated doc has been published

    #189077 Mar 09, 2018 17:42
  • Erik Norberg
    Member since: 2012
     

    Updated doc has been published

    It is broken. Expanding the code example link below:

    Editing a warehouse

    Use IWarehouseRepository.Save(IWarehouse) to edit and save a warehouse.

    Note: The IWarehouse object returned from Get and List methods above IWarehouseRepository is read-only. So, to edit it, you must make a writable clone object then update it, as is.

    Actually expands the code example below:

    Adding a warehouse

    Use IWarehouseRepository.Save(IWarehouse) to add a warehouse.

    Looks like a classic copy-paste miss. laughing

    #189131 Mar 12, 2018 15:46
  • Quan Mai
    Member since: 2011
     

    Good spot :). But it's not a copy-paste error, editing a document with code can be quite, tricky. I will need the superior editing skill from Bob to make it right. 

    Btw the bug I reported is now under review - my boss has fixed it. Take note @Erik, you don't get that a lot ;) 

    #189141 Mar 12, 2018 16:36
  • Bob Bolt
    Member since: 2014
     

    Hopefully, the editing has now been fixed on this page -- Let us know Erik.

    #189147 Mar 12, 2018 17:27
  • Erik Norberg
    Member since: 2012
     

    @Quan Duly noted. cool Is it a breaking changes so it will come in commerce 13 or did he sneak it into 12?

    @Bob works as it should. Thanks Bob. :)

    #189201 Mar 13, 2018 13:44
  • Quan Mai
    Member since: 2011
     

    We are targeting Commerce 12. so if nothing strange happens then you will get the fix soon-ish 

    #189202 Mar 13, 2018 13:48