0

From a lightning component I have to open a url but the problem I need to do the check before, and for that I created action.setCallBack where my check is done.

the problem here is that window.open ('URL'); it is not running. is there any solution for that ?

    redirect : function(component, event, helper) {

        var action = component.get("c.method");
        var recId = component.get("v.recordId");
        action.setParams({ 
            Id : component.get("v.recordId")});
        action.setCallback (this, function(response){
            var state = response.getState();
            if (state === "SUCCESS"){
                component.set("v.recordId", response.getReturnValue());
                if (response.getReturnValue() != condition){  
                    window.open(**URL**) ;
                }               
            }  
 });
 $A.enqueueAction(action);
3
  • Could you please add a console.log just before the condition where the window.open is, also console.log( response.getReturnValue() )and please add a chrome extension(if you are using chrome) that detects all the Lightning errors - Salesforce Lightning Inspector. Also please share as to what you have specifically written in the window.open method Aug 17, 2018 at 14:54
  • well, I checked the console.log , and seems that everything works fine as expected. if I call the windows.open just before closing the redirect, it works fine.
    – mayMay
    Aug 17, 2018 at 15:11
  • Could you please add the extension and look for some errors that generally are not pooped in the browser console? Aug 17, 2018 at 15:30

4 Answers 4

1

Using below code you can navigate form lightning.

var urlEvent = $A.get("e.force:navigateToURL");
urlEvent.setParams({
   "url": someurl   
});
urlEvent.fire();
5
  • To the point answer and +10 for that but the standard events for Lightning do not work when the component is embedded inside a VF page. Aug 17, 2018 at 14:57
  • OP is wondering why window.open is not working from callback in her case, and perhaps not exploring alternatives yet. Aug 17, 2018 at 15:02
  • @MohitArora are you talking about Lightning Out? Thanks for cleafication :)
    – user59378
    Aug 17, 2018 at 15:59
  • @Kal whenever you create a lightning Component Salesforce create Iframe. that's why it cause issue for redirecting. For redirection we always use this in our apps
    – user59378
    Aug 17, 2018 at 16:05
  • @ChandraKiran Yes, embedding a lightning component in a VF page and using a lightning component in ltng:out are nearly the same because in both ways they use the same script to render a lightning component. The standard events are only handled by one.app container in lightning. Aug 17, 2018 at 16:46
1

I just wanted to add a possible reason why this window.open failed in the first place.

Browsers are increasingly designed to prevent unwanted pop-up windows. As a result, if you try and open a brand new window that is not in direct response to an interaction (click, keystroke etc) by the user, the built-in pop-up blocker usually stops it.

Because you are doing asynchronous work between the user's interaction and the window.open, the browser no longer considers it to be in direct response to user interaction and blocks the pop-up. So one alternative, as you have noted, is a same-window redirect. Another alternative is a flow that opens the window immediately in response to an interaction and then does the processing. For example you could open a Visualforce page in the new window that runs your decision logic in the Apex controller and then either does a redirect or closes itself.

0

I noticed that your $A.enqueueAction(action); is outside the scope of redirect method. Perhaps that? Throw in a console.log in your callback and see if it shows up at all in browser logs?

redirect : function(component, event, helper) {
    var action = component.get("c.method");
    var recId = component.get("v.recordId");
    action.setParams({ 
        Id : component.get("v.recordId")
    });
    action.setCallback (this, function(response){
        var state = response.getState();
        if (state === "SUCCESS"){
            component.set("v.recordId", response.getReturnValue());
            console.log('---------> SUCCESS ' + component.get("v.recordId"));
            if (response.getReturnValue() != condition){  
                window.open('<URL>');
            }               
        }
        else
            console.log('---------> ERROR ');
    }
    $A.enqueueAction(action);
}

And on a side note: depending on the URL you are trying to redirect to, perhaps lightning:navigation would be a more prudent approach...?

1
  • no it is inside, it was just a mistakke when creating the question.
    – mayMay
    Aug 17, 2018 at 15:09
0

it was just to replace it by this function and call it.

function urlEvent() { location.href = 'URL'; }

1
  • Can you please add some more relevant code part and explain it? How it is working, where you created this function? Sep 23, 2018 at 15:08

You must log in to answer this question.

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