VB,how to Update the parent property in the onafterupdate event

オフライン

I have an item type and a relationship
I want to calculate the sum of one of the columns of the relationship when the user presses the save button
and update it to the parent item.


If I put the method in the onafterupdate of the parent item, it will do an infinite loop
If I put the method in the onafterupdate of the relationship, when I update the value of more than one relationship, it will prompt me that 「Aras.Server.Core.ItemIsNotLockedException」...

'this is my parent onafterupdate method code

Dim inn As Innovator = Me.getInnovator()
Dim score_1 as Integer = Me.getProperty("score_1") ' parentItem.自評分數

Me.fetchRelationships("Performance_SelfScore") 
Dim rs as Item = Me.getRelationships("Performance_SelfScore") 'get relationship
Dim rsCnt as Integer = rs.getItemCount()

If rsCnt < 1 Then

Else
   Dim i,self_score,sum_self_score as Integer
   sum_self_score=0

   For i=0 to rs.getItemCount() -1
      self_score = rs.getItemByIndex(i).getProperty("self_score")
      sum_self_score = sum_self_score + self_score

   Next
   Dim score1 as String = Me.getProperty("score_1")
   Dim successor_job_cname as String = Me.getProperty("successor_job_cname")
   
   Me.setProperty("score_1",sum_self_score)

   '
   '↓↓The following code is what I use to update the parent property
   'The first way

   'Dim Self_Eval As Item = Me.newItem("Performance_Self_Evaluation","edit")
   '       Self_Eval.setAttribute("where","id ='" + Me.getID() + "'")
   '       Self_Eval.setProperty("score_1",sum_self_score)
   '       Self_Eval.setProperty("successor_job_cname","approve_mg")'day_auth.getProperty("approve_mg"))
   '       Self_Eval = Self_Eval.apply()
   'If Self_Eval.isError() Then
   '   Return inn.newError ("Performance_VB_test:Error Update Performance_Self_Evaluation " & Self_Eval.getErrorDetail())
   'End If

   '
   'Second method
   'Dim Self_Eval As Item = inn.getItemById("Performance_Self_Evaluation", Me.getID())
   '       Self_Eval.setAction("edit")
   '       Self_Eval=Self_Eval.apply()
   '       Self_Eval.setProperty("score_1",sum_self_score)
   '   

   'The third way
   'Me.setAction("edit")
   'Me.setProperty("score_1",sum_self_score)
   'Dim resultItem As Item = Me.apply()
   'Me.setAction("")
   '↑↑
   

   score1 = Me.getProperty("score_1")
   successor_job_cname = Me.getProperty("successor_job_cname")

End If


Can anyone give me any idea?

