I am adding Items to Vault 2013 Professonal through the API.
I now have to populate some UDPs that we have defined for Items.
I don't see the Item UDPs for the Item object, so I figure that something else is needed to access them, but I can't find examples in the Help File or here.
Here is where I am at:
// Get our Item Numbering Schema ("Mapped") ID.
long itemNumberingSchemeID = GetItemNumberingSchemeID("Mapped");
// Create a new Item with that Category ID.
newItem = m_ItemSvc.AddItemRevision(GetCatID("Mechanical Detail"));
// Create an array with the desired item number.
string[] newItemNum = newstring[] { itemNumber };
// Create an array of StringArrays with 1 element.
StringArray[] fieldInputs = new StringArray[1];
// Create a single StringArray.
StringArray tempArr = newStringArray();
tempArr.Items = newItemNum;
fieldInputs[0] = tempArr;
// Change Item Number.
ItemNum[] newNums = m_ItemSvc.AddItemNumbers(newlong[] { newItem.MasterId }, newlong[] { itemNumberingSchemeID }, fieldInputs);
// Set Item values.
newItem.ItemNum = itemNumber;
newItem.Detail = GetExcelData("Description");
newItem.Title = "";
// Set Item UDP values.
newItem.BasicMaterial =
newItem.Size =
newItem.Vendor =
Solved! Go to Solution.
Solved by Redmond.D. Go to Solution.
UpdateItemProperties is the function you want. However, there are some things you need to know about working with items.
Editing an item usually follows the pattern of:
For more information, see the "Editing Items" knowledge base article in the SDK documentation.
Ok, now I have:
// =====================================================================
// Get our Item Numbering Schema ("Mapped") ID.
long itemNumberingSchemeID = GetItemNumberingSchemeID("Mapped");
// Create a new Item with that Category ID (Use "Mechanical Detail" for everything for now).
newItem = m_ItemSvc.AddItemRevision(GetCatID("Mechanical Detail"));
// Set Item values
newItem.ItemNum = itemNumber;
newItem.Detail = GetExcelData("Description");
newItem.Title = "";
string excelUnits = GetExcelData("Base Unit of Measure");
if (excelUnits != "")
{
newItem.Units = UnitsMappingDictionary[excelUnits];
}
else
{
newItem.Units = "";
}
List<string> UDPList = new List<string>();
UDPList.Add("Base Unit of Measure");
UDPList.Add("Basic Material");
UDPList.Add("Part Number");
UDPList.Add("Vendor");
UDPList.Add("Size/Dimension");
//UDPList.Add("Dimension");
int UDPListLength = UDPList.Count();
long[] propDefIdArray = new long[UDPListLength];
string[] propValueArray = new string[UDPListLength];
// get the map between the property name and all Vault objects.
Dictionary<string, PropDef> propDefMap = GetPropDefMap(mgr);
// Get all property values for the Item.
PropInst[] itemProperties = propSvc.GetPropertiesByEntityIds("ITEM", newlong[] { newItem.Id });
string excelCellData = "";
intindex = 0;
// Put UDP values and propDefIds into arrays.
foreach (string item in UDPList)
{
excelCellData = GetExcelData(item);
// Get the property definition from the property name.
PropDef propDef;
if (!propDefMap.TryGetValue(item, out propDef) || excelCellData == null)
{
continue; // tag value doesn't match any Vault properties, or cell was empty.
}
try
{
// Add to the list of UDPs and values to set.
propDefIdArray[index] = propDef.Id;
propValueArray[index] = excelCellData;
index++;
}
catch (Exception ex)
{
MessageBox.Show("Error with property " + item + ":\n"+ ex.Message);
}
}
if(index > 0)
{
// We have the arrays all set; now do the actual save.
m_ItemSvc.UpdateItemProperties( new long[] { newItem.RevId }, propDefIdArray, propValueArray );
// Commit Changes
m_ItemSvc.UpdateAndCommitItems(new Item[] { newItem });
}
// =====================================================================
All seems good except that I get a SOAP error on the last command. Is that command (UpdateAndCommitItems) necessary?
Yes, you need to call UpdateAndCommitItems.
When the function fails, what is the error code?
I see that the resriction code is 2037, ItemNotEditable. I thought that when a new Item was created, it was in an Editable state, but I guess not.
I checked some other ADN cases. It turns out that UpdateItemProperties does a commit of the item. So I was wrong earlier; you don't need to call UpdateAndCommitItems.
The UpdateItemProperties should mention the commit operation, but it doesn't. A defect has already been logged.
Here is the final version:
// Get our Item Numbering Schema ID.
long itemNumberingSchemeID = GetItemNumberingSchemeID("Mapped");
// Create a new Item with that Category ID (Use "Mechanical Detail" for everything for now).
newItem = m_ItemSvc.AddItemRevision(GetCatID("Mechanical Detail"));
// Create the parameter for creating a new Item Number.
string[] newItemNum = new string[] { itemNumber };
// Create a new ItemNum object; we have to tell Vault to expect the new number.
ItemNum newNum = m_ItemSvc.AddItemNumber(newItem.MasterId, itemNumberingSchemeID, newItemNum);
// Set Item values
newItem.ItemNum = newNum.ItemNum1; // Vault knows about the new number now.
newItem.Detail = GetExcelData("Description");
newItem.Title = itemNumber;
// Commit the item, which finalizes the object.
m_ItemSvc.UpdateAndCommitItems(new Item[] { newItem });
// Read the Item back in to update the UDPs.
savedItem = m_ItemSvc.GetLatestItemByItemNumber(itemNumber);
editableItem = m_ItemSvc.EditItem(savedItem.RevId);
// Get the User Defined Properties.
List<string> UDPList = FillUDPList();
int UDPListLength = UDPList.Count();
long[] propDefIdArray = new long[UDPListLength];
string[] propValueArray = new string[UDPListLength];
// get the map between the property name and all Vault objects.
Dictionary<string, PropDef> propDefMap = GetPropDefMap(mgr);
string excelCellData = "";
int index = 0;
// Put UDP values and propDefIds into arrays.
foreach (string item in UDPList)
{
excelCellData = GetExcelData(item);
// Get the property definition from the property name.
PropDef propDef;
if (!propDefMap.TryGetValue(item, out propDef) || String.IsNullOrEmpty(excelCellData))
{
continue; // tag value doesn't match any Vault properties, or cell was empty.
}
try
{
// Add to the list of UDPs and values to set.
propDefIdArray[index] = propDef.Id;
propValueArray[index] = excelCellData;
index++;
}
catch (Exceptionex)
{
MessageBox.Show("Error with property " + item + ":\n"+ ex.Message);
}
}
// Redimension the arrays; it will exception if the arrays are not full.
Array.Resize(refpropDefIdArray, index);
Array.Resize(refpropValueArray, index);
if(index > 0)
{
// We have the arrays all set; now do the actual save.
m_ItemSvc.UpdateItemProperties(new long[] { editableItem.RevId }, propDefIdArray, propValueArray);
}
Can't find what you're looking for? Ask the community or share your knowledge.