4

I have a lightning component, and I want that when I click a div, it redirect me to an object record. I guess I have to use an event but I don't know how it work exactly ? Do you have an example of this use case ?

Updates

I try, in my component, to add a link, but it doesn't work :

<aura:attribute name="actualites" type="Actualite__c[]"/>
<ol class="carousel-indicators">
    <aura:iteration var="a" items="{!v.actualites}">
        <a href="#" data-id="{!a.Id}" onclick="{!c.redirectToSobject}" style="text-decoration:none;">
        <li data-target="#myCarousel" data-slide-to="{!a.position-1}" class="{!a.classNameIndicateur}"></li>
        </a>
    </aura:iteration>
</ol>

with in the js :

    redirectToSobject: function (component, event, helper) {
    var navEvt = $A.get("e.force:navigateToSObject");
    navEvt.setParams({
      "recordId": component.get("v.Actualite__c").Id,
      "slideDevName": "Detail"
    });
    navEvt.fire();
}

How cant I create a link on a div ?

Thank you a lot !

3
  • Dou.. are you able to resolve the issue?
    – Ratan Paul
    Apr 14, 2016 at 9:30
  • Yes I do but with an other way for now... I just put a link like that <a href="{!'https://adress.my.salesforce.com/' + a.Id}">xxxx</a> but this is not a true solution...
    – dou
    Apr 15, 2016 at 9:49
  • hmm if you look into the official documentation they mentioned to use navigateTosobject methods etc.
    – Ratan Paul
    Apr 15, 2016 at 10:17

2 Answers 2

5

Use force:navigateToSObject

Documentation

redirectToSobject: function (component, event, helper) {
    var navEvt = $A.get("e.force:navigateToSObject");
    navEvt.setParams({
      "recordId": "00QB0000000ybNX",
      "slideDevName": "Detail"
    });
    navEvt.fire();
}

recordId: Required. The record ID.

slideDevName: Specifies the slide within the record view to display initially. Valid options are:

  • detail: The record detail slide. This is the default value.
  • chatter: The Chatter slide
  • related: The related information slide

Updates

Create one more component to redirect to sobject

<aura:iteration var="a" items="{!v.actualites}">
      <c:redirectToSobject objSobject="{!v.a}" /> 
</aura:iteration>

redirectToSobject Component

<aura:component >
    <aura:attribute name="objSobject" type="Sobject" required="true" />
    <a onclick="{!c.navigateToRecord}" style="width:100%;"> 
        View Record
    </a>
</aura:component>

redirectToSobject Controller

({
    navigateToRecord : function(component, event, helper) {
         var navEvent = $A.get("e.force:navigateToSObject");
         navEvent.setParams({
              recordId: component.get("v.objSobject").Id,
              slideDevName: "detail"
         });
         navEvent.fire(); 
    }
})
10
  • Thank you, I still have a question : what are the "recordId": "00QB0000000ybNX" and "slideDevName": "Detail" ?
    – dou
    Apr 12, 2016 at 10:16
  • @dou check my updated ans..
    – Ratan Paul
    Apr 12, 2016 at 10:34
  • thank you, for the recordId, can I put a dynamic Id (that I got from an apex controller ) ? Because my component only display the 3 last records and they can change...
    – dou
    Apr 12, 2016 at 10:51
  • 1
    @dou it depends on you. You can use directly dynamic Id or you can get the Id from lightning view using component.get("v.objSobject").Id
    – Ratan Paul
    Apr 12, 2016 at 10:53
  • so I can write , instead of "recordId": "00QB0000000ybNX" , "recordId": component.get("v.objSobject").Id
    – dou
    Apr 12, 2016 at 10:56
0

If it still doesn't work you may add a tag in your component as below:
<aura:dependency resource="markup://force:navigateToSObject" type="EVENT"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.