Community
Vault Customization
Share your knowledge, ask questions, and explore popular Vault API, Data Standard, and VBA topics related to programming, creating add-ins, or working with the Vault API.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Populating Item UDPs

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
michael.collins2
1307 Views, 8 Replies

Populating Item UDPs

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 =

 

 

8 REPLIES 8
Message 2 of 9

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:

  1. Put the item in an editable state, which is essentially "checking out the item"
  2. Make changes, such as setting UDP values.
  3. Commit the changes, which is like a "check-in"

For more information, see the "Editing Items" knowledge base article in the SDK documentation.

 



Doug Redmond
Software Engineer
Autodesk, Inc.

Message 3 of 9

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?

 

 

Message 4 of 9

Yes, you need to call UpdateAndCommitItems.

When the function fails, what is the error code?



Doug Redmond
Software Engineer
Autodesk, Inc.

Message 5 of 9

Error code 1387.

Message 6 of 9

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.

 

Message 7 of 9

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.



Doug Redmond
Software Engineer
Autodesk, Inc.

Message 8 of 9

Super.  Thanks for the help Doug.

 

Message 9 of 9

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.

Post to forums  

Autodesk Design & Make Report