Parents
  • Hi

    I was using another approach by hooking the method to on form populated event of the form

    Steps to be followed are

    Step 1: Create a Javascript method and add below code

    var innovator = new Innovator();
    var action = "edit";
    if(aras.isLocked(parent.item))
    {
    action = "update";
    }
    var parentField = getFieldByName("score_1");
    parentField.getElementsByTagName("input")[0].disabled = true;
    var rs = innovator.newItem("Performance_SelfScore", "get");
    rs.setProperty("source_id", document.thisItem.getProperty("id"));
    rs.setAttribute("Select", "self_score");
    rs = rs.apply();
    if (rs.isError())
    {
    return rs;
    }
    var sum_self_score = 0;
    var self_score = 0;
    for (var i = 0; i < rs.getItemCount(); i++)
    {
    self_score = rs.getItemByIndex(i).getProperty("self_score");
    sum_self_score = sum_self_score + self_score;
    }
    var currentScore = document.thisItem.getProperty("score_1","");
    if(currentScore != sum_self_score)
    {
    var Self_Eval = innovator.newItem("Performance_Self_Evaluation", action);
    Self_Eval.setAttribute("id", document.thisItem.getProperty("id",""));
    Self_Eval.setProperty("score_1", sum_self_score);
    var Self_Eval = Self_Eval.apply();
    aras.uiReShowItem(parent.thisItem.getID(), parent.thisItem.getID());
    }

    step 2: Add this method to Performance_Self_Evaluation form populate event

    Test and let me know if it works

    Thank You

    Gopikrishnan R

  • オフライン in reply to Gopikrishnan

    hi ,Gopikrishnan

      First of all, thank you for your reply!
      I tested this code, but I get the error message "parent.thisItem is undefined" or "menuFrame.populateAccessMenu is not a function".

      

      
      Later I changed the
      aras.uiReShowItem(parent.thisItem.getID(), parent.thisItem.getID());
      changed to
      "aras.uiReShowItem(document.thisItem.getProperty("id"), document.thisItem.getProperty("id"));
      There is still the error message "menuFrame.populateAccessMenu is not a function"...

      And it also seems to cause infinite loops when placed under this event

      

  • オフライン in reply to Mavis

    Hi Mavis

    Can you try below steps

    Remove all the methods and event added as per my previous suggestion.

    Step 1: Create new C# Server Method and add below code

    Innovator inn = this.getInnovator();
    int currentValue = Int32.Parse(this.getProperty("score_1"));
    Item relItm = inn.newItem("Performance_SelfScore", "get");
    relItm.setProperty("source_id", this.getID());
    relItm.setAttribute("select", "self_score");
    relItm = relItm.apply();
    int sum_self_score = 0;
    for (int i = 0; i < relItm.getItemCount(); i++)
    {
    sum_self_score = sum_self_score + Int32.Parse(relItm.getItemByIndex(i).getProperty("self_score","0"));
    }
    if(currentValue != sum_self_score)
    {
    Item updateItem = inn.newItem("Performance_Self_Evaluation", "edit");
    updateItem.setAttribute("id", this.getID());
    updateItem.setProperty("score_1", sum_self_score.ToString());
    updateItem = updateItem.apply();
    }
    return this;

    Add this method to Performance_Self_Evaluation item type server Events OnAfterUpdate event

    Thank You

    Gopikrishnan R

Reply
  • オフライン in reply to Mavis

    Hi Mavis

    Can you try below steps

    Remove all the methods and event added as per my previous suggestion.

    Step 1: Create new C# Server Method and add below code

    Innovator inn = this.getInnovator();
    int currentValue = Int32.Parse(this.getProperty("score_1"));
    Item relItm = inn.newItem("Performance_SelfScore", "get");
    relItm.setProperty("source_id", this.getID());
    relItm.setAttribute("select", "self_score");
    relItm = relItm.apply();
    int sum_self_score = 0;
    for (int i = 0; i < relItm.getItemCount(); i++)
    {
    sum_self_score = sum_self_score + Int32.Parse(relItm.getItemByIndex(i).getProperty("self_score","0"));
    }
    if(currentValue != sum_self_score)
    {
    Item updateItem = inn.newItem("Performance_Self_Evaluation", "edit");
    updateItem.setAttribute("id", this.getID());
    updateItem.setProperty("score_1", sum_self_score.ToString());
    updateItem = updateItem.apply();
    }
    return this;

    Add this method to Performance_Self_Evaluation item type server Events OnAfterUpdate event

    Thank You

    Gopikrishnan R

Children
  • オフライン in reply to Gopikrishnan

    hi,Gopikrishnan

      Thanks again for taking the time to help me!
      You wrote the method in C# ,it works!
      I have also rewritten the original VB code I wrote
      plus determine if the current score_1 is the same as the previous score_1
      It will not loop infinitely and it will run!

      Also, about the code you wrote in the previous paragraph (Javascript)
      I will
      "aras.uiReShowItem(parent.thisItem.getID(), parent.thisItem.getID());"
      change to
      "document.thisItem.setProperty("score_1", sum_self_score);
      handleItemChange("score_1",sum_self_score);"
      It works too!

      Thank you!!

  • オフライン in reply to Mavis

    Glad to hear. Finally all 3 solutions are working as expected :)