Solved

Creating a new inventory item

  • 29 September 2022
  • 7 replies
  • 603 views

Userlevel 4
Badge +1

Hi, this should be simple but I must be misunderstanding something fundamental to Acumatica development.

I want to create a stock item and I have the following code:

InventoryItemMaint graphStock = PXGraph.CreateInstance<InventoryItemMaint>();
InventoryItem newInvItem = new InventoryItem();
newInvItem.InventoryCD = "TEST2";
newInvItem.Descr = "TEST2";
newInvItem.ItemClassID = 49;
graphStock.Item.Current = graphStock.Item.Insert(newInvItem);
graphStock.Actions.PressSave();

I set the ItemClassID and when I call graphStock.Item.Insert(newInvItem) the Acumatica Framework refeshes the newInvItem object and sets fields such as ItemType, ValMethod and BaseUnit.
Then I set the Current object and perform the Save Action, all basic stuff.

The problem is this, if the ItemClass doesn’t have a Default Warehouse then the code above works fine and the stock item is created. However, if the ItemClass does have a Default Warehouse then the code returns the following error:
Error: Inserting  'Inventory Item Currency Settings' record raised at least one error. Please review the errors.'
The InnerFields shows:
"DfltSiteID"
The InnerMessages shows:
"An error occurred during processing of the field Default Warehouse value 159 Error: An error occurred during processing of the field Inventory ID value -214748364 Error: Inventory ID '-2147483647' cannot be found in the system. Please verify whether you have proper access rights to this object."

If I create stock items in the UI then everything works fine, whether the ItemClass contains a default warehouse or not. Up until this point I had thought that calling graphStock.Item.Insert(newInvItem) or graphStock.Item.Update(newInvItem) would refresh the object in the same way that the UI would but this doesn’t seem to be the case.

Can anyone explain what is happening? Does anyone know what I need to do to fix this problem? How I can select an ItemClass and update the InventoryItem fully?

A screen shot of the ItemClass screen, when Default Warehouse is blank my code works, if a Default Warehouse is enter my code fails :(

Thanks
Steve

icon

Best answer by Fernando Amadoz 29 September 2022, 18:37

View original

7 replies

Userlevel 5
Badge +2

@stephenward03 

I follow this rule of thumb for the insertion of data in the cache:

  • Instantiate Graph
  • Assign key values (when it applies - if it’s autonumbered, it would not be indicated)
  • Insert record in the cache - Insert(Row)
  • Assign the rest of the values
  • Update the cache - Update(Row)
  • Save the record

Try this alternative version of your logic

InventoryItemMaint graphStock = PXGraph.CreateInstance<InventoryItemMaint>();
InventoryItem newInvItem = new InventoryItem();
//Key fields are inserted
newInvItem.InventoryCD = "TEST2";
newInvItem = graphStock.Item.Insert(newInvItem); //Inserted in the Cache

//Rest of the fields
newInvItem.Descr = "TEST2";
newInvItem.ItemClassID = 49;
graphStock.Item.Update(newInvItem); //Cache is updated

graphStock.Actions.PressSave();

 

Userlevel 6
Badge +4

Hi @stephenward03 

 

Can you try interacting with the graph as follow:

InventoryItemMaint graphStock = PXGraph.CreateInstance<InventoryItemMaint>();
graphStock.Item.Current = graphStock.Item.Insert(new InventoryItem());
InventoryItem newInvItem = graphStock.Item.Current;

newInvItem.InventoryCD = "TEST2";
newInvItem.Descr = "TEST2";
newInvItem.ItemClassID = 49;

graphStock.Item.Update(newInvItem);


Try
{
graphStock.Actions.PressSave();
}
catch(PXException ex)
{
....
}

When you work with a graph you have to set the current object and then work on the setting of values and logic. Acumatica assumes you work with a document (Current) and all internal views and events are related to that.

Make sure all required fields are set like Unit of Measure, Availability Calculation, etc…

The issue with the warehouse might be related to events not triggered.

Userlevel 4
Badge +1

Hi @Leonardo Justiniano , when I tried to use your code the

graphStock.Item.Update(newInvItem);

line doesn’t copy the values from the ItemClass to the newInvItem. Fields such as

ItemType, ValMethod and BaseUnit all remain as null. So the PressSave method fails.

Userlevel 4
Badge +1

@Fernando Amadoz 
I’ve tried with your code and it seems to be working well. The Update command refreshes the data and the item is save successfully. I will follow this method in future.

  • Instantiate Graph
  • Assign key values (when it applies - if it’s autonumbered, it would not be indicated)
  • Insert record in the cache - Insert(Row)
  • Assign the rest of the values
  • Update the cache - Update(Row)
  • Save the record

I wondered whether sometimes it will be nessesary to call Update multiple time - I guess after every field where CommitChanges is true? That way allowing Acumatica the chance to refresh data just as it would if the UI is being used.

Thanks

Steve

Userlevel 5
Badge +2

@stephenward03 

I wondered whether sometimes it will be nessesary to call Update multiple time - I guess after every field where CommitChanges is true? That way allowing Acumatica the chance to refresh data just as it would if the UI is being used.

The fields that lead to events should/could have an Update() method, but as you get more acquainted with the graphs you could make it a bit more “efficient”:

For instance, during the creation of an invoice, setting the Date defaults the period, and setting the Customer defaults the default Customer Location. However, Date and Customer are not correlated for their respective functionality, so you could do something like this

row.Date = ...
row.Customer = ...
this.DataView.Update(row);

instead of

row.Date = ...
this.DataView.Update(row);
row.Customer = ...
this.DataView.Update(row);

but the latter option is certainly valid.

Userlevel 6
Badge +4

Hi @stephenward03 

You can assign values through the cache and force events to happen by doing

PXCache cache; // somewhere coming from an event or for the graph general cache
...
cache.SetValueExt<DAC.Field1>(row, value)
...
cache.SetValueExt<DAC.Field2>(row, value2)
...
this.DataView.Update(row);

This way is the closest you get as editing through the UI.

Thanks @Fernando Amadoz for your insights… 

Userlevel 4
Badge +1

@Leonardo Justiniano and @Fernando Amadoz 

Thank you both for taking the time to answer these questions. I’ll be trying the tips about using SetValueExt later today. Might not need it for this issue with creating a stock item but I have another task where I’m certain this will be hopeful. 

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved