By Barbara Han
Vault 2013 introduced two new API –GetItemBOMAssociationProperties & UpdateItemBOMAssociationProperties - to allow you read out and change the ItemBOM’s association properties. I played them for a while and copied the sample code here for anyone who wants to use them to refer to:
VB.NET sample snippet:
' Get the Associatin Property Definitions
Dim definitions() As AssocPropDef = m_serviceManager.PropertyService.
GetAssociationPropertyDefinitionsByType(AssociationPropClass.ItemBOMLink)
Dim definition As AssocPropDef = definitions.FirstOrDefault(
Function(p) p.DispName.Equals("Test"))
If definition Is Nothing Then
Dim definitionInfo As AssocPropDefInfo = m_serviceManager.PropertyService.
AddAssociationPropertyDefinition(Guid.NewGuid.ToString("D").ToLower, "Test",
DataType.String, True, Nothing, Nothing,
AssocPropTyp.ItemBOMAssoc)
definition = definitionInfo.PropDef
End If
' Set up the Association Property array in order add it to ItemBOM
Dim itemBOM As ItemBOM = m_serviceManager.ItemService.GetItemBOMByItemIdAndDate(
m_selectedItem.Id, m_selectedItem.EffEnd, BOMTyp.Tip, BOMViewEditOptions.Defaults)
Dim ParIds As List(Of Long) = New List(Of Long)
Dim associations As List(Of AssocPropItem) = New List(Of AssocPropItem)
Dim ChildIds As List(Of Long) = New List(Of Long)
For Each itemAss As ItemAssoc In itemBOM.ItemAssocArray
ParIds.Add(itemAss.ParItemID)
ChildIds.Add(itemAss.CldItemMasterID)
Dim association As AssocPropItem = New AssocPropItem With {
.AssocPropTyp = AssocPropTyp.ItemBOMAssoc,
.FromId = itemAss.ParItemID,
.ToId = itemAss.CldItemMasterID,
.PropDefId = definition.Id,
.Val = "ABC",
.ValTyp = DataType.String
}
associations.Add(association)
Next
' Update item BOM association properties
m_serviceManager.ItemService.UpdateItemBOMAssociationProperties(
associations.ToArray(), Nothing)
' Obtain the item BOM association properties
Dim PropIds As List(Of Long) = New List(Of Long)
PropIds.Add(definition.Id)
Dim assocProps() As AssocPropItem = m_serviceManager.ItemService.
GetItemBOMAssociationProperties(
ParIds.ToArray(), PropIds.ToArray(), ChildIds.ToArray())
C# sample snippet:
static void AddAssociatinProperties_Test()
{
try
{
// Log in Vault
IWebServiceCredentials credentials = new UserPasswordCredentials(
"localhost", "Vault", "Administrator", "");
using (WebServiceManager mgr = new WebServiceManager(credentials))
{
// Get the Associatin Property Definitions
AssocPropDef[] definitions = mgr.PropertyService.
GetAssociationPropertyDefinitionsByType(
AssociationPropClass.ItemBOMLink);
AssocPropDef definition = definitions.FirstOrDefault(
p => p.DispName.Equals("Test"));
if (definition == null)
{
AssocPropDefInfo definitionInfo = mgr.PropertyService.
AddAssociationPropertyDefinition(
Guid.NewGuid().ToString("D").ToLower(),
"Test", DataType.String, true, null, null,
AssocPropTyp.ItemBOMAssoc);
definition = definitionInfo.PropDef;
}
// Set up the Association Property Item in order to add it to ItemBOM
ItemService itemSvc = mgr.ItemService;
Item parentItem = itemSvc.GetLatestItemByItemNumber("100001");
Item childItem = itemSvc.GetLatestItemByItemNumber("100002");
AssocPropItem association = new AssocPropItem()
{
AssocPropTyp = AssocPropTyp.ItemBOMAssoc,
FromId = parentItem.Id,
ToId = childItem.MasterId,
PropDefId = definition.Id,
Val = "ABC",
ValTyp = DataType.String,
};
// Update item BOM association properties
itemSvc.UpdateItemBOMAssociationProperties(
new AssocPropItem[] { association }, null);
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex.Message);
}
}