Page 1 of 1

Add default inventory group if non exists

PostPosted: Wed Feb 18, 2015 3:37 pm
by DannyC
Hi,

when saving an inventory, I want to check if the item has at least 1 Inventory Group. If not, then add in a default group.

I am just having trouble on the Add. I don't think it likes the "ZZZZZZZZZZ0000000000"

Code: Select all
    Public Sub Setup(ByVal JiwaForm As JiwaApplication.IJiwaForm, ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaFormPlugin.Setup
      Dim InventoryForm As JiwaInventoryUI.InventoryMaintenanceForm = DirectCast(JiwaForm, JiwaInventoryUI.InventoryMaintenanceForm)
      AddHandler InventoryForm.Inventory.SaveStart, AddressOf CheckGroup
   End Sub
   
      Sub CheckGroup(Sender As Object, e As System.EventArgs)
         Dim inventory As JiwaInventory.Inventory  = DirectCast(sender, JiwaInventory.Inventory)   
         If inventory.Groups.Count <= 0  Then
            inventory.Groups.Add("ZZZZZZZZZZ0000000000")  'this bit here isn't working
         End If   
      End Sub   


Cheers

Danny

Re: Add default inventory group if non exists  Topic is solved

PostPosted: Sun Feb 22, 2015 11:07 am
by Mike.Sheen
Hi Danny,

Try this:

Code: Select all
Sub CheckGroup(Sender As Object, e As System.EventArgs)
   Dim inventory As JiwaInventory.Inventory  = DirectCast(sender, JiwaInventory.Inventory)   
   If inventory.Groups.Count <= 0  Then
      Dim newGroupMember As New JiwaInventory.Group
      newGroupMember.Group.ReadRecord("ZZZZZZZZZZ0000000000")
      inventory.Groups.Add(newGroupMember)
   End If   
End Sub


The Add method requires a Group object to be provided, which is actually a group member or group link object - one of its properties is the Group entity - so you need to create the group member, read the group entity of the group object (confusing naming - I know) using the ReadRecord, then add it to the inventory Groups collection. Note rather than using the ID, you can also use newGroupMember.Group.ReadRecordFromDescription("Default Group Name") - which probably will make your code more maintainable in the future.

Mike