Adding a related item to a null relationship on onAfterAdd Event of the relationship.

Hi,
I have a relationship defined between two item types say part and document
and the relationship options available on the form are "No related" and "Create Only"
I have a scenario such that when "no related" option is selected and I populate the property values, on onAfterAdd event of the relationship based on some conditions I want to attach a related item(creating from my code) to it.

however even though I am able to achieve this on the "on before update" event. I am unable to do it on onAfterAdd event.
is there any way to achieve this functionality from the onAfterAdd event??
also I would like to know why this.setProperty("related_id",idOfrelatedItem) is working on "onBeforeUpdate" and not on "onAfter/BeforeAdd" events.

  • The Method this.setProperty("related_id",idOfrelatedItem) is not the problem here,the problem is 'this' is the context item returned to the user, not the item to be saved.

    Assuming that "this" is a single item, or you will have to loop for each item, you can use:

    if (this.getProperty("related_id","") == "")
    {
     Item editItem = innovator.newItem("ItemType","get");
     editItem.setProperty("id",this.getProperty("id",""));
     editItem =editItem.apply();

     if (editItem.getItemcount() == 1)
     {
      editItem.setAction("edit");
      editItem.setProperty("related_id",idOfrelatedItem);
      editItem = editItem .apply()
     }
    }

    This should update the item, but I believe it will trigger a new onAfterUpdate Event, so make sure you check for the presence of the related-property and idOfrelatedItem and only perform the update once.