How to get parent Item type recursively

オフライン
I have structure where, I have to find all the parent Item in my BOM tree, so How to find it by aml query, or  any server side method for finding all parent Item in BOM.  
Parents
  • Former Member
    Former Member
    Hi Maddy, As I understood your query, your are trying to do reverse operation using GetItemRepeateconfig. You can try below AML then, <AML> <Item type="Part BOM" action="get" repeatProp="source_id" repeatTimes="2" select="source_id(name)" > <related_id> <Item type="Part" action="GetItemRepeatConfig" select="name" id="{your bom part id}"></Item> </related_id> </Item> </AML>
Reply
  • Former Member
    Former Member
    Hi Maddy, As I understood your query, your are trying to do reverse operation using GetItemRepeateconfig. You can try below AML then, <AML> <Item type="Part BOM" action="get" repeatProp="source_id" repeatTimes="2" select="source_id(name)" > <related_id> <Item type="Part" action="GetItemRepeatConfig" select="name" id="{your bom part id}"></Item> </related_id> </Item> </AML>
Children
  • I've tried this and it doesn't repeat since you put the fix id to the related_id's Item. It always do one level I think.

  • オフライン in reply to TU_YINING

    Hi   Could you please help me how to find this? Advance Thanks...
    Recursive parent...

  • オフライン in reply to Ananta Jena

    Here is a sample for recursive search for forward and backward.

    ////////////////////////////////////////////////////////////////////
    ///   - IN:
    ///     - RequestState
    ///     - [String] initIDs: initial IDs (csv)
    ///     - Properties
    ///     - [String] direction: forward/backward
    ///     - [String] itemtype: itemtype's name of RELATIONSHIP
    ///     - [String] is_all_level: return all levels or only the top/end
    ///       - 0(default): top/end only
    ///       - 1: all levels (all children or all parents)
    ///   - OUT:
    ///   - RequestState
    ///     - [String] returnIDs: target IDs (csv)
    ///   - newResult()
    ///     - 0   : normal
    ///     - E003: Bad Args
    ///     - E004: No target Items
    ///
    ////////////////////////////////////////////////////////////////////
    
    Innovator inn = this.getInnovator();
    List<string> targetList;
    string direction, itemtype;
    
    try
    {
      string temp = (string)RequestState["initIDs"];
      targetList = temp.Split(',').ToList();
      RequestState.Remove("initIDs");
    }
    catch (Exception e)
    {
      return inn.newResult("E004");
    }
    
    try
    {
      direction = this.getProperty("direction");
      itemtype = this.getProperty("itemtype");
      if (direction != "forward" && direction != "backward")
      {
        throw new Exception("Unvalid parameter.");
      }
    }
    catch (Exception e)
    {
      return inn.newResult("E003");
    }
    
    var relatedList = new List<string>();
    
    var libraryList = new List<string>();
    
    try
    {
      while (targetList.Count != 0)
      {
        for (int i = targetList.Count - 1; i >= 0; i--)
        {
          if (libraryList.Contains(targetList[i]) == false)
          {
            Item sourceItem = inn.newItem(itemtype, "get");
            sourceItem.setAttribute("select", direction == "forward" ? "related_id" : "source_id");
            sourceItem.setProperty(direction == "forward" ? "source_id" : "related_id", targetList[i]);
            Item rtItem = sourceItem.apply();
            int rtCount = rtItem.getItemCount();
            if (rtCount == 0)
            {
              relatedList.Add(targetList[i]);
            }
            else
            {
              for (int j = 0; j < rtCount; j++)
              {
                Item resultItem = rtItem.getItemByIndex(j);
                targetList.Add(resultItem.getProperty(direction == "forward" ? "related_id" : "source_id"));
              }
            }
            libraryList.Add(targetList[i]);
          }
          targetList.RemoveAt(i);
        }
      }
    }
    catch (Exception e)
    {
      // unknown Relationship itemtype
      return inn.newResult("E003");
    }
    
    if (this.getProperty("is_all_level") == "1")
    {
      RequestState.Add("returnIDs", String.Join(",", libraryList));
    }
    else
    {
      RequestState.Add("returnIDs", String.Join(",", relatedList));
    }
    return inn.newResult("0");