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
Parteek Goyal 3Parteek Goyal 3 

how to pass the record id from aura component to javascript controller on click on hyperlink

Hi All,

I want to pass the record id from component to javascript controller on click on account name hyperlink.
 
<aura:iteration items="{!v.searchResult}" var="acc" indexVar="count">
                        <tr>
                            <td>
                                <div class="slds-truncate">{!count + 1}</div>
                            </td>
                            <td>
                                
                                <!--a href="{!'/one/one.app?#/sObject/'+ acc.Id + '/view'}" width="400" height="400" target="_blank"-->
                                <a onclick="{!c.openModel}">
                                    
                                	<div class="slds-truncate">{!acc.Name}</div>
                                </a>
                            </td>
                            <td>
                                <div class="slds-truncate">{!acc.PersonEmail}</div>
                            </td>
</tr>
</aura:iteration>

Please help me.

 
Best Answer chosen by Parteek Goyal 3
Deepali KulshresthaDeepali Kulshrestha
Hi Parteek,

Please go through with below code, it may be helpful to you.

Do like this 
In your component, assign the record Id to the button's Id

<aura:iteration items="{!v.newCases}" var="case">  
<button type="button" onclick="{!c.showCaseDeleteModal}" id={!case.Id}>Delete</button> 
</aura:iteration>

In your JS controller, capture the ID as as source of event:

showCaseDeleteModal: function(component, event, helper) {
var idx = event.target.id;
alert(idx);                       //here is your ID
});

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
 

All Answers

Lokesh Krishna SagiLokesh Krishna Sagi
Hi Parteek,

Use dataset in your anchor tag as below.. 
 
<a onclick="{!c.openModel}" data-id="{#v.count}"> 
  <div class="slds-truncate">{!acc.Name}</div> 
</a>


In your controller, get the clicked Account Name's Id as below..
 
var tar = event.target;
var accId = searchResult[tar.dataset.id].Id;



Here, event.target belongs to anchor tag and event.currentTarget belongs to <div> tag inside the anchor tag. Since we have set 'data-id' with indexVar of iteration, this gives the element position in the array (searchResult).

By passing that index position to array, we can get the entire Var --> acc in controller and get whatever the field value we need (Here Id).

Let me know if this helps,
Lokesh
Deepali KulshresthaDeepali Kulshrestha
Hi Parteek,

Please go through with below code, it may be helpful to you.

Do like this 
In your component, assign the record Id to the button's Id

<aura:iteration items="{!v.newCases}" var="case">  
<button type="button" onclick="{!c.showCaseDeleteModal}" id={!case.Id}>Delete</button> 
</aura:iteration>

In your JS controller, capture the ID as as source of event:

showCaseDeleteModal: function(component, event, helper) {
var idx = event.target.id;
alert(idx);                       //here is your ID
});

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
 
This was selected as the best answer
Parteek Goyal 3Parteek Goyal 3
Hi Deepali,

Thanks for your quick reply.

It's working fine when i am using button instead of <a> tag. could you please help with <a> tag.

Thanks,
Parteek