How to run an external script in SAP Analytics Cloud application / user story?

397 views Asked by At

I would like to run an externally hosted script in my SAC Analytic Application.

I know this is how it can be done in regular JavaScript:

function loadExternalScript() {
   var script = document.createElement('script');
   script.src = '/path/to/my/script.js';
   var head = document.getElementsByTagName("head")[0];
   head.appendChild(script);
}

Is there a way of doing the same thing in SAP's JavaScript-based language?

2

There are 2 answers

0
wookash On BEST ANSWER

Eventually I used a custom widget as a workaround.

This is (more or less) my widget's main component:

(function () {
    class ExternalScript extends HTMLElement {
        constructor() {
            super();            
        }

        connectedCallback() {
                var script = document.createElement('script');
                script.defer = 1;
                script.src = 'https://url.to.the.script.js';
                document.getElementsByTagName('head')[0].appendChild(script);
            }
        }     
    
    customElements.define("script-element", ExternalScript);
})();

And this is the json file I use to upload the widget to SAC environment:

{
    "id": "script",
    "version": "1.0",
    "name": "External Script",
    "newInstancePrefix": "external_script",
    "icon": "",
    "webcomponents": [
        {
            "kind": "main",
            "tag": "script-element",
            "url": "/main.js",
            "integrity": "",
            "ignoreIntegrity": true
        }
    ],
    "properties": {},
    "methods": {},
    "events": {}
}
0
Denis On

No, currently that´s not possible (and I guess: will never be possible)