Forum Discussion

Thomas_Hotz's avatar
Thomas_Hotz
Ideator I
9 years ago

Search an item by id across all item types

I sometimes had the problem that I have an ID of an Item within Aras, but I don't know the Item Type that it belongs to. (for example, I find the id in an error log somewhere but it's not clear where it came from.) Since I don't know any direct way of searching an item through all item types, I created an easy search util for that. It consists of a server-side method, a client-side method and a generic action that I can call from the main aras window. This is what the code does: - Ask the user to enter an 32-char ID. - Search through all item types for an item that has this ID - Show the result of the search: the ItemType and the keyed_name of the item that was found - It also asks the user if he/she wants to open the aras tear-off window for that item. Maybe this is helpful for other Aras users as well, so I post the code here (it's the AML that you can use to import as a small package or through AML Studio). Thomas Server side method:
<AML>
 <Item type="Method" id="6FBCE5A606B44A25AD805C1418B88388" action="add">
  <comments>Admin action to search for an ID in all ItemTypes</comments>
  <execution_allowed_to keyed_name="Administrators" type="Identity">2618D6F5A90949BAA7E920D1B04C7EE1</execution_allowed_to>
  <method_code><![CDATA[var inn = this.getInnovator();

// Validate input
var searchId = this.getProperty("search_id");
if (searchId == null)
{
    return inn.newError("Please provide a search_id");
}
if (searchId.Length != 32)
{
    return inn.newError("search_id needs to be 32 characters.");
}

// Search all available ItemTypes
var searchForItemTypes = inn.newItem("ItemType", "get");
searchForItemTypes.setAttribute("select", "name,instance_data");
searchForItemTypes.setAttribute("where", "implementation_type='table'"); // ignore polymorphic and federated ItemTypes
var allItemTypes = searchForItemTypes.apply();

// In every ItemType, search for the given ID
for (int i = 0; i < allItemTypes.getItemCount(); i++)
{
    var itemType = allItemTypes.getItemByIndex(i);
    var itemTypeName = itemType.getProperty("name");
    var tableName = itemType.getProperty("instance_data");
    // Search via SQL to avoid any permission problems
    var sql = "select keyed_name from [" + tableName + "] where id='" + searchId + "'";
    var res = inn.applySQL(sql);

    // If one item is found, return the result.
    if (res.getItemCount() > 0)
    {
        res.setAttribute("type", itemTypeName);
        return res;
    }

}

// No ID found, return error message
return inn.newError("ID " + searchId + " not found!");]]></method_code>
  <method_type>C#</method_type>
  <name>s_find_element_by_id_server</name>
 </Item>
</AML>
Client-side method:
<AML>
 <Item type="Method" id="7CF952DCB88F4EC5A2376E320AFDD05E" action="add">
  <comments>Admin action to search for a given ID in any ItemType</comments>
  <execution_allowed_to keyed_name="Administrators" type="Identity">2618D6F5A90949BAA7E920D1B04C7EE1</execution_allowed_to>
  <method_code><![CDATA[var inn = this.getInnovator();
        

// Ask the user for an ID
top.aras.prompt("Please enter ID:", "").then(
    
    // Callback function after the prompt
    function (search_id) {
    
        if (search_id===undefined) {
            // User canceled the dialog
        } else {
            
            // Check server for ID
            var r = inn.applyMethod("s_find_element_by_id_server","<search_id>" + search_id + "</search_id>");
        
            // If server returned an error, show error to user
            if (r.isError()) {
                top.aras.AlertError(r.getErrorString());
            } else {
                
                // If server found an item, show the ItemType and Keyed name of the item,
                // and ask user if he wants to see the form of the item
                if (top.aras.confirm("Search for ID: " + search_id + "\n\n" +
                    "ItemType: " + r.getType() +"\n" + 
                    "Keyed name: " + r.getProperty("keyed_name") + "\n\n"+
                    "Do you want to open the form of the item?")) {
            
                    // open form of item, if requested
                    top.aras.uiShowItem(r.getType(), search_id);
                }
            }
        }    
    }
);
]]></method_code>
  <method_type>JavaScript</method_type>
  <name>s_find_element_by_id_client</name>
 </Item>
</AML>
Action:
<AML>
 <Item type="Action" id="BAFCB980FF014963B7A6D394774F0D08" action="add">
  <item_query />
  <label xml:lang="en">Find Element By ID</label>
  <location>client</location>
  <method keyed_name="s_find_element_by_id_client" type="Method">
   <Item type="Method" action="get" select="id">
    <name>s_find_element_by_id_client</name>
   </Item>
  </method>
  <target>none</target>
  <type>generic</type>
  <name>s_find_element_by_id</name>
 </Item>
</AML>

4 Replies

  • PS: Is there a way of adding attachments in this forum? It might have been helpful if I could had put my code into a package and attach it as a zip file instead of posting the code directly as text. Also, I would have like to attach a screenshot of my application but I didn't know how to upload a picture. Sorry if I ask stupid questions, this is my first post here... :-)
  • Thomas, I think your solution is good but it overkill for your task. What you can do is:
      1. create Poly ItemType
        set in Poly Source tab all ItemTypes that you want to search in for your ID - I guess you don't want to search all internal tables, but if so you can add them too
        search using simple API: innovator.getItemById("{YOUR POLYITEM NAME}", "{ID}"); or using AML: <AML><Item type='YOUR POLYITEM NAME}' action='get' id='{ID}' /></AML>
    About attachment question: you can upload your files to some cloud and set link as public and link here using link button in text editor
  • Zahar, thanks for your comments. The idea with a poly itemtype is also good, however I think it's a lot more effort to maintain. Whenever I create a new itemtype (and we do this frequently since we're just in the process of implementing Aras), I would need to update the poly item definition to include the new itemtype. And if I want to delete an itemtype (which also happens now and then) I would first need to remove it from the poly definition before I can delete it. So I think my solution is easier to maintain over time.
    you can upload your files to some cloud and set link as public and link here using link button in text editor
    Hmmm... that's not very convenient. It almost sounds like if I implement Aras as a PLM system and I tell my users that they should use Aras to maintain parts and BOMs, but when they have a drawing for a part, they should put it to a network share and post a link in Aras. Doesn't sound very good to me. I really think if Aras wants us to use this community platform more frequently, they should allow to post screenshots and add attachements directly. Thomas
  • Hi Thomas, If you are interested in sharing your method code, I recommend posting your package to an open source code sharing site like GitHub. You can then email the GitHub link to araslabs@aras.com and the Aras Labs team will review the project for the Aras Community Projects list. http://community.aras.com/projects Eli Aras Labs Software Engineer