1

I have following JS file uploaded as static resource in Salesforce.

Static resource utility.zip / utility.js file

var x = function (a, b) {return a * b};

I would like to call function variable from static resource JS file from LWC component.

    import { loadScript, loadStyle } from 'lightning/platformResourceLoader';
    import utility from '@salesforce/resourceUrl/utility';

    export default class lwccomponent extends LightningElement {

     renderedCallback() {

        Promise.all([
            loadScript(this, utilities + '/utility.js')
        ])
            .then(() => {
                alert('loaded');
                /* how can i call method / function variable from JS file , i am unable to do it. */
            })
            .catch(error => {
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error loading JS file',
                        message: error.message,
                        variant: 'error'
                    })
                );
            });

    }
}

my objective is to create a common set of functions to share among components/ project to have to prevent any data issues.

  • Can you please explain what kind of problem do you met during achieving your objective? – kurunve Apr 7 at 12:05
  • Thanks for your comment. I am unable to see the file in developer tools - network section , meaning file is not getting loaded or unable to call method and it says function in undefined. @kurunve – rahulsfdx Apr 7 at 12:06
  • You need to make your static resource a module, and not a plain js -- check similar question here: salesforce.stackexchange.com/questions/295469/… – kurunve Apr 7 at 12:12
  • Thanks @kurunve i checked this question but answer to this i am not looking for, i really wanted to call , method from JS file loaded from static resource no other option. – rahulsfdx Apr 7 at 12:13
  • agree with Vova. If you want to develop code, that will be shared with lwc components, create lwc service component. Commonly, js is loaded as a static resource for already existed 3-party js libraries. And they must be compatible with locker service. – Oleksandr Berehovskyi Apr 7 at 12:15
4

If you want to develop code, that will be shared with lwc components, create lwc service component. Commonly, js is loaded as a static resource for already existed 3-party js libraries, but if you don't have any options except grouping js code in static resource, your js code should follow the following rule: must explicitly attach library global to window

window.myLib = (function() {
    return {
        myFunction: function(a, b) {
            return a * b;
        }
    };
}());

then you can call it like:

loadScript(this, utilities + '/utility.js').then(() => {
    let result = myLib.myFunction(2,2);
});

you can check rest of requirements for the compatible js library with locker service in this question

| improve this answer | |
  • Thanks @oleksandr-berehovskyi, let me try this and get back to the question. appreciate your response. – rahulsfdx Apr 7 at 12:23
  • This is just perfect and worked, thanks @oleksandr-berehovskyi for your answer. – rahulsfdx Apr 7 at 14:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.