Xavier Bertschy - Monday, September 29, 2014 11:42 AM:
Hello,
I am trying to write a vb.net script to prompte a list of Part BOM relationships items from a selected Part. Below is my last try and the error was: "Not a single Item". I assume the error is because there is a set of Items, but I try to prompt the items by a "getItemByIndex()". Does anyone can help me out?
I wrote the same script within an ARAS Method and it's working fine, I do not understand why.
Below is my code:
Dim part_select As Aras.IOM.Item = MyInnovator.getItemById("Part", "01391FC6931A4BFDB8A0BDB34C80D9DE")
part_select = part_select.apply()
Dim partBom As Aras.IOM.Item = MyInnovator.newItem("Part BOM", "get")
partBom.setAttribute("select", "name,item_number")
part_select.addRelationship(partBom)
Dim count As Integer = part_select.getItemCount()
Dim results As Aras.IOM.Item = part_select.apply()
Console.WriteLine("Number of relation : " & count)
Dim item As Aras.IOM.Item = results.getRelationships()
Dim bom As Aras.IOM.Item = item.getRelatedItem()
Dim i As Integer = 0
For i = 0 To count
Console.WriteLine("nom : " & bom.getItemByIndex(i).getProperty("name", "na"))
Next
Thanks!
Xavier
DavidSpackman - Wednesday, October 1, 2014 9:44 PM:
Hi Xavier,
Are you able to debug this bit of code when you run it?
Sometimes it helps to debug the code to see what AML the IOM is generating.
Couple of issues I can see
- There are easier method to get the Part and Part BOM relationships
- Part BOM doesn't have the properties name or item_number
- You are only getting the item count of part (1 result) not the Part BOM(s) (n result(s)) (Also you check the count before you apply the BOM)
See the Aras Programmers guide for a good example (7.5 Query for an Item and return its configuration)
Here is an modified example from the programmers guide in C#
// Set up the query Item.
Item qryItem = MyInnovator.newItem("Part", "get");
qryItem.setAttribute("select", "item_number,description,name");
qryItem.setID("01391FC6931A4BFDB8A0BDB34C80D9DE");
// Add the BOM structure.
Item bomItem = MyInnovator.newItem("Part BOM", "get");
bomItem.setAttribute("select", "quantity, related_id(item_number,description,name)");
qryItem.addRelationship(bomItem);
// Perform the query.
Item results = qryItem.apply();
// Test for an error.
if (results.isError())
{
Console.WriteLine("Item not found: " + results.getErrorDetail());
}
// Get a handle to the BOM Items.
Item bomItems = results.getRelationships();
int count = bomItems.getItemCount();
int i;
// Iterate over the BOM Items.
for (i = 0; i < count; ++i)
{
// Get a handle to the relationship Item by index.
Item bom = bomItems.getItemByIndex(i);
// Get a handle to the related Item for this relationship Item.
Item bomPart = bom.getRelatedItem();
//Write Name Property
Console.WriteLine(bomPart.getProperty("item_number", "") + ": " + bomPart.getProperty("name", ""));
}
Xavier Bertschy - Thursday, October 2, 2014 8:27 AM:
Thank you very much David!
I should have seen those mistakes.
Nevertheless, there is still something which bothering me. From a selected "Part BOM" item, I am able to get the related_id and source_id. But if I want to select an item with a specified source_id and then return its related_id, it doesn't return any results. Here is my code:
Dim bom As Item = inn.newItem("Part BOM", "get")
bom.setAttribute("source_id", "9040AB16F58B461E9A3563A24EB898BA")
bom.setAttribute("select", "related_id")
MsgBox(bom.getItemByIndex(0).getProperty("related_id"))
Am I doing something wrong?
DavidSpackman - Monday, October 6, 2014 1:03 AM:
Hi Xavier,
(Sorry I didn't see an email about this)
Your error is because of a syntax issue
bom.setAttribute("source_id", "9040AB16F58B461E9A3563A24EB898BA")
should be
bom.setProperty("source_id", "9040AB16F58B461E9A3563A24EB898BA")
Where you able to debug these queries?
Remember the IOM is just generating AML to send to the server.
Sometimes is it easier to look at the AML and understand why the query isn't returning the expected output
Your Generated AML
<Item type="Part BOM" action="get" select="related_id" sourceid="9040AB16F58B461E9A3563A24EB898BA"></Item>
Correct AML
<Item type="Part BOM" action="get" select="related_id">
<source_id>9040AB16F58B461E9A3563A24EB898BA</source_id>
</Item>
When I have trouble with the IOM I will often get the generated AML and use AML Studio to troubleshoot.
In this case you could see that "Item" doesn't have an attribute called "source_id"
Dave
Xavier Bertschy - Tuesday, October 7, 2014 10:07 AM:
Thanks a lot David!
Your explanations helps me a lot. I didn't have the reflex to check the AML with AML Studio! By the way, is there any way to get the generated AML by Visual Studio?
Xavier
DavidSpackman - Wednesday, October 8, 2014 1:05 AM:
Hi Xavier,
Add the property to the watch window
The xml will be under the DOM -> InnerXml property
I usually then copy that string to Notepad++ where I have XPatherizerNPP plugin installed. It allows you to format / syntax highlight the XML very quickly.
Example
