function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Valli KValli K 

not able to migrate my javascript button in lightning experience. please help me to convert the below button. I am new to Lightning. current onclick javascript action: window.location.href ='/apex/RequestType?&id={!Opportunity.Id}';

not able to migrate my javascript button in lightning experience. please help me to convert the below button. I am new to Lightning. current onclick javascript action: window.location.href ='/apex/RequestType?&id={!Opportunity.Id}';
Best Answer chosen by Valli K
GhanshyamChoudhariGhanshyamChoudhari
You need to use doinit handler
<aura:component implements="force:lightningQuickAction,force:hasRecordId" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.doinit}" /> 
</aura:component>

controller
({
	doinit : function(component, event, helper) {
              var id=component.get("v.recordId");
        window.open("/apex/P2EvRequestType?id="+id,'_blank');
		
	}
})

 

All Answers

Pradeep SinghPradeep Singh
Hi,
Please refer below link, this might give you some information for the same.
https://trailhead.salesforce.com/modules/lex_javascript_button_migration/units/javascript_button_alternatives
GhanshyamChoudhariGhanshyamChoudhari
You need to create a action button with component.

component
<aura:component implements="force:lightningQuickAction,force:hasRecordId">
    <lightning:button variant="brand" label="Submit" onclick="{! c.handleClick }" />
</aura:component>

controller
 
({
	handleClick : function(component, event, helper) {
		window.open("/"+component.get("v.recordId"),'_blank');
	}
})

 
Narender Singh(Nads)Narender Singh(Nads)
Hi,

I would strongly advice you go through this trailhead Module:
https://trailhead.salesforce.com/modules/lex_javascript_button_migration/units/javascript_button_alternatives

Also, refer to this link to see how to navigate to URLs from lightning components.
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_force_navigateToURL.htm

let me know if it helps.
Thanks!
 
Valli KValli K
Thank you all for quick response. 
@GhanshyamChoudhari
The code which you have provided with this its redicting to a Opportunity.Id, but i want this to redirect to a another Vf page. 
How& where should i use the below link in the given code.
window.location.href ='/apex/RequestType?&id={!Opportunity.Id}';
 
Valli KValli K
Please anyone help me in this
GhanshyamChoudhariGhanshyamChoudhari
window.open("apex/RequestType?&"+component.get("v.recordId"),'_blank');
make sure that in v.recordId opportunity id is coming
Narender Singh(Nads)Narender Singh(Nads)
Hi,
Try this:
 window.open("/apex/RequestType?id="+component.get("v.recordId"),'_blank');

Let me know if it helps.
Thanks!
Valli KValli K
It is redirecting now, for button click it is asking twice means once i click on the button .. again one more popup window is opening showing button. double time need to click to redirect it to vf page. Can we make it single time click here.
<aura:component implements="force:lightningQuickAction,force:hasRecordId">
    <!--<aura:handler name="init" value="{!this}" action="{!c.handleClick}" /> --> 
    <lightning:button variant="brand" label="New RFI" onclick="{! c.handleClick }" />
</aura:component>
({
    handleClick : function(component, event, helper) {
        var id=component.get("v.recordId");
        window.open("/apex/P2EvRequestType?id="+id,'_blank');
    }
})

 
GhanshyamChoudhariGhanshyamChoudhari
You need to use doinit handler
<aura:component implements="force:lightningQuickAction,force:hasRecordId" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.doinit}" /> 
</aura:component>

controller
({
	doinit : function(component, event, helper) {
              var id=component.get("v.recordId");
        window.open("/apex/P2EvRequestType?id="+id,'_blank');
		
	}
})

 
This was selected as the best answer
Narender Singh(Nads)Narender Singh(Nads)
Hi Padma,
I created a component to test the above and it's working just fine. It redirects me to my VF page on a single click.
Valli KValli K
Yes. redirecting but when i go and see my opportunity detail page Button pop up is still being in opty. 
Access= global means what, because in lightning created a new button for this. May be because of the my old javascript button which is existing in classic that is not working. Access =global, are we making it as a global for both class & lightning?
 if yes what about the old one?
Valli KValli K
@GhanshyamChoudhari
It may because of in lightning when i click on the button, redirection of vf page is opening in new tab, but in classic in the opportunity page itself vf page is redirecting. can we do something here to fix this.
Narender Singh(Nads)Narender Singh(Nads)
Hi Padma,
A quick question, you want to redirect to the vf page in same window or you want to open it in a new tab?
Valli KValli K
same window
GhanshyamChoudhariGhanshyamChoudhari
Hi ,
In controller instead of _black use _self or _parent and try
Valli KValli K
did not worked both..
GhanshyamChoudhariGhanshyamChoudhari
Hi pdman,

instead of _blank use _self  and make sure you are refreshing you record at least 2 times and then click on the action button.
 
Valli KValli K
coming in same window, but first blank window is coming and then redirecting the page.
GhanshyamChoudhariGhanshyamChoudhari
hi Padma , that is a standard behaviour of salesforce we can't handle it.
please, markthe best answer if it helps you.

thanks
 
Narender Singh(Nads)Narender Singh(Nads)
Hi, 
Try this in your controller:
handleClick: function (component, event, helper) {
    var id=component.get("v.recordId");
    var Url='/apex/P2EvRequestType?id='+id;
    var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
      "url": Url
    });
    urlEvent.fire();
}

This should do the trick.

P.S: Sorry for the late reply.
 
Valli KValli K
Same issue , first blank page pop up is opening with label name "New RFI" as header and redirecting the page.that blank pop up should not come rest everything is fine. Thanks.
Narender Singh(Nads)Narender Singh(Nads)
Hi, 
Another alternative solution:
handleClick: function (component, event, helper) {
    var id=component.get("v.recordId");
    var Url='/apex/P2EvRequestType?id='+id;
    window.open(Url,'_top');
}

Let me know if it helps.
Thanks!
Narender Singh(Nads)Narender Singh(Nads)
Hi,
I tried this code in my component, its not giving any pop-up.
Valli KValli K
in which action we have to add this component. quick action or global action?
Valli KValli K
And Narender please post me your component once.
Narender Singh(Nads)Narender Singh(Nads)
Hi,
This is my JS controller code:
({
    handleClick: function (component, event, helper) {
    var id=component.get("v.recordId");
    var Url='/apex/P2EvRequestType?id='+id;
    var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
      "url": "/apex/DelVF"//I USED THIS URL SINCE MY VF PAGE'S NAME IS DelVF
    });
    urlEvent.fire();
        
    //window.open('/apex/DelVF','_top')
}
})

I place this component on the Account record Page. But you can use it as a lightning quick Action, it won't make any difference.

Let me know if this solves your purpose.
Thanks!
Valli KValli K
please add ur aura:component page a quick action, then you can find the same issue.
Narender Singh(Nads)Narender Singh(Nads)
Hi Padma,

I see now what your problem is.
And sorry but unfortunately there is no way to hide that pop-up because it's a standard functionality. Nothing can be done about this.

What you can do is add a spinner and show a custom message, like "Redirecting to ABC page" to your lightning component.
 
Valli KValli K
Thank you Narender Singh & GhanshyamChoudhari for quick responses.. Thank you so much..
Valli KValli K
Thank you Narender Singh & GhanshyamChoudhari for quick responses.. Thank you so much..
Narender Singh(Nads)Narender Singh(Nads)
Hi,
Please mark a best answer so that the others with similar kind of issue can benefit from this post.
Thanks!
usersfdc21usersfdc21
Hi Ghanshyam Choudhari,

Thank you very much. it was useful