How to Navigate to the record/get the record id in <Aura:iteration>
Lightning framework from SFDC is a component based framework ,hence I would suggest you to break your app into as much as possible
Lets create a new component for each line item and called it opportunityCard
<aura:component>
<aura:attribute name="oppty" type="Opportunity"/>
<span>
<a href = "" aura:id="{!v.oppty.Id}" onclick = "{!c.navigateToOppty}">{!v.oppty.Name}</a>
</span>
<span>
<a href = "" aura:id="{!v.oppty.AccountId}" onclick = "{!c.navigateToAccount}">{!v.oppty.Account.Name}</a>
</span>
</aura:component/>
Your parent component will simplify like below
<aura:component>
<aura:attribute name ="OpptyList" type = "Opportunity[]"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<div>
<aura:iteration items = "{v.OpptyList}" var = "opp" >
<c:opportunityCard oppty="{!opp}"/>
</aura:iteration/>
</div>
</aura:component>
You can directly use attribute now in your controller js .Note that this is a controller for opportunityCard
opportunityCardController.js
({
navigateToOppty:function(component){
// it returns only first value of Id
var Opprec = component.get("v.oppty");
var sObectEvent = $A.get("e.force:navigateToSObject");
sObectEvent .setParams({
"recordId": Opprec.Id ,
"slideDevName": "detail"
});
sObectEvent.fire();
},
navigateToAccount.:function(component){
// it returns only first value of Id
var AcctId = component.get("v.oppty").AccountId;
var sObectEvent = $A.get("e.force:navigateToSObject");
sObectEvent .setParams({
"recordId": Acctid,
"slideDevName": "detail"
});
sObectEvent.fire();
},
})
NOTE : You can use helper class for your controller and reduce some duplicate code