Forum Discussion
Zahar_Chernov
9 years agoCreator II
You can to assign your method to OnAfterInsert and OnAfterUpdate events of the Part itemtype
And then pull the BOM from DB by filtering it in the AML. If result is error, you OK and there are no related parts that block the save, if there is result, it will only pull for you obsolete parts and all you need to do is to return error. Because Aras insert\update actions are transaction, the save operation will be canceled:
Innovator inn = this.getInnovator();
Item updatedBOM = inn.applyAML(
"<AML>" +
" <Item type='Part BOM' action='get' select='related(item_number)'>" +
" <source_id>" + this.getID() + "</source_id>" +
" <related_id>" +
" <Item>" +
" <eht_production_lc_state condition='eq'>Obsolete</eht_production_lc_state>
" </Item>" +
" </related_id>" +
" </Item>" +
"</AML>");
if (!updatedBOM.isError() && updatedBOM.getItemCount() > 0)
{
// now you can collect Obsolete items PN using for or Linq
string obsoletePNs = "";
/*Option 1*/
for (int i = 0; i < relCount; i++)
obsoletePNs += ((!string.IsNullOrEmpty(obsoletePNs)) ? "," : "") + updatedBOM.getItemByIndex(i).getRelatedItem().getProperty("item_number", "");
/*Option 2*/
obsoletePNs = string.Join(",", updatedBOM.dom.SelectNodes("//related_id/Item/item_number").Cast<XmlNode>().Select(x => x.InnerText).ToArray());
//Return error
string errorMsg = "Cannot save item because the following items on the BOM are obsolete:\r\n" + obsoletePNs ;
return inn.newError(errorMsg);
} else
return this;