Getting an item property in onChangedCell from a recently created relationship vs existing relationship

オフライン

I'm running a method on the property event "onChangedCell". When the method is called on a newly created related item, the following statement successfully get's the property:

var my_item = parent.item.querySelector("#" + relatedID);
var my_property = aras.getItemProperty(my_item, "my_property");

The problem though is when a user edits the cell of an existing related item, I don't get the related item with the query selector. How do I handle the different scenarios of the related item being newly created and the related item being loaded since it is an existing relationship? Since the user just finished changing the property I figured that property at least would be in the cache similar to a newly created item but I'm obviously missing something.

Thank you in advance!

-Ken

  • Hi Ken,

    I tried running this in an 11.0 SP15 instance I have installed locally, and I was able to get the existing related item using your sample above. I think the issue you may be running into is that the querySelector needs special formatting for IDs that begin with a number. You can see the sample below for how you might handle these kinds of IDs.

    var my_item;

    var firstChar = relatedID.substring(0, 1);
    if (isNaN(parseInt(firstChar)))
    {
    my_item = parent.item.querySelector("#" + relatedID)
    }
    else
    {
    // If the ID begins with a number, we need to escape that character
    firstChar = "\\3" + firstChar;
    var restOfId = relatedID.substring(1);
    my_item = parent.item.querySelector("#" + firstChar + " " + restOfId);
    }

    var my_property = aras.getItemProperty(my_item, "item_number");
    alert(my_property);

    Chris

    Christopher Gillis

    Aras Labs Software Engineer

  • +1 オフライン in reply to Christopher Gillis

    Thanks Chris! I'm using 11sp12. I did some digging and it turns out all my test cases happened to begin with a non-number character. So this didn't fix the problem I was running into. However, I created some more items till I got a number as the first character and the code you provided helped with that case. Thank you for your help resolving another problem before it even happened! Slight smile

    Trying to find out more about when/why the method fails, I dug more into the debugger and found that I'm not always getting an item element back from parent.item.querySelector(), sometimes I'm just getting the relatedID back: 

    When it works:
    my_item = item#9D10DBF4F748479981FD06B4CC4C31B6 {namespaceURI: null, prefix: null, localName: "Item", tagName: "Item", id: "9D10DBF4F748479981FD06B4CC4C31B6", …}

    When it doesn't work: 
    my_item = related_id#9D10DBF4F748479981FD06B4CC4C31B6 {namespaceURI: null, prefix: null, localName: "related_id", tagName: "related_id", id: "9D10DBF4F748479981FD06B4CC4C31B6", …}

    I realized I needed to define a better selector to get the correct element from the dom since querySelector() just returns the first match. I updated the code Chris provided to include updates to the CSS selector:

    var my_item;
    var firstChar = relatedID.substring(0, 1);
    if (isNaN(parseInt(firstChar))){
        my_item = parent.item.querySelector("Item[id=" + relatedID + "]");
    }
    else {
        // If the ID begins with a number, we need to escape that character
        firstChar = "\\3" + firstChar;
        var restOfId = relatedID.substring(1);
        my_item = parent.item.querySelector("Item[id=" + firstChar + " " + restOfId + "]");
    }
    var my_property = aras.getItemProperty(my_item, "item_number");
    alert(my_property);
    

    This seems to have resolved the issue I was having and the issue Chris preemptively helped with.

    Thanks again Chris!