0

I am a beginner in learn lightning. I am trying to create a custom component to display open cases on the home page for a user. Using onlick to open the respective record. But I am unable to retrieve the recordid for page reference. The logs shows undefined. Any suggestions?

Cmp:

<aura:component controller="getOpencaseapex" implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId"  access="global">

    <aura:attribute name="casel" type="Case[]"/>
      <aura:attribute name="url" type="String"/>
     <aura:attribute name="pageReference" type="Object"/>
    <aura:handler name="init" value ="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="recordId" type="String" />
    <lightning:navigation aura:id="navService"/>
    <p> MY OPEN CASES</p>
    <aura:iteration items="{!v.casel}" var="cs">

        <div class="itemTitle body" data-id="{!cs.Id}" onclick="{!c.gotorecord}">
            <ul >
                <li >
                    <h3>{!cs.CaseNumber}</h3>

                </li>
                <li>
                    <ui:outputText value="{!cs.Status}" />
                </li>
            </ul>
        </div>
    </aura:iteration>
</aura:component>

JS:

({
    doInit : function(cmp, event, helper) {


        var action = cmp.get("c.opencase");

        console.log('calling doInit');

        action.setCallback(this, function(response) {
            var state=response.getState();
            if(state==="SUCCESS"){
                cmp.set("v.casel",response.getReturnValue());

                console.log('getting cases');
                console.debug(response.getReturnValue().length);
                console.log(response.getReturnValue());
            }
             else {
                console.debug(response.error[0].message);
             }


        });
        $A.enqueueAction(action);
    },
    gotorecord : function(cmp,event,helper) {

        var id = cmp.get("v.recordId");
        console.log(id);
        var navService = cmp.find("navService");

        var pageReference = {
            type: 'standard__RecordPage',
            attributes: {
                recordId:id,
                objectApiName: 'Case',
                actionName: 'view'
            }
        };
        cmp.set("v.pageReference", pageReference);
        console.log('@@',pageReference);

        var defaultUrl = "#";
        navService.generateUrl(pageReference)
            .then($A.getCallback(function(url) {
                cmp.set("v.url", url ? url : defaultUrl);
                console.log('@@url' ,v.url);
            }), $A.getCallback(function(error) {
                cmp.set("v.url", defaultUrl);
            }));




       var pageReference  = cmp.get("v.pageReference");
        event.preventDefault();
        navService.navigate(pageReference );

}
})
2
  • Hi, and welcome to SFSE! In the future, please remember to use the {} button in the editor to format your code properly. Nice job on overall on your first post, though. Here's hoping you learn a lot from the community. If you haven't yet, I invite you to read about How to Ask and How to Answer.
    – sfdcfox
    May 29, 2020 at 7:30
  • Two things that jump out at me. force:hasRecordId would give your component a recordId attribute automatically - you shouldn't define one. Second, the component needs to be placed on a record page for recordId to be auto-filled. My guess is that the recordId is not immediately available in this context. No worries, you should be able to pull it off of the case record you're getting in doInit.
    – nbrown
    May 29, 2020 at 13:24

1 Answer 1

0

You should get the ID from the element:

gotorecord : function(cmp,event,helper) {
  var id = event.target.dataset.id;
1
  • Hello, Thank you for the all the tips. I am now able to understand the use of force:hasRecordId . And yes, I was able to retrieve the id of the record clicked as well. :) Jun 9, 2020 at 6:00

You must log in to answer this question.

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