[SalesForce] Get url into lightning component – Redirection to an object record

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

Best Answer

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.

Related Topic