Forum Discussion

carstenw's avatar
carstenw
Creator II
9 years ago

How to get items on the BOM before save

Hi, the use case is as follows: A user adds items to a BOM, some of which have a (custom made) state of Obsolete I would like to get all the items on the BOM before the Part is saved, filter out the Obsolete and present their item_number in an error message to the user to instruct him to replace those. How do I go about that? I have tried a bit of XPath, of which I am a complete novice, but only managed to get back the items already saved on the BOM. The following is of course flawed, since the only items i can get from the this object are the ones already saved Any suggestions welcome :)
                        item = this;
			var relitemsList = item.getItemsByXPath("Relationships/Item[@type='Part BOM']");
			var relCount = relitemsList.getItemCount();

			for (int i = 0; i < relCount; i++)
			{
				var relItem = relitemsList.getItemByIndex(i);

				var bomItem = relItem.getRelatedItem();
				var relatedState = bomItem.getProperty("eht_production_lc_state");
				var relatedNumber = bomItem.getProperty("item_number");

				if (relatedState == "Obsolete")
				{
					affectedParts += relatedNumber + "\r\n";
				}
			}

			if (string.IsNullOrEmpty(affectedParts)) return item;

			var errorMsg = "Cannot save item because the following items on the BOM are obsolete:\r\n" + affectedParts;
			return inn.newError(errorMsg);

2 Replies

  • 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;
  • Thanks a million for your effort, that got me going in the right direction!