By Barbara Han
Some days back, I wrote this blog introducing how to add association properties to an Item BOM, but it seems that deleting association properties from an Item BOM is not that straight forward, some people got 109 error. So I’m posting the code sample here showing how to implement the deleting. Hope it’s helpful.
C# code snippet from a console application:
static void DeleteAssociationProperties_Test()
{
UserPasswordCredentials login =
new UserPasswordCredentials(
"localhost", "Vault", "Administrator", "");
using (WebServiceManager mgr =
new WebServiceManager(login))
{
// Get the Associatin Property Definitions
PropertyService propSvc = mgr.PropertyService;
AssocPropDef[] definitions =
propSvc.GetAssociationPropertyDefinitionsByType(
AssociationPropClass.ItemBOMLink);
try
{
AssocPropDef definition = definitions.FirstOrDefault(
p => p.DispName.Equals("Test"));
PropDef[] props = propSvc.FindPropertyDefinitionsBySystemNames
("ITEM", new string[] { "Number" });
//Find items that you are interested in.
if (props != null)
{
SrchCond srchConds = new SrchCond();
srchConds.PropDefId =
Convert.ToInt64(props[0].Id);
srchConds.PropTyp =
PropertySearchType.SingleProperty;
srchConds.SrchRule = SearchRuleType.Must;
// (CONTAINS)
srchConds.SrchOper = Convert.ToInt32(1);
// (DOES_NOT_CONTAIN)
//srchConds.SrchOper =Convert.ToInt32(2);
// Number of the Item
srchConds.SrchTxt = "100001";
string bookmark = String.Empty;
SrchStatus searchStatus = null;
SrchSort ss = new SrchSort()
{
PropDefId = srchConds.PropDefId,
SortAsc = true
};
//Find the item
ItemService itemSvc = mgr.ItemService;
Item[] myItems = itemSvc.FindItemRevisionsBySearchConditions
(new SrchCond[] { srchConds },
new SrchSort[] { ss },
true,
ref bookmark,
out searchStatus);
//Get the first item
Item parItem = myItems[0];
// Obtain the item BOM association properties
ItemBOM itemBOM = itemSvc.GetItemBOMByItemIdAndDate(
parItem.Id,
parItem.EffEnd,
BOMTyp.Tip,
BOMViewEditOptions.Defaults);
long childMasterId = 0;
foreach (Item singleItem in itemBOM.ItemRevArray)
{
if (singleItem.ItemNum == "100002")
{
childMasterId = singleItem.MasterId;
break;
}
}
AssocPropItem[] items = itemSvc.GetItemBOMAssociationProperties(
new long[] { parItem.Id },
new long[] { definition.Id },
new long[] { childMasterId });
List<long> propItems = new List<long>();
foreach (AssocPropItem item in items)
{
propItems.Add(item.Id);
}
if (propItems.Count > 0)
itemSvc.UpdateItemBOMAssociationProperties(null, propItems.ToArray());
}
// delete AssocPropDef
if (definition != null)
{
propSvc.DeleteAssociationPropertyDefinitions(new long[] { definition.Id });
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}