Forum Discussion

bryan-riley's avatar
bryan-riley
Creator II
5 months ago
Solved

Refreshing xClass Field on Form in Field Event OnChange

So I wrote a client side method so when you change the Classification of a part, it selects a matching xClassification. This part is working, except the UI isn't updating with the selection of the xC...
  • PLMForge's avatar
    4 months ago

    Hello Bryan,

    Not sure if you found an alternative solution or if Aras ever got back with an enhancement, but after poking around the typescript files for the xclass form I was able to find a handler object with a method to refresh the xClass in the form after updating the AML local cache.


    I've included the javascript method that I was able to get working as an onChange field event for the classification field:

     

    const inn = aras.IomInnovator;
    const thisItem = document.thisItem;
    const itemClass = document.getElementsByName("classification")[1].value;
    if (!itemClass) return;
    
    const existingxClassRel = thisItem.getRelationships("Part_xClass");
    // Remove/delete any existing xclass relationships
    for (let i = 0; i < existingxClassRel.getItemCount(); i++) {
      const classRel = existingxClassRel.getItemByIndex(i);
      const isNew = classRel.getAttribute("isNew");
      isNew ? thisItem.removeRelationship(classRel) : classRel.setAttribute("action", "delete");
    }
    
    // Get corresponding xclass
    let xClassItem = inn.newItem("xClass", "get");
    xClassItem.setAttribute("select", "id,name");
    xClassItem.setProperty("name", itemClass);
    xClassItem = xClassItem.apply();
    
    const xClassId = xClassItem.getID();
    
    // Update local AML cache with new xclass
    let newxClassRel = inn.newItem("Part_xClass", "add");
    newxClassRel.setProperty("related_id", xClassId);
    thisItem.addRelationship(newxClassRel);
    document.item.setAttribute("isDirty", "1");
    
    // Refresh the HTML control to reflect the cache
    controlFormHandlers
      .refreshXClassesListInControl(document.item, xClassesControl, {
        isEditMode: document.isEditMode,
      })
      .then(function () {
        // Expand all xClass roots
        xClassesControl._nav.roots.forEach(function (key) {
          xClassesControl._nav.expand(key);
        });
        xClassesControl._fieldset.classList.add("aras-field-xclasses_expanded");
      });

    I included some logic to trigger after the refresh promise finished to automatically expand the properties under the class, as I always found it annoying it doesn't automatically do it by default.

    Obviously this may need to be adjusted depending on if you named your xClasses the same as the Part classifications, but as a POC I hope it can help you and others who run into this issue in the future.

    Sincerely

    Troy Reynolds