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 Reply Children
  • オフライン 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");