Remove certain property values onAfterVersion an Item

Hi, this one sounds like an easy to do task, but it´s somehow not. I want to reset certain ERP related properties after creating a new generation of a Part. My idea was to use a simple onAfterVersion Method for this task:
innnovator inn=this.getInnovator();

string partConfigId=this.getProperty("config_id");
string itemType=this.getType();

// Get newest version of this Part
Item newPart = this.newItem("Part","get");
newPart.setProperty("config_id", this.getProperty("config_id"));
newPart.setProperty("is_current","1");
newPart.setAttribute("select", "id");
newPart = newPart.apply();
if (newPart.isError())
{
    return inn.newError("Keine neue Revision gefunden: " + newPart.getErrorDetail() ); 
} 
string newId = newPart.getID(); // works!

// Edit newest version of this item
Item editPart = inn.getItemById("Part", newId);
editPart.setAction("edit");
editPart.setAttribute("doGetItem", "0");
editPart.setProperty("erp_is_matched","0");
editPart.removeProperty("erp_variant");
editPart = editPart.apply();
Unfortunately this code doesn´t update any value of the newest Part version. Even applySQL or grant of special permission doesn´t change anything. My only remaining idea is to customize related Methods like PE_CreateNewRevision and the transition handlers...again.. Is there some more general option available to change values after versioning an Item programatically? Best regards, Angela
  • Hi Angela, I had this issue a few months ago. It seems that after the OnAfterVersion event always a on***update event is fired. In this case it seems that this only contains few properties (anyhow: id). So maybe the following code snippet called by a onbeforeupdate event may help yo:
    string configId = this.getProperty("config_id");
    if(String.IsNullOrEmpty(configId))
    {
       // We are coming from change management, in case of 'normal' update configId isn't empty
       this.setProperty("erp_is_matched","0");
    } 
    
    I don't really understand why - but in my case it works. I hope this will help you. Best regards, Andreas
  • Hi Andreas, interesting solution! I made some tests with onBeforeAdd, but never came to the idea of using onBeforeUpdate. I can confirm, that your solution works for new item versions created by CM-processes. We also allow manual revising parts on certain conditions (change of major_rev, minor_rev and generation). The above solution will not work for these ones, but it´s not a big issue to extend Methods like PE_CreateNewRevision. Your code helps to avoid additional customizations in the transition handlers. I have customized them so often, I really don´t want to do it again. So your solution is big help! Thanks and best regards! Angela
  • This will be executed on afterversion we add 3 session state variables to the method remove_prop, newRevsion and newRevisionID these vars will be used in onbeforeupdate method of the same item remove_prop variable can be used in onbefore update see below in method 2 Method 1 action :-onafterversion //Collect old rev for comparison string old_rev = this.getProperty("major_rev"); // Get the latest generation Item newVer = this.newItem(this.getType(), "get"); newVer.setProperty("config_id", this.getProperty("config_id")); newVer.setProperty("is_current", "1"); newVer = newVer.apply(); string new_rev = newVer.getProperty("major_rev",""); string newRevId = newVer.getID(); // If new revision send the remove_Prop flag to be processed in onBeforeupdate() if (!new_rev.Equals(old_rev)) { RequestState.Add("remove_Prop", "1"); RequestState.Add("newRevision", new_rev); RequestState.Add("newRevisionID", newRevId); } Method 2 action: OnBeforeUpdate string removeProps= (string)RequestState["remove_Prop"]; string newRevision = (string)RequestState["newRevision"]; string newItemID = (string)RequestState["newRevisionID"]; // this will logic will only applied if the variable is availabe in session state if (!string.IsNullOrEmpty(removeProps)) { your logic to remove the props here } let me know if this helps you
  • Hi Sudarshan, thanks for the additional idea! I can confirm, that this one is also suitable! I right identified couple of tasks that I want to trigger after versioning an Item. So right now, I cannot tell, which of the two version here fits better to my application, but I can confirm that both of them work! Best regards! Angela
  • Hello Sudarshan and Angela, Thank you for this discussion.  I have been trying to do something very similar.  I implemented the state variable method in the OnBeforeUpdate event as you described but I am still not seeing the changes on the newly created generation of the part.  I have verified in the debugger that it does go through the logic of finding the set state variable and the variable has the right value in it and that the id of the part is the correct one (the new generation).  However, the property changes do not show up.  If I change the action to "edit" I get an  "ItemIsNotLocked"  exception.   Any idea what I might be doing wrong? Thanks, John Tensmeyer  
    // Called OnBeforeUpdate for Parts and Documents. string setRev= (string)RequestState["set_MinRev"]; // this logic will only applied if the variable is available in session state if (!string.IsNullOrEmpty(setRev)) { Item myPart = this.newItem(this.getType(), "edit"); myPart.setProperty("ws_minor_rev", setRev); myPart.setProperty("name","goober"); string newID = this.getID(); myPart.setID(newID); myPart.setAttribute("version","0"); myPart.setAttribute("serverEvents","0"); myPart = myPart.apply(); if (myPart.isError()) return myPart; } return this;
  • this.setProperty("desired_property","desired_value") I will not set double update on the same item with an extra item action since you are in the very context that item this.setProperty("","") should work
  • Sudarshan, You were correct.  Thanks for pointing that out.  I had tried that approach before when I was just using the OnAfterVersion approach but did not try it after splitting it into the OnBeforeUpdate. Thank you for helping me sleep better.  That problem was driving me crazy. John
  • Im glad that this approach worked for you. Do revert if you have any other issues that i can help you with /Sudarshan