Relationship addition

Hello,

I would like to know how to sum up the two elements in the relationship tab and display the result in the "Cost" property of the parent item ?

Thank you !

  • Hi AlBer,

    the answer would be 25!

    Hope this helps,


    C

    Just kidding, of course. What you need to do here depends on your use case. Do you want to simply display that information on the parent item whenever you look at it, or do you want that info to be saved to the database for the parent item whenever the costs change?
    In the former case, you would want to set a Server Event on the ItemType of the parent Item with event onAfterGet. In this method, you need code like this:

    int thisCount = this.getItemCount();
    for (int i = 0; i < thisCount; i++)
    {
       var currentItem = this.getItemByIndex(i);
       var projectSellableItems = currentItem.getRelationships("Name Of the Relationship");
       int projectSellableItemsCount = projectSellableItems.getItemCount();
       int totalCost = 0;
       for (int j = 0; j < projectSellableItemsCount; j++)
       {
          var currentRelationship = projectSellableItems.getItemByIndex(j);
          totalCost += Convert.ToInt32(currentRelationship.getRelatedItem().getProperty("Name Of The Cost Property"));
       }

       currentItem.setProperty("Name Of The Total Cost Property", totalCost.ToString());
    }

    return this;

    This will display the total cost in the parent item and make it available to work with whenever you "get" it, but it will not store the info in the data base. If you want that you would have to implement similar code in a Server Event Method with event "onAfterUpdate" on the relationship ItemType instead.

    Hope this helps,

    C

  • Hello,

    Thank you for your complete answer Slight smile

    However this code cannot access the relationships. The :  projectSellableItemsCount = projectSellableItems.getItemCount(); line always returns 0, even when there are 3 or 4 relationships items.

    Here is the code I used :

    int thisCount = this.getItemCount();
    for (int i = 0; i < thisCount; i++)
    {
    var currentItem = this.getItemByIndex(i);
    var projectSellableItems = currentItem.getRelationships("Project Sellable Items");
    int projectSellableItemsCount = projectSellableItems.getItemCount();
    int totalCost = projectSellableItemsCount;
    for (int j = 0; j < projectSellableItemsCount; j++)
    {
    var currentRelationship = projectSellableItems.getItemByIndex(j);
    totalCost += Convert.ToInt32(currentRelationship.getRelatedItem().getProperty("cost"));
    }

    currentItem.setProperty("cost", totalCost.ToString());
    }

    return this;

  • Hi AlBer,

    that probably means that those relationships are not in the dom of 'this' when accessed in the server event (or that you used the wrong relationship name; make sure to use the name of the relationship, not the label). If that is the case, you need to do a

    currentItem.fetchRelationships("Project Sellable Items");

    first. This will fetch the relations from the data base and put them in the dom of currentItem. You can then access them with getRelationships() as described above.
    The reason why I did not include it in my first answer is that it obviously makes the code slower (any DB query will do that). To add to that, you probably should not simply add a "fetchRelationships" in the loop above. Depending on how many items are in 'this', that would yield extremely poor performance. What you could do instead is get all IDs of the Items in 'this', either by looping over them or using XPath, and then do one query to fetch all Relationships:

    Innovator inn = this.getInnovator();
    var ids = new List<string>();
    int thisCount = this.getItemCount();
    for (int i = 0; i < thisCount; i++)
       ids.Add(this.getItemByIndex(i).getID());

    var projectSellableItems = inn.newItem("Project Sellable Items", "get");
    projectSellableItems.setAttribute("select", "related_id(cost)");
    projectSellableItems.setProperty("source_id", "'" + string.Join("','", ids) + "'");
    projectSellableItems.setPropertyCondition("source_id", "in");
    projectSellableItems = projectSellableItems.apply();

    You now have all relationships for your 'this'-items and can access their cost properties via the related items in projectSellableItems. You simply need to make sure to associate them with the correct source_id.
    There might be a cleverer way of doing this, but this should get you some understanding of how to deal with a use case like this in Aras. As a final note: be careful with 'get'ing items in onAfter/BeforeGet methods. If you are trying to 'get' the ItemType that you are currently in an onAfterGet-method for, you will get stuck in an infinite loop. Make sure to include a serverEvents="0" attribute in the item query if you need to do that.

    Hope this helps,

    C

  • It worked !

    Thank you for your time  Slight smile