Copy the related CR "product" text field value to the "product" text field on the CE form

Our CR and CE forms both have a "Product" text field.

The originator of the CR enters the product number in the CR product field.

I need that value to copy to the product text field on the CE when the CE is being created.

The CR is on the CE as the related CR and the product field is shown in the CE related CR pane.

The CE item type relationship name is a_CE_a_CR for the related CR

Can someone help me with the code that will copy the product value of the related CR to the product text field on the CE form?

Parents
  • Hello,

    There's a few options to consider for this. Based on your description, these suggestions will be made with the assumption that each CE will have one and only one related CR.

    The first option would be to handle this server-side. This easiest place to put this would be in an onAfterAdd server event on the a_CE_a_CR ItemType. Since this is a relationship between CE and CR, you would have references to both items in this server event. You can then look up what the product field of the related CR is and then do an edit on the parent CE to sync the values. This approach does not handle the case where the product field changes on the related CR after the relationship has already been added though.

    If you need to account for the product field on the CR changing, then you can instead use an onAfterAdd/onAfterUpdate event on the CR ItemType itself. This will be slightly more involved since you'll need to do another query inside of this event to get the CE that has a relationship to that CR.

    One other approach that would require some data model changes would be to use a foreign property. If there is indeed only one CR per CE, then you could remove the a_CE_a_CR relationship and instead just use an item property off of the CE ItemType that points to a CR. If you take this approach, you can use a foreign property to have a field on the parent CE that automatically shares its value with that of a property on the linked CR. 

    Chris


    Christopher Gillis

    Aras Labs Software Engineer

Reply
  • Hello,

    There's a few options to consider for this. Based on your description, these suggestions will be made with the assumption that each CE will have one and only one related CR.

    The first option would be to handle this server-side. This easiest place to put this would be in an onAfterAdd server event on the a_CE_a_CR ItemType. Since this is a relationship between CE and CR, you would have references to both items in this server event. You can then look up what the product field of the related CR is and then do an edit on the parent CE to sync the values. This approach does not handle the case where the product field changes on the related CR after the relationship has already been added though.

    If you need to account for the product field on the CR changing, then you can instead use an onAfterAdd/onAfterUpdate event on the CR ItemType itself. This will be slightly more involved since you'll need to do another query inside of this event to get the CE that has a relationship to that CR.

    One other approach that would require some data model changes would be to use a foreign property. If there is indeed only one CR per CE, then you could remove the a_CE_a_CR relationship and instead just use an item property off of the CE ItemType that points to a CR. If you take this approach, you can use a foreign property to have a field on the parent CE that automatically shares its value with that of a property on the linked CR. 

    Chris


    Christopher Gillis

    Aras Labs Software Engineer

Children
  • Chris,

    Thank you for your reply. the first option would work fine. We only have one CR in the related CR tab of the CE. Can you help me with the coding? I can't figure out how to get the related CR product value.

  • There are some examples of getting relationship data inside of the Programmer's Guide.

    This blog post also covers some examples of how to build AML using C#. While the examples are mainly for setting data, most of the sample code given can be converted to read data just by changing the function names from set to get

    The first thing to look at would be to see how much of the related item you have access to in the server event. You might have the whole item with all of its data, or you might just have the ID of the related item. If you have just the ID, you'll need to query for the item so you can actually get the product value.

    Chris

  • Chris, 

    I’m stuck. How do I “...look at would be to see how much of the related item you have access to in the server event.”? I referenced the Programmers Guide and blog but I’m not getting anywhere on solving this.

  • Hello,

    The general approach that I take can be broken down into a few steps:

    1. Try to get the information that you're looking for.
    2. Do some validation that the information exists and is correct.
    3. If the information is not correct or does not exist, query the database for it.
    4. Use the information

    Programmatically, this will look something like below. As a note, the sample here is pseudocode I mocked up based on the information in this post. You'll likely need to change some of the functionality and the ItemType and Property names to match those that are actually in your database.

    Innovator inn = this.getInnovator();
    // 1. Try to get the information
    Item relatedCR = this.getRelatedItem();
    string relatedProduct = relatedCR.getProperty("product", ""); // <-- The second argument here is a default value that will be used if the property does not exist
    // 2. Validate the information
    if (relatedProduct === "") // <-- Compare this to the default value we used above
    {
      // 3. If we get here, the property data did not exist in this context so we query for it
      Item relatedCrId = this.getProperty("related_id");
      relatedCR = inn.newItem("CR", "get");
      relatedCR.setID(relatedCrId);
      relatedCR.setAttribute("select", "product");
      relatedCR = relatedCR.apply();
      // We probably want to do some validation that this query succeeded
      if (relatedCR.isError())
      {
        // Do something. Returning the error here will display an error message to the user with the text of the error.
       return relatedCR;
      }
      // Otherwise, get the product information from the CR
      relatedProduct = relatedCR.getProperty("product", "");
    }
    // 4. Now use the product information of the CR
    Item sourceCeId = this.getProperty("source_id", "");
    Item sourceCE = inn.newItem("CE", "edit");
    sourceCE.setProperty("related_product", relatedProduct);
    sourceCE = sourceCE.apply();
    // Do the same kind of validation to make sure that this query succeeded
    if (sourceCE.isError())
    {
      // Do something
      return sourceCE;
    }
    // If we got here, all of our queries succeeded without issue
    return this;

    Chris