3

I have a lightning component in which I wnat to make a link that redirect to an object record :

<aura:iteration var="a" items="{!v.actualites}">    
  <a href="{! a.Id}" data-id="{!a.Id}" style="text-decoration:none;" target="blank"> an image here </a>
</aura:iteration>

the problem is that when I click the image, the rediction does not work because I got the url : https://mysite.salesforce.com/c/a0o4E0000004EaKQAU And I want the url : https://mysite.salesforce.com/a0o4E0000004EaKQAU so I need to suppress the "/c" ...

I don't know how to do that... Maybe I need to declare a string in my apex controller wich contain the right url but how can I get it in my component ?

I create a function in my apex controller :

public static String getDebutUrl(){

    return URL.getSalesforceBaseUrl().toExternalForm();
}

but I don't know how to retrieve the string in the js controller of my component

1
  • Updated my answer with another solution, using force:navigateToSObject
    – dBeltowski
    Apr 20, 2016 at 13:26

4 Answers 4

2

in the Controller.js, set an url variable in the callback:

component.set("v.url", window.location.origin);

"window.location.origin" takes only the host and the protocol from the url ("https://yourInstance-dev-ed.lightning.force.com")

Then in your component, add an aura:attribute tag to get the url variable

<aura:attribute name="url" type="String" />

Then, in the iteration, add an "a" tag to create the url, then concat the url to the object id. In this example I'm creating a link to Contacts:

<aura:iteration items="{!v.contacts}" var="contact">
    <li class="minli">
        <a target="_blank" href="{!v.url +'/'+ contact.Id}">{!contact.Name}</a>
    </li>
</aura:iteration>

Note the part inside the href attribute: {!v.url +'/'+ contact.Id}

Hope it helps.

1

Can you use URLFOR()?

<a href="{!URLFOR($Action.Actuality__c.View, a.Id)}" data-id="{!a.Id}" style="text-decoration:none;" target="blank"> an image here </a>

Here is a blog by Sam Arjmandi that helps explain it.

Looks like you need to use force:navigateToSObject

Navigates to an sObject record specified by recordId.

To display the record view, set the record ID on the recordId attribute and fire the event. The record view contains slides that displays the Chatter feed, the record details, and related information. This example displays the related information slide of a record view for the specified record ID.

createRecord : function (component, event, helper) {
    var navEvt = $A.get("e.force:navigateToSObject");
    navEvt.setParams({
      "recordId": "00QB0000000ybNX",
      "slideDevName": "related"
    });
    navEvt.fire();
}
2
  • It seems I can't use URLFOR, when I try to save it says "No function found for key: URLFOR: Source"
    – dou
    Apr 20, 2016 at 7:27
  • is there no easier way? a single line of code that can be put in the component? This is just a hyperlink! the base foundation of the web!
    – dacology
    Dec 14, 2016 at 11:31
1

No need of unnecessary line of code :D

Just use the below code in your component

<aura:iteration items="{!v.contacts}" var="contact">
<li class="minli"> 
     <h3 ><a target="_blank" href="{!'/'+contact.Id}">{!contact.Name}</a></h3>                                  
</li>

No need of using var url and all, remove all.

Thanks, Pranav

0

try with below code

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

an image here

You must log in to answer this question.

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