By Barbara Han
Issue
I want to assign an item number of my own (and not use the numbering scheme item number) to the existing item or the new item that I added. How to do that with Vault API?
Solution
To use the custom Item number, the custom Item number needs to be added into Vault first through AddItemNumbers.
The following sample code demonstrates how to change the item number of an existing item. For the new item, just replace the EditItem function in below code with AddItemRevision function. AddItemRevision function also makes the item editable.
C#
ItemService itemSvc = m_serviceManager.ItemService; Item editableItem = itemSvc.EditItem(m_selectedItem.RevId);
NumSchm[] numSchms; numSchms = itemSvc.GetNumberingSchemesByType(NumSchmType.Activated);
System.Collections.ArrayList numArr = new System.Collections.ArrayList();
foreach (NumSchm _numSchm in numSchms) { if (_numSchm.Name == "Mapped") numArr.Add(_numSchm.SchmID); }
long[] numSchmIds = (long[])numArr.ToArray(typeof(long));
long[] masterIds = new long[1]; masterIds[0] = editableItem.MasterId;
string[] newItemNum; newItemNum = new string[] { "blabla" }; StringArray[] fieldInputs = new StringArray[1]; StringArray tempArr = new StringArray(); tempArr.Items = newItemNum; fieldInputs[0] = tempArr;
ItemNum[] newNumbers = itemSvc.AddItemNumbers(masterIds, numSchmIds, fieldInputs);
editableItem.ItemNum = newNumbers[0].ItemNum1;
//commit the item, which finalizes the object
Item[] items = new Item[1]; items[0] = editableItem; itemSvc.UpdateAndCommitItems(items); |
VB.NET
Dim itemSvc As ItemService = m_serviceManager.ItemService Dim editableItem As Item = itemSvc.EditItem(m_selectedItem.RevId)
'To chang the item number, the item number must exist in Vault ' use AddItemNumbers to add the item number
Dim numSchms() As NumSchm numSchms = itemSvc.GetNumberingSchemesByType(NumSchmType.Activated)
Dim numArr As ArrayList numArr = New ArrayList Dim _numSchm As NumSchm
For Each _numSchm In numSchms If (_numSchm.Name = "Mapped") Then numArr.Add(_numSchm.SchmID) Next
Dim numSchmIds() As Long = CType(numArr.ToArray(GetType(System.Int64)), Long())
Dim masterIds(0) As Long masterIds(0) = editableItem.MasterId
' Change the item number to "blabla"
Dim newItem() As String newItem = New String() {"blabla"}
Dim fieldInputs(0) As StringArray Dim tempArr As New StringArray tempArr.Items = newItem
fieldInputs(0) = tempArr
Dim numbers As ItemNum() = itemSvc.AddItemNumbers(masterIds, numSchmIds, fieldInputs) editableItem.ItemNum = numbers(0).ItemNum1
' commit the item, which finalizes the object Dim items(0) As Item items(0) = editableItem itemSvc.UpdateAndCommitItems(items) End Sub |
Please refer to the below code snippet if you want to use AddItemRevision function:
// Get the Part category - just an example,
// you can chose other categories
Cat[] categories = ServiceManager.CategoryService.
GetCategoriesByEntityClassId("ITEM", true);
long catId = -1;
foreach (Cat category in categories)
{
if ((category.SysName == "Part"))
{
catId = category.Id;
}
}
Item editableItem = ServiceManager.ItemService.AddItemRevision(catId);