Hi folks. I've got some code that exists as inject...
# suitescript
m
Hi folks. I've got some code that exists as injected HTML by creating a field on my custom record and doing
inlineHtmlField.defaultValue = '<script src="' + myFileUrl + '"></script>';
on it. The injected code is then to the tune of:
Copy code
(function() {
    require(["N/currentRecord"], (currentRecord) => {
        $(document).ready(function () {
            // Do stuff.
        });
    });
})();
I use currentRecord in order to retrieve my record's ID. So! This works perfectly in multiple places... except one. One particular custom record of mine, let's call it
MyRecord
, it's just like all the other ones from what I can see... but this code doesn't work, and currentRecord.id returns
null
. To verify this, I took that code above and copy-pasted it in the browser console, just to see if I can get the ID. Works everywhere except in
MyRecord
where the returned ID is null. After some testing, I realized that in my injected HTML, I can require anything except N/currentRecord. If I take out that requirement, my record ID is available in the console. Soon as I put N/currentRecord back in, boom, ID's gone. Again, that happens only on
MyRecord
, which is all the more puzzling. The record has a couple of workflows connected to it, both of which I deactivated to diagnose. No dice. Behavior remains the same. The only script active on
MyRecord
is the UE script that fires at beforeLoad() and injects the HTML. That's it. ... Well, that's the issue I'm dealing with. Any clues on what could be causing this? I'm rather mystified by this at the moment.
b
I hate to be the guy who doesn't focus on the question and provides an alternate answer but... if all else fails try using nlapiGetRecordId() , pretty sure that's going to get you the record id running it in the console. You'll be mixing SS2.x, SS1.0 and a DOM hack all in one script.
m
Interesting. I gave
nlapiGetRecordId()
a go in the console and it does give me the ID. Meanwhile I've been diagnosing with a coworker and if I wrap my injected code into a window.timeout(), it begins working. So, something isn't ready by the time the document itself has finished loading... 🤔
😲 1
SOLVED! The order of operations had been messed up. Escaping my notice was the fact that the
require
was happening before
jQuery(document).ready()
Switching them with each other so the require happens afterwards solved it. This means that all my other records using that same sort of injection were simply getting lucky so far. So, to reiterate, the proper way I should've done this is:
Copy code
(function() {
    $(document).ready(function () {
        require(["N/currentRecord"], (currentRecord) => {
            // Do stuff.
        });
    });
})();
👍 1