This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

DEVELOPERS FORUM - Problem while deleting related item.

zapping - Wednesday, May 19, 2010 5:03 AM:

Hi,

Can you please help me with this.

There are 3 items
manufacturer (source)
manufacturer contacts (related)
manufacturers contacts (relationship). Its one to many between manufacturer and manufacturers contacts

The following works fine.
1. when a manufacturer is added a server onAfterAdd event is set to add the same entry to manufacturer contacts related item and also to set manufacturers contacts relationship.
2. a form for manufacturer contacts was created and when a new entry is make an onAfterAdd event is set to manufacturer contacts to create a relationship to manufacturers contacts.

The same needs to happen with delete and edit also hence
3. When a manufacturer is deleted a server event onBeforeDelete is set to delete the corresponding entries from manufacturers contacts and manufacturer contacts.

The problem is when deleting an entry from manufacturer contacts. Tried setting an onBeforeDelete event to manufacturer contacts to remove the corresponding entry from manufacturers contacts. But it does not seem to be working. I get a message saying referenced item is found. The code for the event is as follows:

//find relationship
Item relItem = this.newItem();
relItem.setType("Manufacturers Contacts");
relItem.setAction("get");
relItem.setProperty("related_id", this.getID());
relItem = relItem.apply();
if (relItem.isError()) {
  return relItem;
}

//change action to delete
relItem.setAction("delete");
Item result = relItem.apply();
if (result.isError()) {
  return result;
}

return this;



RobMcAveney - Wednesday, May 19, 2010 4:11 PM:

You may want to consider using a slightly different data model.  If Manufacturer Contacts can only be used under one Manufacturer (i.e. not shared contacts), then you can store the contact info on the relationship and have no related item.  This would simplify the server events needed, I think.  See the Product ItemType and the Model RelationshipType as an example of this.

If you choose to stick with the data model you have, try using the Dependent flag on the Manufacturer Contacts ItemType.  Dependent items get deleted automatically when they are no longer used.

One other note: you could add the initial Manufacturer Contacts using an OnBeforeAdd instead of an OnAfterAdd.  The advantage to this approach would be that all items would be added in a single transaction and any errors would cancel the entire transaction.



zapping - Thursday, May 20, 2010 7:37 AM:

Hi Rob,
  The data model you have mentioned seems to be a nice alternative but have limitations as its something thats carried forward from an external system that needs to be integrated with Aras so have to keep it. This is the code i have for the add event can you let me know how dependency can be set in this. Thanks a bunch for the tips.

//set relationship item.
Item itemRelShip = this.newItem();
itemRelShip.setType("Manufacturers Contacts");
itemRelShip.setAction("add");
itemRelShip.setProperty("source_id",this.getID());

//create, set and add related item
Item itemCont = itemRelShip.createRelatedItem("Manufacturer Contacts", "add");
itemCont.setProperty("idcode", this.getProperty("idcode"));
itemCont.setProperty("conname", this.getProperty("conname"));
Item result  = itemRelShip.apply();

return result;

cheers zaps.



RobMcAveney - Thursday, May 20, 2010 10:04 AM:

The Dependent flag should be set on the Manufacturer Contacts ItemType definition (the related item), not in code.  Once it is set you should not need any logic to delete the contacts.

Try this code OnBeforeAdd instead of the OnAfterAdd code you show.  You should get the same results, but in a single transaction:

Item itemRelShip = this.createRelationship("Manufacturers Contacts","add");
Item itemCont = itemRelShip.createRelatedItem("Manufacturer Contacts", "add");
itemCont.setProperty("idcode", this.getProperty("idcode",""));
itemCont.setProperty("conname", this.getProperty("conname",""));

This code modifies the add request for the Manufacturer item to include adds for the Manufacturers Contacts relationship and the Manufacturer Contacts related item.  Everything is then done in one transaction, instead of waiting until after the Manufacturer is added, then going back and adding the relationship/related.



zapping - Thursday, May 20, 2010 11:57 AM:

Oh ok. Guess yesterday was not all that nice. Hoping today will be a better one at work.