4

I am having trouble finding this information.

I am finding lots of stuff on communicating between a lightning component and Visualforce page when the Visualforce page is embedded inside the lightning component as an iframe, which involves doing postMessages and what not.

However, my situation is that I have a Visualforce page that I put a lightning component on.

I want the user to fill out the form in the lightning component, then click 'OK', and be able to get that info back on the Visualforce page. However, I am unsure how you perform this kind of communication. The Lightning component is not in an iframe when you embed it in a Visualforce page, so doing some complicated postMessage should not be necessary.

2
  • 1
    You should be able to pass in a callback as an attribute when you instantiate the lightning component. I've done it before, but am not sure if Salesforce blocks this now... give me a few mins and I may get time to provide an example. Commented Sep 6, 2018 at 18:00
  • Sure, I'd like to see an example of this. Do I just set it up like: $Lightning.createComponent('c:myComponent',{'callback': someFunctionOnVFPage}, function(cmp){}); ? Hmm... I am unsure how you then trigger that from the Lightning component? Commented Sep 6, 2018 at 18:03

1 Answer 1

7

To communicate from the component to the VisualForce page, use a callback function. For example:

 <script>
 var someFunctionOnVFPage = function(param){
   alert(param);
 }

 $Lightning.createComponent('c:myComponent',
 {'callback':   someFunctionOnVFPage}, 
 function(cmp){});

In the component:

component.get('v.callback')('hey vf page!!');
2
  • Very nice, what do you put the callback to as far as attribute type goes? Commented Sep 6, 2018 at 18:16
  • Just make it an object... due to Javascript's type system, it can be anything, really. Commented Sep 6, 2018 at 18:19

You must log in to answer this question.

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