1

I need to make a visualforce page communicate with a lightning component. Through lightning component I have to call a global javascript function of a chrome extension. For this purpose I used postMessage. The component is inserted in the lead detail. The problem is the following: when I first open a lead and call the function, the global function is called only once. If I open another lead from the same window it is as if the listener was added again and then the external function is called twice. If I open a third lead, the function is called three times and so on. The visualforce page and the lightning component are two separate components in the lightning page. How can I solve the problem?

Example: https://sfdcfacts.com/visualforce/share-data-between-lightning-component-visualforce-page/

This is my controller:

doInit : function(component,event,helper) {         
    helper.processMessage();
}

This is my helper:

processMessage : function() {
    window.addEventListener("message", function(event) {
        if (event.data.source != undefined && (event.data.source == 'LeadPageLex' || event.data.source == 'AccountPageLex')){               
            var numberToCall = event.data.payloader;
            var user = event.data.user;
            var recordId = event.data.recordId;
            //call global funciont      
        } 
    }, false);
}

When I open a record, the method of the component (doInit) is executed every time and consequently also the helper method.

1 Answer 1

2

Why not add a globally scoped variable using window.variableName ? its scope is for the whole browser tab.

processMessage : function() {
    if(!window.isListenerSet){
        window.addEventListener("message", function(event) {
        if (event.data.source != undefined && (event.data.source == 'LeadPageLex' || event.data.source == 'AccountPageLex')){               
            var numberToCall = event.data.payloader;
            var user = event.data.user;
            var recordId = event.data.recordId;
            //call global funciont      
            } 
        }, false);
        window.isListenerSet = true;
    }

}
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .