Update Parent Item when Relationships Tab is Changed
Good day all,
I have ItemType sm_ITA with a Relationship of sm_ITA_Assignment. sm_ITA_Assignment (Assignments) is not related to another ItemType. When an Assignment is added or deleted I need the sm_license_count property of the parent sm_ITA to be updated with the count of Assignments. I have read several posts that seemed to be similar to this, but can't get anything to work. I have methods that will change sm_license_count on a save if it doesn't match the Assignment count, but it will not allow me to add or delete Assignments. I have a method that will make the update, but the sm_ITA has to be saved twice. Thanks for any help that can be provided of what I am missing.
|
Will Update Value - sm_ITA Server Event onBeforeUpdate - Doesn't allow changes, additions, or deletions to Assignments Won't Update - sm_ITA Server Event onAfterUpdate |
|
this.fetchRelationships("sm_ITA_Assignment"); Item RelationshipsItem = this.getRelationships("sm_ITA_Assignment"); string count = "" + RelationshipsItem.getItemCount(); this.setProperty("sm_license_count", count); return this; |
|
Item ITA ("edit") - onBeforeUpdate & onAfterUpdate - Returns an error of "An item with the same key has already been added." Item ITA ("get") - onBeforeUpdate & onAfterUpdate - Does nothing var ITA ("edit") - onBeforeUpdate & onAfterUpdate - Returns an error of "An item with the same key has already been added." var ITA ("get") - onBeforeUpdate & onAfterUpdate - Does nothing |
|
Innovator inn = this.getInnovator(); int currentValue = Int32.Parse(this.getProperty("sm_license_count")); Item relItm = inn.newItem("sm_ITA_Assignment", "get"); relItm.setProperty("source_id", this.getID()); relItm = relItm.apply(); int totalCount = relItm.getItemCount(); // Number of entries in sm_ITA_Assignements string count = totalCount.ToString(); if(currentValue != totalCount) { Item ITA = inn.newItem ("sm_ITA", "edit"); // have tried with get ITA.setID (this.getID()); ITA.setProperty ("sm_license_count", count); ITA = ITA.apply(); } return this; |
Try this code attached to OnBeforeUpdate and OnBeforeAdd server events sm_ITA item type
[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:e6c7537c-0d26-4f90-8421-7423cc484fd4:type=csharp&text=var%20addRels%20%3D%20this.node.SelectNodes%28%22Relationships%2FItem%5B%40type%3D%27sm_ITA_Assignment%27%20and%20%40action%3D%27add%27%5D%22%29%3B%0D%0Avar%20delRels%20%3D%20this.node.SelectNodes%28%22Relationships%2FItem%5B%40type%3D%27sm_ITA_Assignment%27%20and%20%40action%3D%27delete%27%5D%22%29%3B%0D%0Astring%20stcurlicenses%20%3D%20this.getProperty%28%22sm_license_count%22%29%3B%0D%0Aint%20curLicenses%20%3D%20string.IsNullOrEmpty%28stcurlicenses%29%20%3F%200%20%3A%20Int32.Parse%28stcurlicenses%29%3B%0D%0AcurLicenses%20%3D%20curLicenses%20%2B%20addRels.Count%20-%20delRels.Count%3B%0D%0Athis.setProperty%28%22sm_license_count%22%2C%20curLicenses.ToString%28%29%29%3B%0D%0Areturn%20this%3B]