Update Parents Item

Hi I create item "ATEN_VPN"  add relationship "ATEN_VPN_File". When I save ATEN_VPN I need to know how many count in "ATEN_VPN_File"  and save in ATEN_VPN In ATEN_VPN Server Event "onBeforeUpdatae"  add this method Code: Innovator innovator = this.getInnovator(); ArasTool aras = new ArasTool(innovator); string formName=this.getType(); string formId = this.getID(); Item formItem = innovator.newItem(formName,"get"); formItem.setID(formId); formItem=formItem.apply(); formItem.fetchRelationships("ATEN_VPR_File"); Item relationships = formItem.getRelationships("ATEN_VPR_File"); int count = relationships.getItemCount(); formItem.setProperty("_h_test",count.ToString()); formItem=formItem.apply("edit"); return this; ------------------------------------------------------- But apply will show error message:An item with the same key has already been added. I don't know why.    
Parents
  • Hi Michelle, I think the issue you're running into is related to updating the item within an onBeforeUpdate itself. You may be entering into an issue where each update of the item within the server event is triggering it's own onBeforeUpdate event. I was able to work around this issue in a test environment by simplifying the sample of code provided. Within the onBeforeUpdate event, we already have access to the item we want to check the relationships on through the context item (this). Additionally, we don't need to apply an edit since that will automatically take place once the onBeforeUpdate finishes. All that needs to be done is to set the property on this before we return from the method. You can see the sample code that worked in my environment below.
    // Fetch any relationships that may not exist in this context
    this.fetchRelationships("ATEN_VPR_File");
    Item relationships = this.getRelationships("ATEN_VPR_File");
    // Get the count of the relationships
    int count = relationships.getItemCount();
    // Store the count in the property on the item
    this.setProperty("_h_test", count.ToString());
    // And let the apply that will be called after this onBeforeUpdate save that property to the database
    return this;
    Chris
    Christopher Gillis Aras Labs Software Engineer
Reply
  • Hi Michelle, I think the issue you're running into is related to updating the item within an onBeforeUpdate itself. You may be entering into an issue where each update of the item within the server event is triggering it's own onBeforeUpdate event. I was able to work around this issue in a test environment by simplifying the sample of code provided. Within the onBeforeUpdate event, we already have access to the item we want to check the relationships on through the context item (this). Additionally, we don't need to apply an edit since that will automatically take place once the onBeforeUpdate finishes. All that needs to be done is to set the property on this before we return from the method. You can see the sample code that worked in my environment below.
    // Fetch any relationships that may not exist in this context
    this.fetchRelationships("ATEN_VPR_File");
    Item relationships = this.getRelationships("ATEN_VPR_File");
    // Get the count of the relationships
    int count = relationships.getItemCount();
    // Store the count in the property on the item
    this.setProperty("_h_test", count.ToString());
    // And let the apply that will be called after this onBeforeUpdate save that property to the database
    return this;
    Chris
    Christopher Gillis Aras Labs Software Engineer
Children
No Data