How to pass items in applyMethod?

Hi, I have a couple of huge server Methods, that share around 90% of the same code. So I want to unify them into one large Method, and use more compact calling Methods. My current problem is to past the context Item(s) to the main Method. When I call a server Method from a server Method, the selected Items are replaced with the Method item. This is what I have tried so far:
// Method triggered from Lifecycle
string body ="<keyword_>INSERT</keyword_><items_>" + this + "</items_>"; // ???
Item res = inn.applyMethod ("MyMainMethod", body);
return res;
// Main Method
Innovator inn = this.getInnovator();
XmlDocument xDoc = new XmlDocument();
if ((this.getAttribute("type", "") == "Method") & (this.node.SelectSingleNode("items_") != null))
{
  this.node = xDoc.CreateElement(this.node.SelectSingleNode("items_").ToString());
}
string thisItemTypeName = this.getItemByIndex(0).getAttribute("type",""); // will return Method instead of original items
Is it possible to overwrite the context of "this" with the item passed in applyMethod? Thanks for any input! Best regards, Angela
  • Hi Angela, I do not know if I understood you correctly but maybe this (from Aras Programmer's Guide) could help you: 4.1 Item Actions Extend the Item Class One purpose for Methods is to extend the Item Class. Methods extend the Item Class when they are bound as the related Item for ‘Item Action’ relationships on the ItemType. In the AML the Method name is the action attribute name for the Item tag. <Item type="My ItemType" action="My Method" id="…"/> The Method could be called using the IOM like this (all three examples below are equivalent and are written in C#):
    
    1) Item myItem = this.newItem("My ItemType", "My Method");
    myItem.setID(this.getID());
    Item results = myItem.apply();
    2) Item myItem = this.newItem("My ItemType");
    myItem.setID(this.getID());
    Item results = myItem.apply("My Method");
    3) Item myItem = this.newItem();
    myItem.setID(this.getID());
    myItem.setType("My ItemType");
    myItem.setAction("My Method");
    Item results = myItem.apply();
    
  • Hi Andreas, thanks for your help! Your code basically works, but I still have some trouble to hand over the complete Item that calls the Method:
    // Calling Method
    Innovator inn = this.getInnovator();
    Item myItem = this.newItem(this.getAttribute("type"), "MyMainMethod");
    myItem.setID(this.getID());
    myItem.setProperty("keyword_","INSERT");
    Item results = myItem.apply();
    if (results.isError())
    {
        return inn.newError("Error while Insert Data. " + results);
    }
    return results;
    
    // Main Method
    Innovator inn = this.getInnovator();
    string thisItemTypeName = this.getItemByIndex(0).getAttribute("type",""); // work! will return type of calling Item instead of Method
    // Parameters passed with Method call
    string keyword = this.getProperty("keyword_","invalid"); // works
    if (keyword == "..."){}
    
    // Get properties from calling Item
    int count = this.getItemCount(); // don´t work
    string pn = this.getItemByIndex(0).getProperty("item_number", "empty"); // don´t work, will always be empty, as "this" doesn´t contain the complete original Item.
    It would be possible to extend the calling Method to handover more properties. But this would make the calling Method more complex. One alternative is to extend the Method call so it contains the original Item: myItem.setProperty("myitem_", this); But I am not sure if passing "this" is really the best solution. This will move around a lot of data, as the Method shall apply to multiple items.
  • Hi Angela, have you tried this?
    
    this.apply("MyMainMethod");
    
  • Hi Andreas, no, of course I didn´t try that... Many thanks for this one, this version now does the job pretty well:
    Innovator inn = this.getInnovator();
    this.setProperty("keyword_","INSERT");
    Item res = this.apply("MyMainMethod");
    if (res.isError())
    {
        XmlDocument xDoc = new XmlDocument();
        xDoc.LoadXml(res.ToString());
        string faultstring = xDoc.SelectSingleNode("//faultstring").InnerText;
        return inn.newError("Error: " + faultstring);
    }
    return res;