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
Sisodia SaurabhSisodia Saurabh 

passing parameter to URL in lightning component

Hi,

It was really simple to open URL in new window with passing parameters and some Validations using salesforce classic Onclick Java. for example:
if({!ISBLANK( Account.CSOne_Case_Number__c )})
{
window.open('https://.na29.visual.force.com/apex/Relationship360?rId={!Account.Gainsight_Relationship_ID__c}');
}

Now we are moving to Salesforce Lightning and As Onclick Javascript is not supported. can someone please let me know how can I achieve this in Lightning.

Thanks in advance.

Saurabh Sisodia
Best Answer chosen by Sisodia Saurabh
sfdcMonkey.comsfdcMonkey.com
use below code once :
({
    openActionWindow : function(component, event, helper) {
       
      var action = component.get("c.returnAccount");
        action.setParams({
            Id : component.get("v.recordId")
        }); 
        
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
              tempVars = response.getReturnValue();
			  var urlEvent = $A.get("e.force:navigateToURL");
                 urlEvent.setParams({
                 "url": '/apex/BayBridge?casenumber='+tempVars
                });
               urlEvent.fire();
            }
        });
        
    $A.enqueueAction(action);
        
    }
})
Thanks
let us know if it helps you
 

All Answers

sfdcMonkey.comsfdcMonkey.com
Yes, custom javaScript buttons are not support in lightning, please checkout below refernce for alternate of javaScript button in lightning :
https://trailhead.salesforce.com/en/modules/lex_javascript_button_migration

in additional you have need to create a lightning componnet and use this lightning component by Lightning Action (button).

Thanks , hopes this will helps you
let me know if it helps you and kindly close your query by choosing best answer so it will helps others in future
 
Sisodia SaurabhSisodia Saurabh
Hi Piyush,
Thanks for replying.
I have created component but problem is I am not able to pass {!Account.Gainsight_Relationship_ID__c}(which was automatically done by Javascript button in Classic) in URL.

Below is my component
 cmp:
<aura:component controller='GetAccount' implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
    
    <ui:button label="Open in New window" press="{!c.openActionWindow}"/>
</aura:component>

Js:
({
    openActionWindow : function(component, event, helper) {
       
      var action = component.get("c.returnAccount");
        action.setParams({
            Id : component.get("v.recordId")
        }); 
        var tempVars;
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
              tempVars = response.getReturnValue();
            }
        });
        window.open("/apex/BayBridge?casenumber="+tempVars);
    $A.enqueueAction(action);
        
    }
})

apxc
public with sharing class GetAccount {
    
    @AuraEnabled
    public static String returnAccount(Id Id)
    {
        system.debug(Id);
        string Gainsight_Rel_Id;
        List<Account> acc= [select Gainsight_Relationship_ID__c from account where Id=: Id];
        for(Account a: acc)
        {
            Gainsight_Rel_Id = a.Gainsight_Relationship_ID__c;
        }
        return Gainsight_Rel_Id;
    }

}

Srb
sfdcMonkey.comsfdcMonkey.com
use below code once :
({
    openActionWindow : function(component, event, helper) {
       
      var action = component.get("c.returnAccount");
        action.setParams({
            Id : component.get("v.recordId")
        }); 
        
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
              tempVars = response.getReturnValue();
			  var urlEvent = $A.get("e.force:navigateToURL");
                 urlEvent.setParams({
                 "url": '/apex/BayBridge?casenumber='+tempVars
                });
               urlEvent.fire();
            }
        });
        
    $A.enqueueAction(action);
        
    }
})
Thanks
let us know if it helps you
 
This was selected as the best answer
Sisodia SaurabhSisodia Saurabh
Awesome!!!! It worked. Thanks a Lot.
 
ArunaAruna
Hi  {!Piyush_soni__c}  or @Sisodia Saurabh I did try u r solution but is not working me getting below error. can anyone please help me out.

Unfortunately, there was a problem. Please try again. If the problem continues, get in touch with your administrator with the error ID shown here and any other related details.
Action failed: forceChatter:lightningComponent$controller$doInit [Right-hand side of 'instanceof' is not an object]
quickActionHandlerHelper.js failed to create component - forceChatter:lightningComponent


I need to convert below Java script button to lighting ready 
window.open( '/apex/ProductInUse?id={!Account.Id}');

Comp

<aura:component controller='ProductInUSe' implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
    <ui:button label="Open in New window" press="{!c.openActionWindow}"/>
</aura:component>

JS

({
    openActionWindow : function(component, event, helper) {
      var action = component.get("c.returnAccount");
        action.setParams({
            Id : component.get("v.recordId")
        }); 
        var tempVars;
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
              tempVars = response.getReturnValue();
            }
        });
        window.open("/apex/ProductInUse?id="+tempVars);
    $A.enqueueAction(action);
    }
})

Apex

public with sharing class ProductInUSe {
    @AuraEnabled
    public static String returnAccount(Id Id)
    {
        system.debug(Id);
        string accountId;
        List<Account> acc= [select Id,Name from Account where Id=: Id];
        for(Account a: acc)
        {
            accountId = a.id;
        }
        return accountId;
    }
}


 
Sisodia SaurabhSisodia Saurabh
Hi Aruna,

your code seems ok. Please change the browser and try again(use chrome).

Thanks,
Saurabh Sisodia
ArunaAruna
SOlution 

<aura:component controller="ProductInUseCntrl2" Implements="flexipage:availableForAllPageTypes,force:lightningQuickAction,force:hasRecordId" access="global" >
 <!--<lightning:button label="Open in new Window" onclick="{!c.openActionWindow}"/>-->
    
    <aura:handler name="init" value="{!this}" action="{!c.productInUse}"/>

</aura:component>
({
    productInUse : function(component, event, helper) {
        
        window.open( "/apex/productInUse2?id=" + component.get("v.recordId"));
       // window.alert("this is test message to check alert");
        window.close();
       $A.get("e.force:closeQuickAction").fire();
    }
})