0

How do I pass apex query parameters to a third party url onclick in Lightning

@AuraEnabled
    public static Account getAccount()
    {
        Account a = [SELECT Id,name FROM Account LIMIT 1];
        return a;
    } 

cmp:

 <a click"{!c.Entersite}">Enter the site</a>

JS Controller

 Entersite: function (component, event, helper) {
 //I want something like www.google.com?id=accountid&Name=accountname
});
7
  • are you trying to hit a url and trying to a get response?
    – Praveen
    Commented Oct 1, 2016 at 17:30
  • No I just need to redirect to that particular URL
    – cartman
    Commented Oct 1, 2016 at 17:36
  • have tried window.location.href="https://www.google.com"?
    – Praveen
    Commented Oct 1, 2016 at 17:37
  • should I do that within the JS controller? and how do I pass the accountid and name values?
    – cartman
    Commented Oct 1, 2016 at 17:39
  • you have to call the @auraenabled method from the component's controller.js and get the accountid and name values.With that said, where are using the component, is it inside one/one.app or standalone app(eg:myappname.app)
    – Praveen
    Commented Oct 1, 2016 at 17:42

1 Answer 1

2

your @AuraEnabled method returns Account instance(you mentioned you would limit it to 1) which contains Name and Id.

Once you get the response, it will be an JSON Object, which can be retrieved by calling the getReturnValue().

{
    "Name":"Test Account",
    "Id":"001XXXXXXX"
}

Since above is an JS Object, you could the values of using . notation. Also make sure you are trying to use the field's API correctly, since everything in lightning is case-sensitive.

Entersite : function(component, event, helper) {
    var action = component.get("c.getAccount");
    action.setCallback(this,function(response){
            if (component.isValid() && response.getState() === "SUCCESS"){
                window.location.href = "https:\\www.google.com?id="+response.Id+"&Name="+response.Name;
            }
        });
    $A.enqueueAction(action);
}

NOTE : Also for every async callback's it is recommend to check if the component exist by doing component.isValid()

You must log in to answer this question.

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